hibernate中联合主键的一种写法

在hibernate中使用联合主键可以对某个对象对应的联合主键建立一个主键类,这个类中包含了主键的属性,并且这个类必须实现Serializable接口,

重写equals,hashcode方法。

 

如对Student进行持久化,假设它的联合主键是sid,sname,那么可以它建立一个对应的主键类,类中包含了sid,sname

如下:

package vo;

import java.io.Serializable;

public class StudentPK implements Serializable{
 private int sid;
 private String sname;
 public int getSid() {
  return sid;
 }
 public void setSid(int sid) {
  this.sid = sid;
 }
 
 public String getSname() {
  return sname;
 }
 public void setSname(String sname) {
  this.sname = sname;
 }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + sid;
  result = prime * result + ((sname == null) ? 0 : sname.hashCode());
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  StudentPK other = (StudentPK) obj;
  if (sid != other.sid)
   return false;
  if (sname == null) {
   if (other.sname != null)
    return false;
  } else if (!sname.equals(other.sname))
   return false;
  return true;
 }
 
 
}

 

而Student类如下:

package vo;


public class Student {

 private StudentPK pk;
 private int age;
 
 
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public StudentPK getPk() {
  return pk;
 }
 public void setPk(StudentPK pk) {
  this.pk = pk;
 }
 
 
 
}
然后配置Student.hbm.xml

<hibernate-mapping>

    <class name="vo.Student" table="student">
        <composite-id name="pk" class="vo.StudentPK">
         <key-property name="sid"/>
         <key-property name="sname"/>
     </composite-id>
    
        <property name="age"/>
    </class>

</hibernate-mapping>

写测试类

package vo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class StudentTest {

 private static SessionFactory sf=null;
 @BeforeClass
 public static void getSessionFactory(){
  Configuration config = new AnnotationConfiguration();
  sf = config.configure().buildSessionFactory();
 }
 @AfterClass
 public static void closeSessionFactory(){
  sf.close();
 }
 @Test
 public void save(){
  StudentPK pk = new StudentPK();
  pk.setSid(1);
  pk.setSname("david");
  Student s = new Student();
  s.setPk(pk);
  s.setAge(22);
  
  Session session = sf.openSession();
  session.beginTransaction();
  session.save(s);
  session.getTransaction().commit();
  session.close();
 }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate 配置联合,可以通过使用 @IdClass 或 @EmbeddedId 注解来实现。 1. 使用 @IdClass 注解: 首先,需要创建一个用于表示联合的类,该类需要实现 Serializable 接口,并且需要包含所有联合字段。例如: ``` public class MyCompositeKey implements Serializable { private Long id1; private Long id2; // constructors, getters, setters, equals, hashCode, etc. } ``` 然后,在实体类使用 @IdClass 注解,并指定联合类的名称。例如: ``` @Entity @IdClass(MyCompositeKey.class) public class MyEntity { @Id private Long id1; @Id private Long id2; // other entity fields, getters, setters, etc. } ``` 2. 使用 @EmbeddedId 注解: 首先,需要创建一个用于表示联合的类,该类需要实现 Serializable 接口,并且需要使用 @Embeddable 注解标记。例如: ``` @Embeddable public class MyCompositeKey implements Serializable { private Long id1; private Long id2; // constructors, getters, setters, equals, hashCode, etc. } ``` 然后,在实体类使用 @EmbeddedId 注解,并指定联合类的名称。例如: ``` @Entity public class MyEntity { @EmbeddedId private MyCompositeKey id; // other entity fields, getters, setters, etc. } ``` 以上就是在 Hibernate 配置联合的两种方式。需要注意的是,使用 @IdClass 注解时需要在实体类指定所有联合字段,而使用 @EmbeddedId 注解时只需要在联合指定即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值