在Spring Data JPA中使用@PrimaryKeyJoinColumn批注 [英] Using @PrimaryKeyJoinColumn annotation in spring data jpa

查看:98
本文介绍了在Spring Data JPA中使用@PrimaryKeyJoinColumn批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 @PrimaryKeyJoinColumn 批注.这样做时,我收到一个错误-试图从空的一对一属性分配id .

I try to use the @PrimaryKeyJoinColumn annotation. When doing this, I get an error - attempted to assign id from null one-to-one property.

用户已保存,但地址未保存.我想为 User Address 表创建一个公共主键.我在此处找到了一个示例.

The user is saved, but the address is not saved. I want to create a common primary key for the User and Address table. I found an example here.

请告诉我我在做什么错,为什么这个例子对我不起作用?

Please, tell me what I'm doing wrong, why doesn't this example work for me?

https://github.com/mytestPercon/TestHiber

https://github.com/mytestPercon/TestHiber

User.java

@Entity
@Table(name = "user", schema = "TestKeyJoin")
public class User implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private Long id;

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

   @OneToOne(mappedBy = "user")
   @PrimaryKeyJoinColumn
   private Address activated;

   // Getter and Setter ...
}

Address.java

@Entity
@Table(name = "Address", schema = "TestKeyJoin")
public class Address implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private long id;

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

   @OneToOne
   @MapsId
   @JoinColumn(name = "id")
   private User user;

   // Getter and Setter ...

}

SaveController.java

@Controller
public class SaveController {

   @Autowired
   ServiceJpa serviceJpa;

   @GetMapping(value = "/saveUser")
   public String getJpa () {
      User user = new User();
      user.setId(1L);
      user.setName("Michael Joseph Jackson");
      serviceJpa.saveUser(user);

      Address address = new Address();
      address.setId(1L);
      address.setCity("Los Angeles");
      serviceJpa.saveActivated(address);

      return "/saveUser";
   }
}

推荐答案

尝试通过以下方式更正您的映射:

Try to correct your mapping in the following way:

@Entity
@Table(name = "user", schema = "TestKeyJoin")
public class User implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private Long id;

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

   @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
   private Address address;

   // ...
}

@Entity
@Table(name = "Address", schema = "TestKeyJoin")
public class Address implements Serializable {

   @Id
   private Long id;

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

   /* You can use @PrimaryKeyJoinColumn instead of the @MapsId here.
      See the Example 153. Derived identifier @PrimaryKeyJoinColumn
      from the hibernate documentation.
   */
   @OneToOne
   @MapsId
   @JoinColumn(name = "id")
   private User user;

   // ...
}

,然后通过以下方式保存用户:

and then save user in the following way:

@GetMapping(value = "/saveUser")
public String getJpa () {

   User user = new User();
   user.setName("Michael Joseph Jackson");

   Address address = new Address();
   address.setCity("Los Angeles");

   // make both sides of the bidirectional @OneToOne in-sync
   user.setAddress(address);
   address.setUser(user);

   serviceJpa.saveUser(user);

   return "/saveUser";
}

和一些注意事项:

  1. 无论何时形成双向关联,应用程序开发人员都必须确保双方始终保持同步.

  1. Whenever a bidirectional association is formed, the application developer must make sure both sides are in-sync at all times.

您不应单独保存实体,而应使用适当的

You should not save your entities separately, you just should use a proper cascading on your association.

您不应为当您在 Address.user 上使用 @MapsId 时,实际上意味着 Address 实体将从以下地址借用标识符一对一关联.因此,您不应为 Address.id 使用 @GeneratedValue 批注.(请参见)

When you use @MapsId on the Address.user, it actually means that the Address entity will borrow the identifier from the one-to-one association. So, you should not use @GeneratedValue annotation for the Address.id. (See this)

这篇关于在Spring Data JPA中使用@PrimaryKeyJoinColumn批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆