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