新建数据库User:
- CREATE TABLE user (
- name VARCHAR(100) NOT NULL,
- phone VARCHAR(50) NOT NULL,
- age INT,
- PRIMARY KEY(name, phone)
- );
现将name和phone定义成复合主键。分别使用2种方法如下:
方法1.复合主键字段直接包含在PO类中。
User.java(需要实现Serializable接口,并定义equals()和hashCode()方法)
- package com.hb3.pack_03.model;
- import java.io.Serializable;
- import org.apache.commons.lang.builder.EqualsBuilder;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- public class User implements Serializable {
- private static final long serialVersionUID = -8377583111386512407L;
- private String name;
- private String phone;
- private Integer age;
-
- public User() {
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
-
- public boolean equals(Object obj) {
- if(obj == this) {
- return true;
- }
-
- if(!(obj instanceof User)) {
- return false;
- }
-
- User user = (User) obj;
- return new EqualsBuilder()
- .append(this.name, user.getName())
- .append(this.phone, user.getPhone())
- .isEquals();
- }
-
- public int hashCode() {
- return new HashCodeBuilder()
- .append(this.name)
- .append(this.phone)
- .toHashCode();
- }
- }
在定义equals和hashCode方法时使用了apache的common-lang包。
User.hbm.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.hb3.pack_03.model.User" table="user">
-
- <composite-id>
-
- <key-property name="name"
- column="name"
- type="java.lang.String"/>
- <key-property name="phone"
- column="phone"
- type="java.lang.String"/>
- </composite-id>
- <property name="age" column="age" type="java.lang.Integer"/>
-
- </class>
-
- </hibernate-mapping>
hibernate.cfg.xml中修改:
......
<mapping resource="com/hb3/pack_03/model/User.hbm.xml" />
......
测试代码如下:
- package com.hb3.pack_03;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_03.model.User;
- public class BusinessService {
- public static void main(String[] args) {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
- User user = new User();
- user.setName("shenbin");
- user.setPhone("0970123456");
- user.setAge(28);
- Transaction tx = session.beginTransaction();
- session.save(user);
- tx.commit();
-
- user = (User) session.load(User.class, user);
-
- System.out.println(user.getAge() + "\t" + user.getName() + "\t" + user.getPhone());
- session.close();
- sessionFactory.close();
- }
- }
请注意:在实际测试过程中,如果数据库已经有数据存在,则不需要上面的黑题部分向数据库追加数据。
另外还请留意的是,光有session.save(user);而没有tx.commit();的话,数据不会被持久到数据库中的。
方法2.复合主键字段独立到另一个类中。
UserPK.java(需要实现Serializable接口,并定义equals()和hashCode()方法)
- package com.hb3.pack_04.model;
- import java.io.Serializable;
- import org.apache.commons.lang.builder.EqualsBuilder;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- public class UserPK implements Serializable {
- private static final long serialVersionUID = -2457999265373664790L;
- private String name;
- private String phone;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public boolean equals(Object obj) {
- if(obj == this) {
- return true;
- }
-
- if(!(obj instanceof User)) {
- return false;
- }
-
- UserPK pk = (UserPK) obj;
- return new EqualsBuilder()
- .append(this.name, pk.getName())
- .append(this.phone, pk.getPhone())
- .isEquals();
- }
-
- public int hashCode() {
- return new HashCodeBuilder()
- .append(this.name)
- .append(this.phone)
- .toHashCode();
- }
- }
User.java
- package com.hb3.pack_04.model;
- import java.io.Serializable;
- public class User implements Serializable {
- private static final long serialVersionUID = -8630481462628539996L;
- private UserPK userPK;
- private Integer age;
-
- public User() {
- }
- public UserPK getUserPK() {
- return userPK;
- }
- public void setUserPK(UserPK userPK) {
- this.userPK = userPK;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
User.hbm.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.hb3.pack_04.model.User" table="user">
-
- <composite-id name="userPK"
- class="com.hb3.pack_04.model.UserPK"
- unsaved-value="any">
- <key-property name="name"
- column="name"
- type="java.lang.String"/>
- <key-property name="phone"
- column="phone"
- type="java.lang.String"/>
- </composite-id>
-
- <property name="age" column="age" type="java.lang.Integer"/>
-
- </class>
- </hibernate-mapping>
hibernate.cfg.xml中修改:
......
<mapping resource="com/hb3/pack_04/model/User.hbm.xml" />
......
测试代码如下:
- package com.hb3.pack_04;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_04.model.User;
- import com.hb3.pack_04.model.UserPK;
- public class BusinessService {
- public static void main(String[] args) {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
- UserPK pk = new UserPK();
- pk.setName("shenbin");
- pk.setPhone("0970123456");
-
- User user = (User) session.load(User.class, pk);
-
- System.out.println(user.getAge() +
- "\t" + user.getUserPK().getName() +
- "\t" + user.getUserPK().getPhone());
- session.close();
- sessionFactory.close();
- }
- }
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程