基本O/R映射(二)--Hibernate快速参考
composite-id
<composite-id
name="propertyName"
type="typename" column="column_name"/>
<key-many-to-one name="propertyName column="column_name"/>
......
</composite-id>
For a table with a composite key, you may map multiple properties of the class as identifier properties. The <composite-id> element accepts <key-property> property mappings and <key-many-to-one> mappings as child elements.
<composite-id>
<key-property name="medicareNumber"/>
<key-property name="dependent"/>
</composite-id>
Your persistent class must override equals() and hashCode() to implement composite identifier equality. It must also implements Serializable.
例如:( 孙卫琴 <精通Hibernate:Java对象持久化技术详解> 137页
一、定义复合属性
Customer表没有定义ID代理主键,而是以NAME字段和COMPANY_ID作为复合主键,那么相应地,在Customer类中也不必定义id属性,而是以name和company_id作为OID,映射代码如下:
<composite-id>
<key-property name="name" column="NAME" type-"string"/>
<key-property name="companyId" column-"COMPANY_ID" type="long"/>
</composite-id>
<version name-"version" column-"VERSIONI" unsaved-value="0"/>
创建:
Customer customer=new Customer();
customer.setName("Tom");
customer.setCompanyId(11);
session.saveOrUpdate(customer);//由于customer对象的version属性为0,因此调用save()方法
加载:
Customer customer-=ew Customer();
customer.setName("TOm");
customer.setCompanyId(11);
session.load(Customer.class,customer);
注:需要实现java.io.Serializable接口
二、定义主键类
映射复合主键的另一种方法是先定义主键类,以包含name,companyId字段,实现Serializable接口,添加映射代码如下:
<class name="mypack.Customer" table="CUSTOMERS">
<composite-id name="custoerId" column="NAME" type=String/>
<key-property name-"companyId" column="COMPANY_ID" type="long"/>
</composite-id>
<version name="version" column="VERSION" unsaved-value="0"/>
</class>
创建:
CustomerId customerId=new CustomerId("Tom",11);
Customer customer=new Customer();
customer.setCustomerId(customerId);
session.saveOrUpdate(customer);
加载:
CustomerId customerId=new CustomerId("Tom",11);
Customer customer=(Customer)session.load(Customer.class,customerId);
三、外键参照
当COMPANY_ID还作为外键参照COMPANY表,则在Customer类中:
private CustomerId customerId;
private Company company;
映射代码:
<class name="mypack.Customer" table="CUSTOMERS">
<composite-id name=customerId" class ="mypack.CustomerId">
<key-property name="name" column="NAME" type="string"/>
<key-property name="companyId" column="COMPANY_ID" type="long"/>
</composite-id>
<version name="version" column="VERSION" unsaved-value="0"/>
<many-to-one name="company" column="COMPANY_ID"
insert="false" update="fase"/>
<!--保存或更新Customer对象时,会忽略company属性-->
</class>
也可用<key-many-to-one>映射
<class name="mypack.Customer" table="CUSTOMERS">
<composite-id name=customerId" class ="mypack.CustomerId">
<key-property name="name" column="NAME" type="string"/>
<key-many-to-one name="company"
column="VERSION" unsaved-value="0"/>
</class>
四、Column属性
<column
name="column_name"
length="N"
precision="N"
scale="N"
not-null="true|false"
unique="true|false"
unique-key="multicolumn_unique_key_name"
index="index_name"
sql-type="sql_type_name"
check="SQL expression"
default="SQL expression"/>
<formula>SQL expression</formula>
column and formula attributes may even be combined within the same property or association mapping to express, for example, exotic join conditions.
<many-to-one name="homeAddress" update="false">
<column name="person_id" not-null="true" length="10"/>
<formula>'MAILING'</formula>
</many-to-one>