共享ID不起作用的Spring数据OneToOne

时间:2017-04-13 09:37:31

标签: java jpa spring-data

我有两节课:

家长班:

@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;
    }
}

儿童班:

@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;
    }
}

架构:

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)
);

存储库:

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

保存对象时抛出错误:

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

错误讯息:

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

任何想法有什么问题,即为什么ChildObject没有从ParentObject获取id?

更新: 这是一个有效的代码:

@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;
    }
}

但是,我真的不想将parentObject设置为ChildObject:

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

这是获得共享ID的唯一方法吗?如果未明确设置parentObject,则抛出以下错误:

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 个答案:

没有答案