- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果你知道更好的标题,请更改标题,因为我真的不知道如何表达问题。
我有三门课:
@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 如何将该联系人保存到数据库中?此外,您没有保留联系,因此它是暂时的。
你必须
以下是联系人实体的代码:
@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/
甲骨文: create table t7(c1 number primary key,c2 number); insert into t7 values (1,3); insert into t
谁能告诉我为什么点击模板链接生成的url是/.pk。我想了解 url 是如何工作的。这里是 Django 新手。 Traceback (most recent call last): File "
我有以下 MySQL 触发器。它因 tbl_users 的更新而被触发。如果当天已经存在记录,它将用一些信息更新 tbl_user_stats;如果当天没有记录,它将在 tbl_user_stats
已知:tX_1.name 永远不能为 NULL,长度可变,最多 45 个字符,并且必须始终是唯一的。 哪些因素会影响是否应使用代理 PK 加唯一列(架构 1)或 PK 自然键是否使用第一个表的唯一列作
我有一个包含复合 PK(code_id、user_id)的表,它已经有 100k 条记录。从复合 PK 移动到单个 PK 是否安全,如下例所示? ALTER TABLE orders DROP C
为什么这个模型没有创建 pk 并提示相关模型的 pk 的完整性? 创建 UserProfile 的新实例时,它不会创建主键。 我正在遵循一对一的说明 in this tutorial (这就是所有 @
注意:我是 JPA 开发的新手,正在快速学习试用,所以请原谅我可能遗漏的任何明显内容。 以下是相关的 JPA 实体。地址未列出,但它是一个非常简单的实体,具有一个名为 id 的 @Id。 ShipTo
我们的系统中有两个实体,如下所示: Invoice:[InvoiceID, OrgID] 是表示为 bean 的主键,并使用 @IdClass 注释与 Invoice 相关联 WorkflowStat
场景:我遇到了一些在事务中将 JPA 与 JDBC 混合的代码。 JDBC 正在对基本上是空白行的表执行 INSERT,将主键设置为 (SELECT MAX(PK) + 1) 并将 middleNam
我有一个包含 3 个表的数据库:类别、项目、关系。 categories 包含类别。商品存储在items中,relation是一个绑定(bind)表,它存储了产品id和类别id或类别。 对于给定的项目
问题 当我尝试导入 CSV 文件时,我收到 key “PRIMARY”的重复条目“x-x-xx-x-x-x” - PK 错误。 PK 本身是两个连接的 varchar。据我所知,连接永远不会重复。 我
我正在尝试使用连接从两个大表中进行选择: EXPLAIN SELECT SQL_NO_CACHE e.* FROM `table_A` e JOIN (SELECT id FROM
我有这个 post_list.html 文件:(忽略第二个 'blog:post_detail' url) {% for post in post_list %} {{ post.title }}
我需要使用 JPA 处理现有数据库表。这些表使用复合主键。外键属性与表的主键重叠。 简化示例,每个“订单”都有许多“OrderItems” Table Order
据我所知,每当我在 JPA/Hibernate 实体内的 Long 字段上使用 @Id 和 @GeneratedValue 时,我实际上是在使用代理键,我认为这是定义主键的一种非常好的方式,考虑到我在
所以我有这段代码: request = self.factory.get(reverse('portal-edit-automation', args=(self.rule.id,))) respon
当在MySQL中使用InnoDB存储引擎时,如果在创建表时不指定PRIMARY KEY,则使用隐藏唯一索引作为聚集索引。由于数据字典上的互斥锁,我了解到这些隐藏索引可能会导致争用。 我的问题是 - 如
我正在尝试删除 table1 中与 table2 中具有匹配 PK 的所有行。尽管我的 WHERE 子句使用了键,但我收到错误 1175。我熟悉切换安全模式,但这不应该成为问题,因为我的 WHERE
我正在尝试更新具有外键字段的 View 的记录,因此出现错误,因为我尝试更新没有外键字段的另一个模型并且效果很好。 还有其他类似的问题,但就我而言,我通过了 pk。 urls.py urlpatte
Beeferman 的 PK 和 WindowDIFF 的 Python NLTK 实现从两者的 python segeval 实现中得到完全不同的结果。 使用相同的参数。 hyp: 01001000
我是一名优秀的程序员,十分优秀!