Java代码
package Domain;
public class Person {
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
thisidCard = idCard;
}
private int id;
private String name;
private IdCard idCard;
}
Java代码
package Domain;
import javautilDate;
public class IdCard {
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
public Date getUsefulLife() {
return usefulLife;
}
public void setUsefulLife(Date usefulLife) {
thisusefulLife = usefulLife;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
thisperson = person;
}
private int id;
private Date usefulLife;
private Person person;
}
Xml代码
<?xml version=?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
mappingdtd>
<hibernatemapping package=Domain>
<class name=Person table=person>
<id name=id>
<generator class=native />
</id>
<property name=name />
<onetoone name=idCard/>
</class>
</hibernatemapping>
Xml代码
<?xml version=?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
mappingdtd>
<hibernatemapping package=Domain>
<class name=IdCard table=id_card>
<id name=id>
<generator class=foreign>
<param name=property>person</param>
</generator>
</id>
<property name=usefulLife column=useful_Life/>
<onetoone name=person constrained=true/>
</class>
</hibernatemapping>
Java代码
package DaoTest;
import orghibernateSession;
import orghibernateTransaction;
import UtilshibernateUtil;
import DomainDepartment;
import DomainEmployee;
public class OneManyTest {
public static void main(String[] args) {
Department depart = add();
queryDepart(departgetId());
}
static Department queryDepart(int departId) {
Session s = null;
try {
s = hibernateUtilgetSession();
Department depart = (Department) sget(Departmentclass departId);
Systemoutprintln(emp size: + departgetEmps()size());
return depart;
} finally {
if (s != null)
sclose();
}
}
static Department add() {
Session s = null;
Transaction tx = null;
try {
Department depart = new Department();
departsetName(+Manager+);
Employee emp = new Employee();
empsetDepart(depart);// 对象模型建立两个对象的关联
empsetName(emp name);
Employee emp = new Employee();
empsetDepart(depart);// 对象模型建立两个对象的关联
empsetName(emp name);
/*Set<Employee> emps = new HashSet<Employee>();
empsadd(emp);
empsadd(emp);
departsetEmps(emps);*/
s = hibernateUtilgetSession();
tx = sbeginTransaction();
ssave(depart);
ssave(emp);
ssave(emp);
Systemoutprintln();
mit();
return depart;
} finally {
if (s != null)
sclose();
}
}
}
Java代码
package DaoTest;
import javautilDate;
import orghibernateSession;
import orghibernateTransaction;
import UtilshibernateUtil;
import DomainIdCard;
import DomainPerson;
public class OneOneQueryTest {
public static void main(String[] args) {
add();
query();
}
static Person query(int id){
Session s = null;
Transaction tx = null;
try{
s = hibernateUtilgetSession();
tx = sbeginTransaction();
// 一对一 查询主对象
// Person p = (Person)sget(Personclass id);
// Systemoutprintln( pgetIdCard()getUsefulLife() + pgetIdCard()getUsefulLife());
// 一对一 查询从对象
IdCard idCard = (IdCard)sget(IdCardclass id);
Systemoutprintln(idCardgetPerson()getName() + idCardgetPerson()getName());
mit();
//return p;
return null;
}finally{
if(s != null){
sclose();
}
}
}
static Person add(){
Session s = null;
Transaction tx = null;
try{
s = hibernateUtilgetSession();
IdCard idCard = new IdCard();
idCardsetUsefulLife(new Date());
Person p = new Person();
psetName(Chen Huiyi);
psetIdCard(idCard);
// 用Person对象来指定IdCard的id
idCardsetPerson(p);
tx = sbeginTransaction();
ssave(p);
ssave(idCard);
mit();
return p;
}finally{
if(s != null){
sclose();
}
}
}
}