.XXX.C" table="C" >
<id name="id" type="long">
<column name="ID"/>
<generator class="assigned">
</generator>
</id>
<property name="a_id" type="long">
<column name="A_ID">
</column>
</property>
</class>
</hibernate-mapping>
在Hibernate中直接操作JDBC接口(3)
时间:2011-08-11 IBM 唐清原
对应 的 Hibernate 领域实体类代码示例如下:
清单 2. hibernate 实体类示例
A.java:
public class A implements java.io.Serializable,Comparable {
private long id;
private Set children_b = new HashSet<B> ();
private Set children_c = new HashSet<C>();
public A (long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set getChildern_b() {
return children_b;
}
public void setChildren_b (Set children_b) {
this.children_b = children_b;
}
public Set getChildern_c() {
return children_c;
}
public void setChildren_c (Set children_c) {
this.children_c = children_c;
}
public int compareTo(Object other) {
A otherSubject = (A)other;
long curAmount=this.getChildren_b().size()+this.getChildren_c ().size();
long otherAmount =otherSubject.getChildren_b().size()
+ otherSubject.getChildren_c().size();
if(curAmount<otherAmount)
{
return -1;
}
else if (curAmount>otherAmount)
{
return 1;
}
else
{
return 0;
}
}
}
B.java:
public class B implements java.io.Serializable,Comparable {
private long id;
private long a_id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getA_id() {
return a_id;
}
public void setA_id(long a_id) {
this.a_id = a_id;
}
public B(long id) {
this.id=id;
}
}
C.java:
public class C implements java.io.Serializable,Comparable {
private long id;
private long a_id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getA_id() {
return a_id;
}
public void setA_id(long a_id) {
this.a_id = a_id;
}
public C(long id) {
this.id=id;
}
}
在Hibernate中直接操作JDBC接口(4)
时间:2011-08-11 IBM 唐清原
假设现在要统 计 A 表中从属的 B 表和 C 表记录之和最高的 top10 的 A 表记录,在 Hibernate 框架下,由于取 A 表对应的数据库记录时,已关联取出了对应的 B、C |