Spring data OneToOne with shared id not working

2019-09-13 20:18发布

I have following two classes:

Parent class:

@Entity
@Table(name = "parent_object")
public class ParentObject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    @Column(name = "age", nullable = false)
    private Long age;

    @OneToOne(orphanRemoval = true, cascade = CascadeType.ALL, mappedBy = "parentObject")
    private ChildObject childObject;

    public ParentObject() {
    }

    public ParentObject(Long age, ChildObject childObject) {
        this.age = age;
        this.childObject = childObject;
    }

    public Long getId() {
        return id;
    }

    public Long getAge() {
        return age;
    }

    public ChildObject getChildObject() {
        return childObject;
    }
}

And child class:

@Entity
@Table(name = "child_object")
public class ChildObject implements Serializable {
    @Id
    @OneToOne
    @JoinColumn(name = "id")
    private ParentObject parentObject;

    @Column(name = "name")
    private String name;

    public ChildObject() {
    }

    public ChildObject(String name) {
        this.name = name;
    }

    public ParentObject getParentObject() {
        return parentObject;
    }

    public String getName() {
        return name;
    }
}

Schema:

create table parent_object
(
    id bigserial not null
        constraint pk_parent_object
            primary key,
    age bigint not null
)
;
create table child_object
(
    id bigint not null,
    name varchar(32)
);

Repository:

@Component
public interface ParentObjectRepository extends JpaRepository<ParentObject, Long>, JpaSpecificationExecutor<ParentObject> {
}

The error is thrown when saving object:

ParentObject parentObject = new ParentObject(12L, new ChildObject("name"));
parentObjectRepository.save(parentObject);

Error message:

2017-04-13 09:27:27.672 DEBUG 4496 --- [io-12500-exec-1] org.hibernate.SQL                        : insert into parent_object (age) values (?)
Hibernate: insert into parent_object (age) values (?)
2017-04-13 09:27:27.677 DEBUG 4496 --- [io-12500-exec-1] org.hibernate.SQL                        : select currval('parent_object_id_seq')
Hibernate: select currval('parent_object_id_seq')
2017-04-13 09:27:27.688 DEBUG 4496 --- [io-12500-exec-1] org.hibernate.SQL                        : insert into child_object (name, id) values (?, ?)
Hibernate: insert into child_object (name, id) values (?, ?)
2017-04-13 09:27:27.693  WARN 4496 --- [io-12500-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23502
2017-04-13 09:27:27.693 ERROR 4496 --- [io-12500-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column "id" violates not-null constraint
  Detail: Failing row contains (null, name).
2017-04-13 09:27:27.694  INFO 4496 --- [io-12500-exec-1] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2017-04-13 09:27:27.705 ERROR 4496 --- [io-12500-exec-1] c.r.c.c.s.mvc.advice.ExceptionAdvice     : Unexpected server error occurred: could not execute statement; SQL [n/a]; constraint [id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
....
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
  Detail: Failing row contains (null, name).
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:135) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    ... 106 common frames omitted

Any ideas what is wrong, i.e. why ChildObject does not get id from ParentObject?

Update: This is a code which works:

@Entity
@Table(name = "parent_object")
public class ParentObject {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    @Column(name = "age")
    private Long age;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "parentObject")
    private ChildObject childObject;

    public ParentObject() {
    }

    public ParentObject(Long age, ChildObject childObject) {
        this.age = age;
        this.childObject = childObject;
    }

    public Long getId() {
        return id;
    }

    public Long getAge() {
        return age;
    }

    public ChildObject getChildObject() {
        return childObject;
    }

    public void setChildObject(ChildObject childObject) {
        this.childObject = childObject;
    }
}

@Entity
@Table(name = "child_object")
public class ChildObject {
    @Id
    @Column(name = "child_id", unique = true, nullable = false)
    private Long id;

    @MapsId
    @OneToOne
    @JoinColumn(name = "child_id")
    private ParentObject parentObject;

    @Column(name = "name")
    private String name;

    public ChildObject() {
    }

    public ChildObject(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public ParentObject getParentObject() {
        return parentObject;
    }

    public String getName() {
        return name;
    }

    public void setParentObject(ParentObject parentObject) {
        this.parentObject = parentObject;
    }
}

However, I really don't like to set parentObject to ChildObject:

    ChildObject childObject = new ChildObject("name");
    ParentObject parentObject = new ParentObject(12L, childObject);
    childObject.setParentObject(parentObject);

Is this the only way to have shared id? If parentObject is not set explicitly, then following error is thrown:

Caused by: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [test.ChildObject.parentObject]
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:83) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]

0条回答
登录 后发表回答