Hibernate Annotation 联合主键有三种写法 :
第一种:
Jlee01.java代码:
Java代码
package com.jlee03.compositeId;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author JLee
* @since 2011-2-10
*/
@Entity
@Table(name="JLEE01")
public class Jlee01 implements Serializable{
private static final long serialVersionUID = 3524215936351012384L;
private String address ;
private int age ;
private String email ;
private String phone ;
JleeKey01 jleeKey = new JleeKey01();
/**
* @return the jleeKey
*/
@Id
public JleeKey01 getJleeKey() {
return jleeKey;
}
/**
* @param jleeKey the jleeKey to set
*/
public void setJleeKey(JleeKey01 jleeKey) {
this.jleeKey = jleeKey;
}
/**
* @return the phone
*/
@Column(name="phone" , length=20)
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the address
*/
@Column(name="address" , length=50)
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the age
*/
@Column(name="age")
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the email
*/
@Column(name="email" , length=23)
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
}
JleeKey01.java代码:
Java代码
package com.jlee03.compositeId;
import java.io.Serializable;
import javax.persistence.Embeddable;
/**
* @author JLee
* @since 2011-2-10
*/
@Embeddable
public class JleeKey01 implements Serializable{
private static final long serialVersionUID = -3304319243957837925L;
private long id ;
private String name ;
/**
* @return the id
*/
@Column(name="id",length=64)
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
@Column(name="name",length=64)
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if(o instanceof JleeKey01){
JleeKey01 key = (JleeKey01)o ;
if(this.id == key.getId() && this.name.equals(key.getName())){
return true ;
}
}
return false ;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
第二种写法:
Jlee02.java代码:
Java代码
package com.jlee03.compositeId;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @author JLee
* @since 2011-2-10
*/
@Entity
@Table(name="JLEE02")
public class Jlee02 {
private String address ;
private int age ;
private String email ;
private String phone ;
private JleeKey02 jleeKey ;
/**
* @return the jleeKey
*/
@EmbeddedId
public JleeKey02 getJleeKey() {
return jleeKey;
}
/**
* @param jleeKey the jleeKey to set
*/
public void setJleeKey(JleeKey02 jleeKey) {
this.jleeKey = jleeKey;
}
/**
* @return the phone
*/
@Column(name="phone" , length=20)
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the address
*/
@Column(name="address" , length=50)
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the age
*/
@Column(name="age")
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the email
*/
@Column(name="email" , length=23)
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
}
JleeKey02.java代码:
Java代码
package com.jlee03.compositeId;
import java.io.Serializable;
/**
* @author JLee
*/
public class JleeKey02 implements Serializable{
private static final long serialVersionUID = -3236523319933461469L;
private long id ;
private String name ;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if(o instanceof JleeKey02){
JleeKey02 key = (JleeKey02)o ;
if(this.id == key.getId() && this.name.equals(key.getName())){
return true ;
}
}
return false ;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
第三种写法:
Jlee03.java代码:
Java代码
package com.jlee03.compositeId;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="JLEE03")
@IdClass(JleeKey03.class)
public class Jlee03 {
private long id ;
private String name ;
/**
* @return the id
*/
@Id
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
@Id
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
private String address ;
private int age ;
private String email ;
private String phone ;
/**
* @return the phone
*/
@Column(name="phone" , length=20)
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the address
*/
@Column(name="address" , length=50)
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the age
*/
@Column(name="age")
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the email
*/
@Column(name="email" , length=23)
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
}
JleeKey03.java代码:
Java代码
package com.jlee03.compositeId;
import java.io.Serializable;
/**
* @author JLee
*/
public class JleeKey03 implements Serializable{
private static final long serialVersionUID = 6060166117433738173L;
private long id ;
private String name ;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if(o instanceof JleeKey03){
JleeKey03 key = (JleeKey03)o ;
if(this.id == key.getId() && this.name.equals(key.getName())){
return true ;
}
}
return false ;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
联合主键必须重写 equals 和 hashCode 方法。
hibernate.cfg.xml配置文件:
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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">100</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--<property name="hbm2ddl.auto">create</property>-->
<mapping class="com.jlee03.compositeId.Jlee01"/>
<mapping class="com.jlee03.compositeId.Jlee02"/>
<mapping class="com.jlee03.compositeId.Jlee03"/>
</session-factory>
</hibernate-configuration>
JleeTest.java代码:
Java代码
package com.jlee03.compositeId;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class JleeTest extends TestCase {
public void testJlee(){
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory() ;
Session session = sf.getCurrentSession() ;
session.beginTransaction() ;
Jlee01 jlee01 = new Jlee01() ;
JleeKey01 key01 = new JleeKey01() ;
key01.setId(0) ;
key01.setName("jlee") ;
jlee01.setJleeKey(key01) ;
jlee01.setAddress("北京") ;
jlee01.setAge(23) ;
jlee01.setPhone("21321312321") ;
Jlee02 jlee02 = new Jlee02() ;
JleeKey02 key02 = new JleeKey02() ;
key02.setId(0) ;
key02.setName("jlee") ;
jlee02.setJleeKey(key02) ;
jlee02.setAddress("上海") ;
jlee02.setAge(32) ;
jlee02.setEmail("[email protected]") ;
Jlee03 jlee03 = new Jlee03() ;
jlee03.setId(1) ;
jlee03.setName("jlee") ;
jlee03.setAddress("这里") ;
jlee03.setAge(32) ;
jlee03.setEmail("[email protected]") ;
session.save(jlee01) ;
session.save(jlee02) ;
session.save(jlee03) ;
session.getTransaction().commit() ;
}
public void testExport(){
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ;
}
}