【发布时间】:2021-07-03 16:50:33
【问题描述】:
@Entity
@Table(name = "TableA")
public class TableAEntity{
@Id
@Column(name = "RUL_ID"
private Integer rulId;
@Column(name = "COMMENT"
private Integer comment;
@OneToOne
@JoinColumn(name = "RUL_ID" referencedColumnName ="PRNT_ID", insertable=false, updatable=false)
private TableBEntity tableB;
//GETTERS AND SETTERS
}
@Entity
@Table(name = "TableB")
public class TableBEntity{
@Id
@Column(name = "ADD_ID"
private Integer addID;
@Column(name = "PRNT_ID"
private Integer prntId;
//GETTERS AND SETTERS
}
有 2 个数据库表。
- 主键为 rulId 的 TableA。
- 主键为 addID 的 TableB。
我必须使用 JPA 原生查询来实现自定义的 JOIN 查询。
Java 代码是:
StringBuilder querySql = "select a.rulId, b.prntId from TableA a JOIN TableB b ON a.rulID = b.prntId"
Query tabQuery = entityManager.createNativeQuery(querySql.toString, TableAEntity.class)
List<TableAEntity> entityList = tabQuery.getResultList();
当它们没有与任何键(pk/fk)链接时,如何建立这种 OneToOne(TableA:TableB) 关系。 我无法将 ResultList 映射到我的实体类。TableA“rulId”的主键始终链接到 TableB“addId”的 PrimaryKey,其中我想将其关联到“prntId”。
谁能帮忙解决这个问题。
【问题讨论】:
-
TableBEntity中的OneToOne关系是否正确?还是和TableAEntity有关系? -
道歉... 已更新相同。 TableA 中存在 OneToOne 关系。
标签: oracle jpa mapping entity nativequery