数据库脚本:
create table people(
name varchar(100) not null,
phone varchar(50) not null,
age int,
primary key(name,phone)
);
一、不把复合主键封装成类
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">sql2000</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="show_sql">true</property>
<mapping resource="org/myhibernate/demo02/People.hbm.xml" />
</session-factory>
</hibernate-configuration>
People.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.myhibernate.demo02.People" table="people" schema="dbo" catalog="mldn">
<composite-id>
<key-property name="name" type="java.lang.String">
<column name="name" length="100" />
</key-property>
<key-property name="phone" type="java.lang.String">
<column name="phone" length="50" />
</key-property>
</composite-id>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
</class>
</hibernate-mapping>
People.kava
package org.myhibernate.demo02;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class People implements Serializable {
private String name;
private String phone;
private int age;
@Override //此处使用到了Apache的开源工具包commons-lang-2.3.jar 复写equals和hashCode方法
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof People)) {
return false;
}
People p = (People) obj;
return new EqualsBuilder().append(this.name, p.name).append(this.phone,
p.phone).append(this.age, p.age).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(this.name).append(this.phone)
.append(this.age).toHashCode();
}
public int getAge() {
return age;
}
public void setAge(int 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;
}
}
PeopleDAO.java
package org.myhibernate.demo02;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class PeopleDAO {
private Session session;
public PeopleDAO() {
this.session = new Configuration().configure().buildSessionFactory()
.openSession();
}
public void insert(People p){
this.session.save(p);
this.session.beginTransaction().commit();
}
}
Test.java(测试文件)
package org.myhibernate.demo02;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
People p = new People();
PeopleDAO pdao = new PeopleDAO();
p.setName("咖啡迷");
p.setPhone("8888888");
p.setAge(23);
pdao.insert(p);
}
}
二、把复合主键封装成类
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">sql2000</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="show_sql">true</property>
<mapping resource="org/myhibernate/demo03/People.hbm.xml" />
</session-factory>
</hibernate-configuration>
People.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.myhibernate.demo03.People" table="people" schema="dbo" catalog="mldn">
<composite-id name="id" class="org.myhibernate.demo03.PeopleId">
<key-property name="name" type="java.lang.String">
<column name="name" length="100" />
</key-property>
<key-property name="phone" type="java.lang.String">
<column name="phone" length="50" />
</key-property>
</composite-id>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
</class>
</hibernate-mapping>
People.java
package org.myhibernate.demo03;
/**
* People generated by MyEclipse Persistence Tools
*/
public class People implements java.io.Serializable {
// Fields
private PeopleId id;
private Integer age;
// Constructors
/** default constructor */
public People() {
}
/** minimal constructor */
public People(PeopleId id) {
this.id = id;
}
/** full constructor */
public People(PeopleId id, Integer age) {
this.id = id;
this.age = age;
}
// Property accessors
public PeopleId getId() {
return this.id;
}
public void setId(PeopleId id) {
this.id = id;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
}
PeopleId.java
package org.myhibernate.demo03;
/**
* PeopleId generated by MyEclipse Persistence Tools
*/
public class PeopleId implements java.io.Serializable {
// Fields
private String name;
private String phone;
// Constructors
/** default constructor */
public PeopleId() {
}
/** full constructor */
public PeopleId(String name, String phone) {
this.name = name;
this.phone = phone;
}
// Property accessors
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof PeopleId))
return false;
PeopleId castOther = (PeopleId) other;
return ((this.getName() == castOther.getName()) || (this.getName() != null
&& castOther.getName() != null && this.getName().equals(
castOther.getName())))
&& ((this.getPhone() == castOther.getPhone()) || (this
.getPhone() != null
&& castOther.getPhone() != null && this.getPhone()
.equals(castOther.getPhone())));
}
public int hashCode() {
int result = 17;
result = 37 * result
+ (getName() == null ? 0 : this.getName().hashCode());
result = 37 * result
+ (getPhone() == null ? 0 : this.getPhone().hashCode());
return result;
}
}
PeopleDAO.java
package org.myhibernate.demo03;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class PeopleDAO {
private Session session;
public PeopleDAO() {
this.session = new Configuration().configure().buildSessionFactory()
.openSession();
}
public void insert(People p){
this.session.save(p);
this.session.beginTransaction().commit();
}
}
Test.java(测试文件)
package org.myhibernate.demo03;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
People p = new People();
PeopleId id = new PeopleId();
PeopleDAO pdao = new PeopleDAO();
id.setName("hi.baidu.com/javajavajava");
id.setPhone("8888888");
p.setId(id);
p.setAge(23);
pdao.insert(p);
}
}