当前位置: 代码迷 >> J2EE >> Hibernate联合主键查询报错,该如何处理
  详细解决方案

Hibernate联合主键查询报错,该如何处理

热度:336   发布时间:2016-04-22 02:29:35.0
Hibernate联合主键查询报错
Hi,friends.

Student.java:
Java code
@Entity@Table(name="student")@IdClass(StudentPK.class)public class Student {    @Id    public int getId() {        return id;    }    @Id    public String getName() {        return name;    }   ...}StudentPK.java:   public class StudentPK implements java.io.Serializable{    private int id;    private String name;    ...   }

Java code
session1.beginTransaction();        StudentPK pk = new StudentPK();        pk.setId(1);        pk.setName("Hiway");        Student s = (Student) session1.get(StudentPK.class, pk);        System.out.println(s);

and hibernate.cfg.xml:
  <mapping class="com.hiway.entity.Student"></mapping>
here , I use StudentPK as Student's composite key, and use session.get() to retrive Student instance.
but it fails:
org.hibernate.MappingException: Unknown entity: com.hiway.entity.StudentPK. 
so what's the problem ?

thanks in advance.

------解决方案--------------------
怎么有2个@Id?的
------解决方案--------------------
探讨
就是,你见过1个表有2个主键的吗
  相关解决方案