`
laodaobazi
  • 浏览: 272779 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 使用 Annotation 3(联合主键)

阅读更多

Hibernate Annotation 联合主键有三种写法 :

第一种:

Jlee01.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 ;
	private JleeKey01 jleeKey ;
	
	/**
	 * @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代码:

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
	 */
	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 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代码:

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代码:

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代码:

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代码:

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 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代码:

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("444823046@qq.com") ;
		
		Jlee03 jlee03 = new Jlee03() ;
		jlee03.setId(1) ;
		jlee03.setName("jlee") ;
		jlee03.setAddress("这里") ;
		jlee03.setAge(32) ;
		jlee03.setEmail("444823046@qq.com") ;
		
		session.save(jlee01) ;
		session.save(jlee02) ;
		session.save(jlee03) ;
		
		session.getTransaction().commit() ;
		
	}
	
	public void testExport(){
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ;
	}
	
}

 

分享到:
评论
1 楼 深夜的清风 2012-10-11  
哪个是主表那个是子表

相关推荐

    Hibernate一对一单向外键关联 (联合主键annotation)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932507

    Hibernate笔记 马士兵

    第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 第4课 资源 7 第5课 环境准备 7 ...三、 联合主键 24 1、xml方式 24 2、annotation方式 27 第14课 Hibernate核心开发接口(重点) 29 ........

    最全Hibernate 参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

    Hibernate3+中文参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

    Hibernate 中文 html 帮助文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate3的帮助文档

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    hibernate 体系结构与配置 参考文档(html)

    使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate教程

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    Hibernate注释大全收藏

    以上所有实体使用 JOINED 策略 Ferry和Boat class使用同名的主键关联(eg: Boat.id = Ferry.id), AmericaCupClass 和 Boat 关联的条件为 Boat.id = AmericaCupClass.BOAT_ID. 从父类继承的属性 @MappedSuperclass ...

    hibernate3.04中文文档.chm

    6.4.2. 使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. ...

    hibernate 框架详解

    使用 JDK 5.0 的注解(Annotation) 7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合...

    Hibernate参考文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Spring3.x企业应用开发实战(完整版) part1

    《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用...

    Spring.3.x企业应用开发实战(完整版).part2

    《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目源代码

    FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上...

    springmybatis

    其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...

Global site tag (gtag.js) - Google Analytics