package
y.model;
import
java.io.Serializable;
import
java.util.Date;
/**
* 多表联合查询测试实体类
* @param classname
* @param info
* @param project
* @param classChineseName
*/
public
class
Test
implements
Serializable {
//无参数构造方法
public
Test()
{
super
();
}
//编号
private
String CId;
public
String getCId(){
return
this
.CId;
}
public
void
setCId(String CId){
this
.CId=CId;
}
//姓名
private
String CName;
public
String getCName(){
return
this
.CName;
}
public
void
setCName(String CName){
this
.CName=CName;
}
//编号
private
String DId;
public
String getDId(){
return
this
.DId;
}
public
void
setDId(String DId){
this
.DId=DId;
}
//姓名
private
String DName;
public
String getDName(){
return
this
.DName;
}
public
void
setDName(String DName){
this
.DName=DName;
}
//含参数构造方法
public
Test(String cId,String cName,String dId,String dName)
{
super
();
this
.CId=cId;
this
.CName=cName;
this
.DId=dId;
this
.DName=dName;
}
}
package
y.model;
import
java.io.Serializable;
import
java.util.Date;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.Table;
import
org.hibernate.annotations.GenericGenerator;
@Entity
@Table
(name =
"test1"
)
public
class
Test1
implements
Serializable {
@Id
@GenericGenerator
(name =
"generator"
, strategy =
"uuid.hex"
)
@GeneratedValue
(generator =
"generator"
)
@Column
(name =
"C_Id"
)
private
String CId;
@Column
(name =
"C_Name"
)
private
String CName;
@Column
(name =
"C_Pwd"
)
private
String CPwd;
@Column
(name =
"C_Sex"
)
private
String CSex;
@Column
(name =
"C_Ago"
)
private
String CAgo;
public
String getCId(){
return
this
.CId;
}
public
void
setCId(String CId){
this
.CId=CId;
}
public
String getCName(){
return
this
.CName;
}
public
void
setCName(String CName){
this
.CName=CName;
}
public
String getCPwd(){
return
this
.CPwd;
}
public
void
setCPwd(String CPwd){
this
.CPwd=CPwd;
}
public
String getCSex(){
return
this
.CSex;
}
public
void
setCSex(String CSex){
this
.CSex=CSex;
}
public
String getCAgo(){
return
this
.CAgo;
}
public
void
setCAgo(String CAgo){
this
.CAgo=CAgo;
}
}
package
y.model;
import
java.io.Serializable;
import
java.util.Date;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.Table;
import
org.hibernate.annotations.GenericGenerator;
@Entity
@Table
(name =
"test2"
)
public
class
Test2
implements
Serializable {
@Id
@GenericGenerator
(name =
"generator"
, strategy =
"uuid.hex"
)
@GeneratedValue
(generator =
"generator"
)
@Column
(name =
"D_Id"
)
private
String DId;
@Column
(name =
"D_Name"
)
private
String DName;
@Column
(name =
"D_Pwd"
)
private
String DPwd;
@Column
(name =
"D_Age"
)
private
Integer DAge;
@Column
(name =
"D_Addr"
)
private
String DAddr;
@Column
(name =
"D_Sex"
)
private
String DSex;
public
String getDId(){
return
this
.DId;
}
public
void
setDId(String DId){
this
.DId=DId;
}
public
String getDName(){
return
this
.DName;
}
public
void
setDName(String DName){
this
.DName=DName;
}
public
String getDPwd(){
return
this
.DPwd;
}
public
void
setDPwd(String DPwd){
this
.DPwd=DPwd;
}
public
Integer getDAge(){
return
this
.DAge;
}
public
void
setDAge(Integer DAge){
this
.DAge=DAge;
}
public
String getDAddr(){
return
this
.DAddr;
}
public
void
setDAddr(String DAddr){
this
.DAddr=DAddr;
}
public
String getDSex(){
return
this
.DSex;
}
public
void
setDSex(String DSex){
this
.DSex=DSex;
}
}
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Date;
import
java.util.List;
import
java.util.Vector;
import
org.hibernate.Query;
import
org.hibernate.Session;
import
org.hibernate.Transaction;
import
y.model.Test;
import
y.model.Test1;
import
y.model.Test2;
import
com.targ.track.financeManager.buildProjectAccount.domain.BuildProjectAccount;
import
com.targ.track.financeManager.buildProjectAccount.domain.BuildProjectAccountDetail;
import
com.targ.track.startAppManager.domain.StartApplication;
public
class
MyJunitTest {
@org
.junit.Test
public
void
testDDL(){
//插入数据
HibernateTest.doInHibernateSession(
new
InHibernateSession(){
@Override
public
Object doInHibernateSession(Session session) {
Test1 test1 =
new
Test1();
test1.setCAgo(
"CAgo"
);
test1.setCName(
"CName"
);
test1.setCPwd(
"CPwd"
);
test1.setCSex(
"CSex"
);
Test2 test2 =
new
Test2();
test2.setDAddr(
"DAddr"
);
test2.setDAge(
123
);
test2.setDName(
"DName"
);
test2.setDPwd(
"DPwd"
);
test2.setDSex(
"DSex"
);
session.save(test1);
session.save(test2);
return
null
;
}
});
//查询
List all = (List) HibernateTest.doInHibernateSession(
new
InHibernateSession(){
public
Object doInHibernateSession(Session session) {
String sql=
"select new y.model.Test(c.CId,c.CName,d.DId,d.DName) from Test1 c,Test2 d where c.CId=d.DId"
;
Query query = session.createQuery(sql);
return
query.list();
}
});
for
(Test test : all){
System.out.println(test);
}
}
public
static
class
HibernateTest {
public
static
Object doInHibernateSession(InHibernateSession doInHibernateSession){
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
try
{
Object object = doInHibernateSession.doInHibernateSession(session);
transaction.commit();
return
object;
}
catch
(RuntimeException e){
try
{
System.err.println(
"rollback"
);
transaction.rollback();
}
catch
(RuntimeException ex){
System.err.println(
"回滚失败!"
);
}
throw
e;
}
finally
{
//HibernateSessionFactory.closeSession();
}
}
}
public
interface
InHibernateSession {
public
Object doInHibernateSession(Session session);
}
}