hibernate联合主键
一、联合主键没有封装在对象中
持久化类
public class Customer implements Serializable {
private String countryCode;
private String idCardNo;
private String firstName;
private String lastName;
private String address;
private String email;
然后是setter和getter方法
Customer.hbm.xml文件配置
<hibernate-mapping>
<class name="lianhezhujian.Customer">
<composite-id>
<key-property name="countryCode" length="100"></key-
property>
<key-property name="idCardNo" length="50"></key-property>
</composite-id>
<property name="address"></property>
<property name="email"></property>
<property name="firstName"></property>
<property name="lastName"></property>
</class>
</hibernate-mapping>
二、联合主键封装在对象中
持久化类
public class Customer implements Serializable {
private String CustId;
private String firstName;
private String lastName;
private String address;
private String email;
封装的持久化类
public class CustId {
private String countryCode;
private String idCardNo;
Customer.hbm.xml配置文件
<composite-id class="lianhefengzhuang.CustId">
<key-property name="countryCode" length="100"></key-property>
<key-property name="idCardNo" length="50"></key-property>
</composite-id>
三、注解方式联合主键
持久化类
@Entity
@Table(name = "customer2")
public class Customer implements Serializable{
@EmbeddedId
private CustomerId customerId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name",length = 50)
private String lastName;
private String address;
private String email;
封装的持久化类
@Embeddable
public class CustomerId implements Serializable{
@Column(length =50)
private String countryCode;
@Column(length = 50)
private String idCardNo;
hibernamte.cfg.xml配置文件注意要更改mapping配置文件
<mapping class="Hibernate_lianhezhujian.Customer"></mapping>