关闭

hibernate联合主键

143人阅读 评论(0) 收藏 举报
分类:

①首先我们要搭建主类:

  package study;
import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Student{
private StudentPK PK;
private int age;
private String sex;

@Id
public StudentPK getPK() {
return PK;
}
public void setPK(StudentPK pK) {
PK = pK;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}

②我们搭建组建类:
package study;


import javax.persistence.Embeddable;
@Embeddable
public class StudentPK implements java.io.Serializable{
private int id;
private String name;

public int getId() {
return id;
}
public void setId(int 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 StudentPK){
StudentPK pk = (StudentPK)o;
if(this.id == pk.getId()&&this.name.equals(pk.getName()));
return true;
}
return false;
}
}

③配置配置文件:

 <mapping  class="study.StudentPK"/>
 <mapping  class="study.Student"/>

在编写程序的过程中,遇到了2个问题:
 org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

这是因为在hibernate.cfg.xml中没有配置组建类:<mapping class="study.StudentPK">

org.hibernate.MappingException: Unknown entity: study.Student
这是因为没有配置@Entity

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3641次
    • 积分:132
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论