2011-11-14 173 views
1

我有复合主键的表:休眠:使用实体与复合键

以从其他问题表中例如SO

复合主键类:

@Embeddable 
public class TimePK implements Serializable {  
protected Integer levelStation;  
protected Integer confPathID; 
protected Integer col1; 
protected Integer col2;  

public TimePK() {}  

public TimePK(Integer levelStation, Integer confPathID, Integer col1, Integer col2) {   
this.levelStation = levelStation;   
this.confPathID = confPathID; 
this.col1 = col1; 
this.col2 = col2; 

}  

// equals, hashCode 
} 

和实体:

在persistent.xml作为
@Entity 
class Time implements Serializable {  

@EmbeddedId  
private TimePK timePK;  

private String src;  
private String dst;  
private Integer distance;  
private Integer price;  
//... 
} 

将有两个条目:

com.somepackage.Time com.somepackage.TimePK

问:

如何在查询中使用上述类? 例如找到confPathId,col2,其中levelstation是10,col1是20 - 对于这个需求,什么是hibernate查询?

在查询中使用“from TimePK T”给出“TimePK未映射”错误!

回答

2

看看JPA specification

部分2.4.1.3 Examples of Derived Identities定义了很多的例子。

+0

“从时间T开始,其中T.timePK.levelStation = 10和T.timePK.col1 = 20”正确? – Nik

+0

它应该做的工作;你测试过了吗? –

+0

是的..它的作品..谢谢!测试它! – Nik

0

你的TimePK不是一个真正的实体(映射到db中的表),它是一个包含你的Time对象的PK的虚拟对象。 您应该找到时间对象,而不是TimePK。