gpt4 book ai didi

java - 使用 JPA 持久保存 PK 对象(ManyToMany)

转载 作者:行者123 更新时间:2023-12-01 15:45:13 25 4
gpt4 key购买 nike

如果你知道更好的标题,请更改标题,因为我真的不知道如何表达问题。

我有三门课:

@Entity
public class User {

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

@NotNull
@Size(min = 1, max = 45)
@Column(name = "name")
private String name;

@JoinTable(name = "usuer_has_contact", joinColumns = {
@JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "contact_id", referencedColumnName = "id")})
@ManyToMany(cascade = CascadeType.ALL)
private List<Contato> contactList;

//Getters and Setters

}
<小时/>
DB Table:
Table Name: User
Columns: id (int pk), name (varchar(45) not null).
<小时/>
@Entity
private class Contact {

@EmbeddedId
protected UserHasContact userHasContact;

@NotNull
@Size(min = 1, max = 45)
@Column(name = "value")
private String value;

@ManyToMany(mappedBy = "contactList")
private List<User> userList;

//Getters and Setters

}
<小时/>
DB Table:
Table Name: Contact
Columns: id (int pk), value (varchar(45) not null).
<小时/>
@Embeddable
private class UserHasContact {

@NotNull
@Column(name = "id")
private Integer id;

//Getters and Setters

}
<小时/>
DB Table:
Table Name: UserHasContact
Columns: userId (int pk), contactId (int pk).
<小时/>

我想做的是,当我保留用户本身时保留所有内容。例如:

User user = new User();
user.setContactList(new ArrayList<Contact>());

Contact contact = new Contact();
contact.setValue("555-5555");

user.getContactList().add(contact);

// Here I'd call another class, passing User so it would only do a
// entityManager.persist(user), and it would persist it all and
// take care of all tables for me. What I don't want to do is to
// fill up the values myself, I want let JPA do it for me.

我希望在执行此操作后保存,但它说 contactId 为 null 并且不能为 null。我能做什么?

最佳答案

为什么要创建一个可嵌入的 UserHasContact 类来仅存储单个 Integer?你让事情变得比必要的更加困难。只需使用整数 ID 作为联系人主键即可。然而,这不是您的问题的原因。

您正在尝试保留一个在其联系人列表中包含联系人的用户。您的联系人 ID 不是自动生成的,并且您没有为此联系人 ID 分配任何 ID。 JPA 如何将该联系人保存到数据库中?此外,您没有保留联系,因此它是暂时的。

你必须

  • 为联系人分配一个 ID,或对其 ID 进行注释以便自动生成
  • 保留联系人和用户

以下是联系人实体的代码:

@Entity
private class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // this makes the ID auto-generated
@Column(name = "id")
private Integer id;

@NotNull
@Size(min = 1, max = 45)
@Column(name = "value")
private String value;

@ManyToMany(mappedBy = "contactList")
private List<User> userList;

//Getters and Setters
}

在创建用户和联系人的代码中:

User user = new User();
user.setContactList(new ArrayList<Contact>());

entityManager.persist(user);

Contact contact = new Contact();
contact.setValue("555-5555");

entityManager.persist(contact);

user.getContactList().add(contact);
// you should also make sure that the object graph is consistent, so
// the following line should lso be added, (though not strictly necessary)
contact.getUserList().add(user);

关于java - 使用 JPA 持久保存 PK 对象(ManyToMany),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7176718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com