Hibernate细粒度划分与复合主键

细粒度划分

Person.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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<!-- Created Sat Nov 11 20:51:28 CST 2006                         -->
<hibernate-mapping package="org.lxh.hibernate">

<class name="Person" table="PERSON">
  <id name="id" column="ID" type="int">
   <generator class="assigned" />
  </id>
  <component name="name" class="org.lxh.hibernate.Name">
   <property name="firstname" column="FIRSTNAME" type="string" not-null="true" />
   <property name="lastname" column="LASTNAME" type="string" not-null="true" />
  </component>
  <component name="contact" class="org.lxh.hibernate.Contact">
   <property name="address" column="ADDRESS" type="string" not-null="true" />
   <property name="zipcode" column="ZIPCODE" type="string" not-null="true" />
   <property name="tel" column="TEL" type="string" />
  </component>
</class>

</hibernate-mapping>



Person.java

package org.lxh.hibernate;

public class Person {
// 在本类中要包含Name和Contact对象
private int id ;
private Name name ;
private Contact contact ;
public Contact getContact() {
  return contact;
}
public void setContact(Contact contact) {
  this.contact = contact;
}
public int getId() {
  return id;
}
public void setId(int id) {
  this.id = id;
}
public Name getName() {
  return name;
}
public void setName(Name name) {
  this.name = name;
}
}



Name.java

package org.lxh.hibernate;

public class Name {
private String firstname ;
private String lastname ;
public String getFirstname() {
  return firstname;
}
public void setFirstname(String firstname) {
  this.firstname = firstname;
}
public String getLastname() {
  return lastname;
}
public void setLastname(String lastname) {
  this.lastname = lastname;
}
}


Contact.java

package org.lxh.hibernate;

public class Contact {
private String address ;
private String zipcode ;
private String tel ;
public String getAddress() {
  return address;
}
public void setAddress(String address) {
  this.address = address;
}
public String getTel() {
  return tel;
}
public void setTel(String tel) {
  this.tel = tel;
}
public String getZipcode() {
  return zipcode;
}
public void setZipcode(String zipcode) {
  this.zipcode = zipcode;
}
}




复合主键
如果要实现复合主键,首先类应该先满足以下要求:
1、本类必须实现 Serializable接
2、复写 equals 和 hashCode 方法
· equals:对象比较方法
· hashCode:取得 Hash
对于 equals 和 hashCode方法的复写,可以使用第三方工具:commons-lang-1.0.1.jar



Person.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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->
<!-- Created Sat Nov 11 21:30:08 CST 2006                         -->
<hibernate-mapping package="org.lxh.hibernate02">

    <class name="Person" table="PERSON">
        <composite-id>
            <key-property name="name" column="NAME" type="string"/>
            <key-property name="phone" column="PHONE" type="string"/>
        </composite-id>
        <property name="age" column="AGE" type="int" />
    </class>
   
</hibernate-mapping>



Person.java
package org.lxh.hibernate02;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class Person implements Serializable {
private String name;

private String phone;

private int age;

public boolean equals(Object obj) {
  // TODO 自动生成方法存根
  if (this == obj) {
   return true;
  }
  if (!(obj instanceof Person)) {
   return false;
  }
  Person p = (Person) obj;
  return new EqualsBuilder().append(this.name, p.name).append(this.phone,
    p.phone).append(this.age, p.age).isEquals();
}

public int hashCode() {
  // TODO 自动生成方法存根
  return new HashCodeBuilder().append(this.name).append(this.age).append(
    this.phone).toHashCode();
}

public int getAge() {
  return age;
}

public void setAge(int age) {
  this.age = age;
}

public String getPhone() {
  return phone;
}

public void setPhone(String phone) {
  this.phone = phone;
}

public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
}

}



PersonOperate.java
package org.lxh.hibernate02;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class PersonOperate {
private Session session ;
public PersonOperate()
{
  this.session = new Configuration().configure().buildSessionFactory().openSession() ;
}
public void insert(Person p)
{
  this.session.save(p) ;
  this.session.beginTransaction().commit() ;
}
}


你可能感兴趣的:(apache,xml,.net,Hibernate,MyEclipse)