gpt4 book ai didi

java - 如何从我的实体获取 EmbeddedId

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:19 24 4
gpt4 key购买 nike

我对我的表格/实体有疑问。

我的 SQL 表如下所示:

表 1:

IdTable1(pk)
属性...

<小时/>

表 2:

IdTable2(pk)
属性...

<小时/>

表3:

IdTable1(pk)
IdTable2(pk)
日期

<小时/>

好的..现在我在 Java JPA 中有以下实体:

表1

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Table1 {

private Long table1Id

protected Table1() {
super();
}

protected Table1(Long table1Id) {
super();
this.table1Id = table1Id;
}

public boolean equals(Object obj) {
if (obj instanceof Table1) {
Table1 fach = (Table1) obj;
return this.getTable1Id().equals(fach.getTable1Id());
} else {
return false;
}
}

@Id
@Column(name = "IDTABLE1")
public Long getTable1Id() {
return table1Id;
}

public void setTable1Id(Long table1Id) {
this.table1Id = table1Id;
}
}

表2

@Entity
public class Table2 {

private Long table2Id

protected Table2() {
super();
}

protected Table2(Long table2Id) {
super();
this.table2Id = table2Id;
}

public boolean equals(Object obj) {
if (obj instanceof Table2) {
Table2 fach = (Table2) obj;
return this.getTable2Id().equals(fach.getTable2Id());
} else {
return false;
}
}

@Id
@Column(name = "IDTABLE2")
public Long getTable2Id() {
return table12d;
}

public void setTable2Id(Long table2Id) {
this.table2Id = table2Id;
}
}

表3

@Entity
public class Table3 implements Serializable {

private static final long serialVersionUID = 1L;

private Table3Id table3Id;
private Date date;

public Table3() {
super();
}

public Table3(Date date, Table1 table1, Table2 table2) {
super();
this.date = date;
this.table3Id = new Table3Id(table1, table2);
}

@EmbeddedId
public Table3Id getTable3Id() {
return table3Id;
}

public void setTable3Id(Table3Id table3Id) {
this.table3Id = table3Id;
}

@Column(name = "date", nullable = true)
public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}

表3Id

@Embeddable
public class Table3Id implements Serializable {

private static final long serialVersionUID = 1L;

private Table1 table1;
private Table2 table2;

public Table3Id() {
}

public Table3Id(Table1 table1, Table2 table2) {
this.table1 = table1;
this.table2 = table2;
}

public Table1 getTable1() {
return table1;
}

public void setTable1(Table1 table1) {
this.table1 = table1;
}

public Table2 getTable2() {
return table2;
}

public void setTable2(Table2 table2) {
this.table2 = table2;
}
}

好的...我现在如何从我的 Dao 对象中获取 Table3 属性:?

Query query = entityManager.createQuery("select t from Table3 as t")
<小时/>

编辑

就像 this ,但我的 EntitiyId 中有实体(例如 Table1 table1;Table2 table2),而不是“long table1id;long table2id”。

<小时/>

编辑

我认为 @Niemand 的查询是正确的,但我得到以下异常:

Caused by: org.hibernate.MappingException: Could not determine type for: Table1, at table: Table3, for columns: [org.hibernate.mapping.Column(table1)]

最佳答案

Query query =entityManager.createQuery("从Table3中选择t.table3Id.table1, t.table3Id.table2作为t")

这是你想要的吗?

关于java - 如何从我的实体获取 EmbeddedId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27566339/

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