如何从我的实体获取EmbeddedId

时间:2014-12-19 12:36:05

标签: java oracle jpa

我对我的桌子/实体有疑问。

我的SQL-Table如下所示:

表1:

IdTable1(pk)
属性...


表2:

IdTable2(pk)
属性...


表3:

IdTable1(pk)
IdTable2(pk)
日期


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

表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;
}
}

Table3Id

@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-Object获取Table3属性:?

Query query = entityManager.createQuery("select t from Table3 as t")

修改

它像this,但我的EntitiyId中有实体(f.e.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)]

1 个答案:

答案 0 :(得分:0)

Query query = entityManager.createQuery("select t.table3Id.table1, t.table3Id.table2 from Table3 as t")

这是你想要的吗?