日期:2014-05-20  浏览次数:20799 次

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个主键的吗