Java代碼
package Domain;
public class Person {
public int getId() {
return id;
}
public void setId(int id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this
}
private int id;
private String name;
private IdCard idCard;
}
Java代碼
package Domain;
import java
public class IdCard {
public int getId() {
return id;
}
public void setId(int id) {
this
}
public Date getUsefulLife() {
return usefulLife;
}
public void setUsefulLife(Date usefulLife) {
this
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this
}
private int id;
private Date usefulLife;
private Person person;
}
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<one
</class>
</hibernate
Xml代碼
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
<param name=
</generator>
</id>
<property name=
<one
</class>
</hibernate
Java代碼
package Dao
import org
import org
import Utils
import Domain
import Domain
public class One
public static void main(String[] args) {
Department depart = add();
queryDepart(depart
}
static Department queryDepart(int departId) {
Session s = null;
try {
s = hibernateUtil
Department depart = (Department) s
System
return depart;
} finally {
if (s != null)
s
}
}
static Department add() {
Session s = null;
Transaction tx = null;
try {
Department depart = new Department();
depart
Employee emp
emp
emp
Employee emp
emp
emp
/*Set<Employee> emps = new HashSet<Employee>();
emps
emps
depart
s = hibernateUtil
tx = s
s
s
s
System
mit();
return depart;
} finally {
if (s != null)
s
}
}
}
Java代碼
package Dao
import java
import org
import org
import Utils
import Domain
import Domain
public class One
public static void main(String[] args) {
add();
query(
}
static Person query(int id){
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
tx = s
// 一對一 查詢主對象
// Person p = (Person)s
// System
// 一對一 查詢從對象
IdCard idCard = (IdCard)s
System
mit();
//return p;
return null;
}finally{
if(s != null){
s
}
}
}
static Person add(){
Session s = null;
Transaction tx = null;
try{
s = hibernateUtil
IdCard idCard = new IdCard();
idCard
Person p = new Person();
p
p
// 用Person對象來指定IdCard的id
idCard
tx = s
s
s
mit();
return p;
}finally{
if(s != null){
s
}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28080.html