方式一
例如實體類People中
/*實體類
public class People implements Serializable
{
private static final long serialVersionUID =
private String id;
private String name;
private int age;
public People()
{
}
public String getId()
{
return id;
}
public void setId(String id)
{
this
}
public String getName()
{
return name;
}
public void setName(String name)
{
this
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this
}
@Override
public int hashCode()
{
final int prime =
int result =
result = prime * result + ((id == null)
?
result = prime * result + ((name == null) ?
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj
return false;
People other = (People) obj;
if (id == null)
{
if (other
return false;
}
else if (!id
return false;
if (name == null)
{
if (other
return false;
}
else if (!name
return false;
return true;
} } People
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<!
<composite
<!
<key
<key
</composite
<property name=
</class>
</hibernate
Hibernate中使用復合主鍵時需要注意一些規則
Object load(Class theClass
Object get(Class theClass
當我們查找復合主鍵類的對象時
保存測試
public class Client {
public static void main(String[] args)
{
Session session = HibernateUtil
Transaction tx = null;
try
{
tx = session
People people = new People()
/*主鍵值由我們自己維護*/
people
people
people
session
mit()
}
catch (Exception e)
{
if(tx != null)
{
tx
}
e
}
finally
{
session
}
}
}
看看數據庫
數據被正確的插入到數據庫中了
讀取數據測試
public class Client
{
public static void main(String[] args)
{
Session session = HibernateUtil
Transaction tx = null;
try
{
tx = session
/*查詢復合主鍵對象
People peoplePrimaryKey = new People()
peoplePrimaryKey
peoplePrimaryKey
/*然後將構建的主鍵值傳入get方法中獲取對應的People對象*/
People people = (People)session
System
mit()
}
catch (Exception e)
{
if(tx != null)
{
tx
}
e
}
finally
{
session
}
}
}
控制台輸出
people age is:
方式二
主鍵類
/*必須實現Serializable接口*/
public class PeoplePrimaryKey implements Serializable
{
private static final long serialVersionUID =
/*復合主鍵值*/
private String id;
private String name;
public PeoplePrimaryKey()
{
}
/*復合主鍵值的get和set方法*/
public String getId()
{
return id;
}
public void setId(String id)
{
this
}
public String getName()
{
return name;
}
public void setName(String name)
{
this
}
@Override
public int hashCode()
{
final int prime =
int result =
result = prime * result + ((id == null) ?
result = prime * result + ((name == null) ?
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj
return false;
PeoplePrimaryKey other = (PeoplePrimaryKey) obj;
if (id == null)
{
if (other
return false;
}
else if (!id
return false;
if (name == null)
{
if (other
return false;
}
else if (!name
return false;
return true;
}
}
實體類
public class People {
/*持有主鍵類的一個引用
private PeoplePrimaryKey peoplePrimaryKey;
private int age;
public People()
{
}
public PeoplePrimaryKey getPeoplePrimaryKey()
{
return peoplePrimaryKey;
}
public void setPeoplePrimaryKey(PeoplePrimaryKey peoplePrimaryKey)
{
this
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this
}
}
People
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<!
<!
class
<composite
<!
<key
<key
</composite
<property name=
</class> </hibernate
場景測試與方式一大同小異這裡不再舉例了
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28901.html