2013-11-26 74 views
0
StuPersonal table-sno,stu_name,gender,FRID; 
StuDegree table-sno,degree,dtype,regno,col_name,stP(FRID). 

以上是我的表格定義(OnetoOne)的關係。 我的POJO類是如何使用條件從多個表中獲取記錄?

@Entity 
public class StuPersonal { 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="ID") 
private int sno; 

@Column(name="NAME") 
private String stu_name; 
private int name_verify=0; 

@Column(name="GENDER") 
private String gender; 

@OneToOne(cascade={CascadeType.ALL}) 
@JoinColumn(name="FRID") 
private List<StuDegree> StuDegree; 
@Id 
private String FRID; 

@Entity 
public class StuDegree { 
@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="sNO")   
private int sno; 

@Column(name="DEGREE") 
private String degree; 

@Column(name="DEGREE_TYPE") 
private String dtype; 
@Column(name="COLLEGE_NAME") 
private String col_name; 
@OneToOne 
@JoinColumn(name="FRID") 
private StuPersonal stP; 

和我的主類是

Criteria criteria= s. createCriteria(StuDegree.class); 
criteria.add(Restrictions.eq("col_name","PSYEC")); 
List<StuDegree> colname =(List<StuDegree>) criteria.list(); 

我上面線擺脫Studegree表記錄中使用;

for (Iterator iterator = colname.iterator(); iterator.hasNext();) 
{ 
    StuDegree stdeg= (StuDegree) iterator.next(); 
    { 
    Criteria criteria1= s.createCriteria(StuPersonal.class); 
    criteria.add(Restrictions.eq("stdeg.getFRID()","StuPersonal.getFRID()")); 
    List<StuPersonal> stuper=(List<StuPersonal>) criteria1.list(); 
    System.out.println(stuper.size()); 
    } 
} 

我用上面的代碼從Studegree表中使用FRID從StuPersonal表中取記錄,是不是?它不工作... PI建議...

+0

你的代碼沒有意義。您不能在List類型的屬性上使用OneToOne。列表包含許多元素。所以這顯然是OneToMany。 –

+0

謝謝JB Nizet .... –

回答

1

像下面那樣改變你的關係。

變化

@OneToOne(cascade={CascadeType.ALL}) 
@JoinColumn(name="FRID") 
private List<StuDegree> StuDegree; 

@ManyToOne(cascade={CascadeType.ALL}) 
@JoinColumn(name="FRID") 
private List<StuDegree> StuDegree; 

,你的標準是這樣的。它將基於StuDegree sno獲取StuPersonal。

Integer snos[] = {1,2,3}; 
Criteria criteria = s.createCriteria(StuPersonal.class, "sp"); 
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 
criteria.createAlias("sp.StuDegree","sd"); 
criteria.add(Restrictions.in("sd.sno", snos)); 
List<StuPersonal> stuper= criteria1.list(); 
相關問題