- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了共享 1<>1 关系的主键,我尝试使用 JPA @MapsId 注释但我没有成功。
这是我的 sql 表生成:
CREATE TABLE `myschema`.`table2` (
`id` INT NOT NULL AUTO_INCREMENT,
`coltable2` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `myschema`.`table1` (
`id` INT NOT NULL,
`coltable1` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk`
FOREIGN KEY (`id`)
REFERENCES `myschema`.`table2` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE);
@Entity
@Table(name="table1")
public class Table1 {
@Id
private int id;
@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="id")
@MapsId(value="id")
private Table2 table2;
....
}
@Entity
@Table(name="table2")
public class Table2 {
@Id @GeneratedValue
private int id;
@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "table2")
private Table1 table1;
...
}
Table1 table1 = new Table1();
table1.setColTable1("table1");
Table2 table2 = new Table2();
table2.setColTable2("table2");
table1.setTable2(table2);
table2.setTable1(table1);
dao.save(table1);
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
最佳答案
我不确定 @MapsId
是你想要的。@PrimaryKeyJoinColumn
可能是你需要的:
and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity.
关于hibernate - 使用 JPA @MapsId 注释共享主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20070535/
所以,我有 A 类和 B 类。 他们使用以下配置共享主键: 在 A 类中,我小时候引用了 B 类 @OneToOne(fetch = FetchType.EAGER, cascade = Cascad
尽管遵循了网上的所有示例,但我还是无法让@MapsId 正常工作。 这是我的设置: pom.xml: org.hibernate hibernate-entitymanager
我正在努力将 Spring Boot 1.5.21 项目 (Java 8u221) 升级到 Spring Boot 2.1.9 (Java 11.0.2-open)。在这两种情况下,我们都使用带有 s
为了共享 1<>1 关系的主键,我尝试使用 JPA @MapsId 注释但我没有成功。 这是我的 sql 表生成: CREATE TABLE `myschema`.`table2` ( `id` IN
我的 @MapsId 注释和 @EmbeddedId 存在问题。在 Hibernate 中运行代码时,我得到: Caused by: org.hibernate.PropertyAccessExcep
我的实体 DirigeantsEntreprise 和 Fournisseur 之间有 @OneToOne 关系,如下所示: @Entity @Table(name = "dirigeants_ent
我想了解 @MapsId 注释在 Hibernate 中有什么不同。我已经阅读了 Hibernate 文档,但由于我是 Hibernate 的新手,我仍然对那里给出的解释感到困惑。 document是
我想使用 Hibernate 5.3.10 和 JPA 映射一个 @OneToOne 关联。 我知道当不使用字节码增强时,@OneToOne 关联的父端不能延迟加载。 在这种情况下,我只想映射客户端并
我在 spring boot 2.1.1.RELEASE 上工作,hibernate 5.3.7.FINAL 规则是,用户可以没有电话(phone 在 user 中可以为空)但是,电话不能没有用户(u
我阅读了一些有关如何使用 @MapsId 映射 @OneToOne 关系的最佳实践 Like this 我想知道,如果实体是“共享”的,它会起作用吗? 例如... Employee has Addre
我的实体是: @Entity public class ReportSnapshot extends LightEntity { @Id @GeneratedValue private l
根据 Hibernate documentation , @MapsId 注解的解释如下: In the embedded id object, the association is represen
在以下示例中(来自 Vlad Mihalcea's post on "The best way to map a @OneToOne relationship with JPA and Hiberna
我有两个实体: @Entity @Table(name = "child") public class MappedChild { @Id @Column(name = "parent
我有 2 个实体,比如 Car 和 Engine(只是示例名称,但逻辑是相同的)。 @Entity public class Car { @Id private Long id;
我有 2 个名为 Post 和 PostDetails 的实体类。两者都使用 MapsId 并共享主键以 OneToOne 关系进行映射,如下所示。 Post.java @Entity @Table(
在我看来,以下两种映射方式实际上没有区别。以下是基于 @MapsId javadoc 的示例: // parent entity has simple primary key @Entity publ
我正在尝试使用多个映射属性。但是当我尝试运行代码时,代码会生成异常,此示例基于 Pro JPA 2 第 10 章一书。有以下代码: DeptID.java public class DeptId im
我正在尝试对位于 Hbase 中的两个表进行映射连接。我的目的是在hashmap中保留小表的记录并与大表进行比较,一旦匹配,再次将记录写入hbase中的表中。我使用 Mapper 和 Reducer
以下代码存在于 PROD 中并且每天运行,我正在尝试对其进行优化。我看到设置 hive.auto.convert.join=FALSE;正在让它执行一个 Reduce side join,它运行 2.
我是一名优秀的程序员,十分优秀!