使用复合主键和注释映射ManyToMany: [英] Mapping ManyToMany with composite Primary key and Annotation:

查看:87
本文介绍了使用复合主键和注释映射ManyToMany:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用复合主键在学生和教学课程之间创建许多复杂的关系:



我的课程:

  @Entity 
@Table(name =Student_mtm_cId)
public class Student {

private String id;
私人设置< StudentTClass> teachingClasses = new HashSet< StudentTClass>();

@OneToMany(fetch = FetchType.LAZY,mappedBy =pk.student)
public Set< StudentTClass> getTeachingClasses(){
return teachingClasses;
}
public void setTeachingClasses(Set< StudentTClass> teachingClasses){
this.teachingClasses = teachingClasses;
}

public void addStudentToClass(TeachingClass teachingClass){
StudentTClass studentTClass = new StudentTClass();
studentTClass.setStudent(this);
studentTClass.setTeachingClass(teachingClass);
teachingClasses.add(studentTClass);
}

public void setLastName(String lastName){
this.lastName = lastName;

$ b @Id @GeneratedValue(generator =system-uuid)
@GenericGenerator(name =system-uuid,strategy =uuid)
@Column(name =student_id,nullable = false)
public String getId(){
return id;
}

public void setId(String id){
this.id = id;
}

//所有其​​他setter和getter和isequal / hashCode被省略。
}

TeachingClass:
<$










$ private String id;
私人字符串名称;
私有字符串描述;
私人设置< StudentTClass> teachingClasses = new HashSet< StudentTClass>();

Public TeachingClass(){}

public TeachingClass(String name,String description){
super();
this.name = name;
this.description = description;
}

public void addStudentToClass(Student student){
StudentTClass studentTClass = new StudentTClass();
studentTClass.setStudent(student);
studentTClass.setTeachingClass(this);
teachingClasses.add(studentTClass);
}

@OneToMany(fetch = FetchType.LAZY,mappedBy =pk.teachingClass)
public Set< StudentTClass> getTeachingClasses(){
return teachingClasses;
}

public void setTeachingClasses(Set< StudentTClass> teachingClasses){
this.teachingClasses = teachingClasses;
}

public void setDescription(String description){
this.description = description;

$ b @Id @GeneratedValue(generator =system-uuid)
@GenericGenerator(name =system-uuid,strategy =uuid)
@Column(name =teachingClass_id,nullable = false)
public String getId(){
return id;
}

public void setId(String id){
this.id = id;


$ / code $ / pre
$ b

集合对象:

  @Entity 
@Table(name =student_TClass_MTM)
@AssociationOverrides({
@AssociationOverride(name =pk .student,joinColumns = @JoinColumn(名称= student_id数据)),
@AssociationOverride(名称= pk.teachingClass,joinColumns = @JoinColumn(名称= teachingClass_id))
})
public class StudentTClass {

@EmbeddedId
private StudentTClassPK pk = new StudentTClassPK();

public StudentTClassPK getPk(){
return pk;
}

public void setPk(StudentTClassPK pk){
this.pk = pk;

$ b $ public StudentTClass(){}

@Transient
public Student getStudent(){
return this.pk.getStudent( );
}

@Transient
public TeachingClass getTeachingClass(){
return this.pk.getTeachingClass();
}

public void setStudent(Student student){
this.pk.setStudent(student);
}

public void setTeachingClass(TeachingClass teachingClass){
this.pk.setTeachingClass(teachingClass);
}

}

现在主键: p>

  @Embeddable 
public class StudentTClassPK implements Serializable {

private static final long serialVersionUID = -7261887879839337877L ;
私立学生;
私人TeachingClass teachingClass;

@ManyToOne
public Student getStudent(){
return student;
}
public void setStudent(Student student){
this.student = student;
}

@ManyToOne
public TeachingClass getTeachingClass(){
return teachingClass;
}
public void setTeachingClass(TeachingClass teachingClass){
this.teachingClass = teachingClass;
}
public StudentTClassPK(Student student,TeachingClass teachingClass){
this.student = student;
this.teachingClass = teachingClass;

public StudentTClassPK(){}

$ b}

当我试图坚持学生时,我得到以下错误:

 原因:org。 hibernate.MappingException:无法确定类型:com.vanilla.objects.Student,在表:student_TClass_MTM,为列:在org.hibernate.mapping.SimpleValue [org.hibernate.mapping.Column(学生)
.getType(SimpleValue.java:306)
。在org.hibernate.tuple.PropertyFactory.buildStandardProperty(PropertyFactory.java:143)LT
。在org.hibernate.tuple.component.ComponentMetamodel&;初始化>( ComponentMetamodel.java:68)
at org.hibernate.mapping.Component.buildType(Component.java:184)
at org.hibernate.mapping.Component.getType(Component.java:177)
在org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)
在org.hibernate.mapping.RootClass.validate(RootClass.java:236)
在org.hibernate.cfg .Configuration.validate(C onfiguration.java:1362)美元,org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865 B $ B)
在org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)在org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)

在org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 51 more

我做错了什么?

解决方案

我解决了这个问题。

  public class StudentTClass {

// @ EmbeddedId
私人StudentTClassPK pk =新StudentTClassPK();

@EmbeddedId
public StudentTClassPK getPk(){
return pk;
}


I'm trying to create manytomany realation between Student and Teaching Course using Composite Primary key:

my classes:

@Entity
@Table(name="Student_mtm_cId")
public class Student {

    private String id;
    private Set<StudentTClass> teachingClasses = new HashSet<StudentTClass>();

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.student")
    public Set<StudentTClass> getTeachingClasses() {
        return teachingClasses;
    }
    public void setTeachingClasses(Set<StudentTClass> teachingClasses) {
        this.teachingClasses = teachingClasses;
    }

    public void addStudentToClass(TeachingClass teachingClass){
        StudentTClass studentTClass = new StudentTClass();
        studentTClass.setStudent(this);
        studentTClass.setTeachingClass(teachingClass);
        teachingClasses.add(studentTClass);
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Id @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
     @Column(name = "student_id", nullable = false)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    //all other setters and getters and isequal/hashCode omitted.
}

TeachingClass:

@Entity
@Table(name="TechingClass_MTM")
public class TeachingClass {

    private String id;
    private String name;
    private String description;
    private Set<StudentTClass> teachingClasses = new HashSet<StudentTClass>();

    public TeachingClass(){}

    public TeachingClass(String name, String description) {
        super();
        this.name = name;
        this.description = description;
    }

    public void addStudentToClass(Student student){
        StudentTClass studentTClass = new StudentTClass();
        studentTClass.setStudent(student);
        studentTClass.setTeachingClass(this);
        teachingClasses.add(studentTClass);
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.teachingClass")
    public Set<StudentTClass> getTeachingClasses() {
        return teachingClasses;
    }

    public void setTeachingClasses(Set<StudentTClass> teachingClasses) {
        this.teachingClasses = teachingClasses;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Id @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")    
     @Column(name = "teachingClass_id", nullable = false)
    public String getId() {
        return id;
    }

public void setId(String id) {
        this.id = id;
    }
}

Collection Objects:

@Entity
@Table(name = "student_TClass_MTM")
@AssociationOverrides({
@AssociationOverride(name = "pk.student", joinColumns = @JoinColumn(name = "student_id")),
@AssociationOverride(name = "pk.teachingClass", joinColumns = @JoinColumn(name = "teachingClass_id"))
        })
public class StudentTClass {

    @EmbeddedId
    private StudentTClassPK pk = new StudentTClassPK();

    public StudentTClassPK getPk() {
        return pk;
    }

    public void setPk(StudentTClassPK pk) {
        this.pk = pk;
    }

    public StudentTClass() {}

    @Transient
    public Student getStudent(){
     return this.pk.getStudent();
    }

    @Transient
    public TeachingClass getTeachingClass(){
     return this.pk.getTeachingClass();     
    }

    public void setStudent(Student student){
        this.pk.setStudent(student);
    }

    public void setTeachingClass(TeachingClass teachingClass){
        this.pk.setTeachingClass(teachingClass);    
    }

    }

Now The primary Key:

@Embeddable
public class StudentTClassPK implements Serializable{

    private static final long serialVersionUID = -7261887879839337877L;
    private Student student;
    private TeachingClass teachingClass;

    @ManyToOne
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }

    @ManyToOne
    public TeachingClass getTeachingClass() {
        return teachingClass;
    }
    public void setTeachingClass(TeachingClass teachingClass) {
        this.teachingClass = teachingClass;
    }
    public StudentTClassPK(Student student, TeachingClass teachingClass) {
        this.student = student;
        this.teachingClass = teachingClass;
    }
    public StudentTClassPK() {}


}

When I'm trying to Persist Student I got the following error:

Caused by: org.hibernate.MappingException: Could not determine type for: com.vanilla.objects.Student, at table: student_TClass_MTM, for columns: [org.hibernate.mapping.Column(student)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
    at org.hibernate.tuple.PropertyFactory.buildStandardProperty(PropertyFactory.java:143)
    at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:68)
    at org.hibernate.mapping.Component.buildType(Component.java:184)
    at org.hibernate.mapping.Component.getType(Component.java:177)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:236)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 51 more

What am I doing wrong?

解决方案

I solved this issue. I mapped Getter instead of field.

public class StudentTClass {

    //@EmbeddedId
    private StudentTClassPK pk = new StudentTClassPK();

    @EmbeddedId
    public StudentTClassPK getPk() {
        return pk;
    }

这篇关于使用复合主键和注释映射ManyToMany:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆