添加新的关联JPA实体将尝试插入id为空的行

添加新的关联JPA实体将尝试插入id为空的行,jpa,spring-data,spring-data-jpa,Jpa,Spring Data,Spring Data Jpa,我有两个JPA实体,类似于以下内容: @Entity class Customer { @Id @GeneratedValue Long id @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn CustomerInformation customerInformation } @Entity class CustomerInformation { @Id @OneToOne

我有两个JPA实体,类似于以下内容:

@Entity
class Customer {
   @Id
   @GeneratedValue
   Long id

   @OneToOne(cascade = CascadeType.ALL)
   @PrimaryKeyJoinColumn
   CustomerInformation customerInformation
}


@Entity
class CustomerInformation {
   @Id
   @OneToOne
   @JoinColumn(name = "id")
   Customer customer

   String firstName
   String lastName
}
我正在使用SpringDataJPA生成我的DAO层。在这里,虽然不是很有趣:

public interface CustomerRepository extends CrudRepository<Customer, Long> {
}
但是,如果我想更新现有客户,我会遇到一个问题,即它试图插入具有空id的CustomerInformation对象。例如,这失败得很惨:

@Transactional
public void updateExistingCustomer(Long userId) {

   Customer foundCustomer = customerRepository.findOne(userId);

   if (foundCustomer.getCustomerInformation() == null) {
      CustomerInformation custInf = new CustomerInformation();
      custInf.setCustomer(foundCustomer);
      custInf.setFirstName("asdf");
      custInf.setLastName("hjkl");

      cust.setCustomerInformation(custInf);

      customerRepository.save(foundCustomer);
   }
}
此操作失败,并显示错误消息:

Hibernate: insert into CustomerInformation (firstName, lastName, id) values (?, ?, ?)
Feb 1, 2013 7:40:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 20000, SQLState: 23502
Feb 1, 2013 7:40:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Column 'ID'  cannot accept a NULL value.
我是不是误解了什么?感谢您的帮助


提前谢谢

还应将
Id
字段标记为生成的值,以便hibernate为其提供一些值:

@Entity
class Customer {
   @Id
   @GeneratedValue // !!!
   Long id

...

@Entity
class CustomerInformation {
   @Id
   @GeneratedValue // !!!
   @OneToOne
   @JoinColumn(name = "id")
   Customer customer

...

您还应将
Id
字段标记为生成的值,以便hibernate为其提供一些值:

@Entity
class Customer {
   @Id
   @GeneratedValue // !!!
   Long id

...

@Entity
class CustomerInformation {
   @Id
   @GeneratedValue // !!!
   @OneToOne
   @JoinColumn(name = "id")
   Customer customer

...

我将实体修改为如下所示:

@Entity
class CustomerInformation {

    @Id
    Long id

    @MapsId
    @OneToOne
    @JoinColumn(name = "id")
    Customer customer

    String firstName
    String lastName
}

一切顺利。据我所知,两个版本的
CustomerInformation
都会产生相同的SQL,除了第二个版本模拟实际的id,我不一定需要它。我将在另一个问题中对此进行扩展,但上面的代码解决了我的问题。

我将实体修改为如下所示:

@Entity
class CustomerInformation {

    @Id
    Long id

    @MapsId
    @OneToOne
    @JoinColumn(name = "id")
    Customer customer

    String firstName
    String lastName
}

一切顺利。据我所知,两个版本的
CustomerInformation
都会产生相同的SQL,除了第二个版本模拟实际的id,我不一定需要它。我将在另一个问题中对此进行扩展,但上面的代码解决了我的问题。

没有任何区别。我应该提到的是,
客户
的Id已经是
@GeneratedValue
,理论上,
客户信息
上的注释应该足以告诉JPA提供者(在我的例子中是Hibernate)其Id应该与相关实体的Id匹配。我弄错了吗?我将在
客户
上用更正更新问题,但我仍然有完全相同的问题。没有区别。我应该提到的是,
客户
的Id已经是
@GeneratedValue
,理论上,
客户信息
上的注释应该足以告诉JPA提供者(在我的例子中是Hibernate)其Id应该与相关实体的Id匹配。我弄错了吗?我将在
Customer
上用更正更新问题,但我仍然有完全相同的问题。