Hibernate 使用 Annotation 之 联合主键

hibernate使用annotation注释的方法来映射联合主键查看hibernate文档有三种方法:

1,将主键类注解为@Embeddable,并将主键的属性注解为@Id;

2,将主键的属性注解为@EmbeddedId;

3,将类注解为@IdClass,并将该实体类种的所有属性主键的属性都注解为@Id;

常用的有第二第三种,下面给出三种的例子程序:

---------------主键类,实现java.io.Serializable接口重写equals,hashCode方法是必须的:


第一种:

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;
	}
	
}

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();
	}
	
}
第二种:

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;
	}
	
}

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();
	}
	
}

第三种:

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;
	}
	
}

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();
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值