Hibernate多表查询实践总结(转)
实在无聊,前段时间做了个对单表的ZCGC(增删改查),练了练手,虽然做过,但还是碰到了不少问题,让自己对struts+hibernate模式开发更加熟练了点。今天,我做多表的复习的时候,又碰到问题了,现在将关键点记录下来,以免日后再有相同问题出现,以便查阅。
我做的两张表:一张department表,一张employee表,建表语句如下:
droptableemployee
createtableEMPLOYEE
(
IDNUMBER(10)notnull,
parentIDnumber(10),
NAMEVARCHAR2(20),
AGENUMBER(3),
PASSWARDVARCHAR2(12),
CSRQDATE,
PICTUREvarchar(12),
primarykey(id),
foreignkey(parentID)referencesdepartment(id)
)
droptabledepartment
createtabledepartment
(
idnumber(10)notnull,
dep_idvarchar2(20),
dep_mcvarchar2(20),
primarykey(id)
)
其中,employee表中的parentID是department表的外键,department和employee表是一对多的关系,反过来是多对一的关系。怎么叫都可以。
Department.hbm.xml代码如下:
<hibernate-mapping>
<classname="com.dudeng.employee.hibernate.vo.Department"
table="DEPARTMENT"schema="DUDENG">
<idname="id"type="java.lang.Long">
<columnname="ID"precision="10"scale="0"/>
<generatorclass="sequence">
<paramname="sequence">seq_department</param>
</generator>
</id>
<propertyname="dep_id"type="java.lang.String">
<columnname="DEP_ID"length="20"not-null="false"/>
</property>
<propertyname="dep_mc"type="java.lang.String">
<columnname="DEP_MC"length="20"not-null="false"/>
</property>
<setname="employees"inverse="true">
<key>
<columnname="parentID"precision="5"scale="0"
not-null="true"/>
</key>
<one-to-many
class="com.dudeng.employee.hibernate.vo.Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml的代码如下:
<hibernate-mapping>
<classname="com.dudeng.employee.hibernate.vo.Employee"
table="EMPLOYEE"schema="DUDENG">
<idname="id"type="java.lang.Long">
<columnname="ID"precision="10"scale="0"/>
<generatorclass="sequence">
<paramname="sequence">seq_employee</param>
</generator>
</id>
<many-to-onename="department"class="com.dudeng.employee.hibernate.vo.Department">
<columnname="parentID"precision="5"scale="0"not-null="true"/>
</many-to-one>
<propertyname="name"type="java.lang.String">
<columnname="NAME"length="20"not-null="false"/>
</property>
<propertyname="age"type="java.lang.Long">
<columnname="AGE"precision="3"scale="0"not-null="false"/>
</property>
<propertyname="passward"type="java.lang.String">
<columnname="PASSWARD"length="12"not-null="false"/>
</property>
<propertyname="csrq"type="java.util.Date">
<columnname="CSRQ"length="7"not-null="false"/>
</property>
<propertyname="picture"type="java.lang.String">
<columnname="PICTURE"length="20"not-null="false"/>
</property>
</class>
</hibernate-mapping>
查询结果集合:
Listlist=null;
StringQueryStr="fromEmployeeemp,Departmentdepwhereemp.department=dep.id";
EmployeeBizempbiz=EmployeeBiz.getInstance();
list=empbiz.find(QueryStr);
for(inti=0;i<list.size();i++)
{
Object[]obj=(Object[])list.get(i);
for(intj=0;j<obj.length;j++)
{
if(obj[j]instanceofEmployee)
{
Employeeemp=(Employee)obj[j];
System.out.print(emp.getName());
}
elseif(obj[j]instanceofDepartment)
{
Departmentdep=(Department)obj[j];
System.out.print(dep.getDep_mc());
}
}
System.out.println();
}
以上特别注意的地方:emp.department不能写成emp.parentID,我上网查了半天才找出原因的。开始写成emp.parentID了,后来改成emp.department程序运行正常了。
另外种方法
写个DocumentDetail对象,然后获得查询结果集合
selectnewcom.cetc.bean.util.vo.DocumentDetail(da.contentid,dd.id,dd.filename,dd.filesource,dd.filesize,dd.mimetype,dd.author,dd.filepath)fromDocumentDetaildd,ContentAttachmentdawheredd.id=da.documentid
构造函数一定要写好
publicDocumentDetail(Stringcontentid,Stringdocumentid,StringfileName,StringfileSource,LongfileSize,StringmimeType,Stringauthor,StringfilePath){
this.contentid=contentid;
this.documentid=documentid;
this.fileName=fileName;
this.fileSource=fileSource;
this.fileSize=fileSize;
this.mimeType=mimeType;
this.author=author;
this.filePath=filePath;
}