hibernate 复合主键

hibernate 复合主键
在实体中可以定义一个嵌入式组件(embedded component),甚至覆盖该实体中原有的列映射.组件类必须在类一级定义@Embeddable注解.
在特定的实体的关联属性上使用@Embedded和@AttributeOverride注解可以覆盖该属性对应的嵌入式对象的列映射:
作为嵌入式主键类,要满足以下几点要求。
必须实现 Serializable 接口、必须有默认的 public 无参数的构造方法、必须覆盖 equals 和 hashCode 方法,这些要求与使用复合主键的要求相同。
将嵌入式主键类使用 @Embeddable 标注,表示这个是一个嵌入式类。

//方法1

import javax.persistence.Embeddable; 
@Embeddable 
public class DemoPK1  implements Serializable{  
    private static final long serialVersionUID = -3304319243957837925L;  
    private long id ;  
    private String name ;  
    public long getId() {  
        return id;  
    }    
    public void setId(long id) {  
        this.id = id;  
    }   
    public String getName() {  
        return name;  
    }
    public void setName(String name) {  
        this.name = name;  
    }  
 
    @Override 
    public boolean equals(Object o) {  
        if(o instanceof DemoPK1){  
            DemoPK1 key = (DemoPK1)o ;  
            if(this.id == key.getId() && this.name.equals(key.getName())){  
                return true ;  
            }  
        }  
        return false ;  
    }  
      
    @Override 
    public int hashCode() {  
        return this.name.hashCode();  
    }  
      
}
@Entity
@Table(name="Test")
public class Demo1 implements Serializable{  
{
    private String email ;  
    private String phone ;
    private String DemoPK1 pk;
    @Id 
    public DemoPK1 getPk() {  
        return pk;  
    }
    public void setPk(DemoPK1 pk1) {  
        this.pk = pk1;  
    }
    @Column(name="phone" , length=20)  
    public String getPhone() {  
        return phone;  
    }  
    public void setPhone(String phone) {  
        this.phone = phone;  
    }
    @Column(name="email" , length=23)  
    public String getEmail() {  
        return email;  
    }    
    public void setEmail(String email) {  
        this.email = email;  
    }
   
   
    //省略get set 方法注视
}


// 方法2
public class DemoPK2  implements Serializable{  
    private static final long serialVersionUID = -3304319243957837925L;  
    private long id ;  
    private String name ;  
    public long getId() {  
        return id;  
    }    
    public void setId(long id) {  
        this.id = id;  
    }   
    public String getName() {  
        return name;  
    }
    public void setName(String name) {  
        this.name = name;  
    }  
 
    @Override 
    public boolean equals(Object o) {  
        if(o instanceof DemoPK2){  
            DemoPK2 key = (DemoPK2)o ;  
            if(this.id == key.getId() && this.name.equals(key.getName())){  
                return true ;  
            }  
        }  
        return false ;  
    }  
      
    @Override 
    public int hashCode() {  
        return this.name.hashCode();  
    }      


@Entity
@Table(name="Test")
public class Demo2 implements Serializable{  
{
    private String email ;  
    private String phone ;
    private String DemoPK2 pk;
    @EmbeddedId 
    public DemoPK2 getPk() {  
        return pk;  
    }
    public void setPk(DemoPK2 pk1) {  
        this.pk = pk1;  
    }
    @Column(name="phone" , length=20)  
    public String getPhone() {  
        return phone;  
    }  
    public void setPhone(String phone) {  
        this.phone = phone;  
    }
    @Column(name="email" , length=23)  
    public String getEmail() {  
        return email;  
    }    
    public void setEmail(String email) {  
        this.email = email;  
    }
   
   
    //省略get set 方法注视
}

// 方法3
public class DemoPK3  implements Serializable{  
    private static final long serialVersionUID = -3304319243957837925L;  
    private long id ;  
    private String name ;  
    public long getId() {  
        return id;  
    }    
    public void setId(long id) {  
        this.id = id;  
    }   
    public String getName() {  
        return name;  
    }
    public void setName(String name) {  
        this.name = name;  
    }  
 
    @Override 
    public boolean equals(Object o) {  
        if(o instanceof DemoPK3){  
            DemoPK3 key = (DemoPK3)o ;  
            if(this.id == key.getId() && this.name.equals(key.getName())){  
                return true ;  
            }  
        }  
        return false ;  
    }  
      
    @Override 
    public int hashCode() {  
        return this.name.hashCode();  
    }      
}

@Entity 
@Table(name="Test")  
@IdClass(DemoPK3.class)  
public class Demo3 {
    private String email ;  
    private String phone ;
    @Column(name="phone" , length=20)  
    public String getPhone() {  
        return phone;  
    }  
    public void setPhone(String phone) {  
        this.phone = phone;  
    }
    @Column(name="email" , length=23)  
    public String getEmail() {  
        return email;  
    }    
    public void setEmail(String email) {  
        this.email = email;  
    }
}

下面看下配置文件:
<?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> 
  
        <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> 
 
        <property name="connection.pool_size">100</property> 
 
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
 
        <property name="current_session_context_class">thread</property> 
 
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
            
        <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.Demo1"/> 
    <mapping class="com.Demo2"/>
    <mapping class="com.Demo3"/>
 
 
    </session-factory> 
 
</hibernate-configuration>

//参考连接:
http://blog.csdn.net/ly5156/article/details/6755585 JPA实体的注解规范以及Hibernate特有的扩展

//测试代码:
criteria.addOrder(Order.desc("pk.id"));//对符合主键排序注意的地方
criteria.addOrder(Order.desc("pk.name"));          
List list=criteria.list(); 
//转载:http://laodaobazi.iteye.com/blog/903236

你可能感兴趣的:(Hibernate)