在JPA中将单表映射到可嵌入集合 [英] Mapping Single Table to Embeddable Collection in JPA

查看:96
本文介绍了在JPA中将单表映射到可嵌入集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当独特的情况,试图将单个表映射到JPA中的多个实体。我已阅读@Embeddable和@ElementCollection,但我不知道如何在我的情况下使用它们(或者如果可以的话)。一个数据库表包含课程信息。表中可以有行,除了少数值之外,课程中的所有内容都是相同的,例如房间号和日期。例如:

  TERM_CODE SUBJECT_CODE ROOM DAY INSTRUCTOR_ID 
201220 EGRE 0101 TR 123
201220 EGRE 0103 W 124

有没有一种方法可以像上面那样从两行中提取数据,并将常用数据放入一个对象和不同对象集合中的不同值?以下是我想如何定义类的示例:

  @Entity 
public class Course implements Serializable {

@Id
@Column(name =TERM_CODE)
private长期代码;

@Column(name =SUBJECT_CODE)
private String subjectCode;

@Embedded
收藏< CourseSchedule>时间表;

public Long getTermCode(){
return termCode;
}

public void setTermCode(Long termCode){
this.termCode = termCode;
}

public String getSubjectCode(){
return subjectCode;
}

public void setSubjectCode(String subjectCode){
this.subjectCode = subjectCode;


课程安排:

  @Embeddable 
公共类CourseSchedule {
私人字符串房间;
私人字符串日;

public String getRoom(){
return room;
}

public void setRoom(String room){
this.room = room;
}

public String getDay(){
return day;
}

public void setDay(String day){
this.day = day;
}

public String getInstructorId(){
return instructorId;
}

public void setInstructorId(String instructorId){
this.instructorId = instructorId;
}
}

我对JPQL的外观感到困惑就像在这种情况下,一旦我得到它们的映射。

编辑:

如果我将@Id添加到TERM_CODE列,则返回一个没有Hibernate错误的Course对象,但作为课程一部分的CourseSchedule Collection为null。



编辑2:



我尝试将Course和CourseSchedule作为两个单独的表(尽管它们不是),但我似乎无法使用@OneToMany和@ManyToOne来加入它们。 / p>

  @Entity 
@IdClass(CourseId.class)
@Table(name =course_table)
public class Courseizable Serializable {

@OneToMany(mappedBy =course)
private Collection< CourseSchedule>时间表;

@Id
@Column(name =TERM_CODE)
private长期代码;

@Id
@Column(name =SUBJECT_CODE)
private long subjectCode;


}

@Entity
@IdClass(CourseScheduleId.class)
@Table(name =course_table)
public class CourseSchedule实现Serializable {

@ManyToOne
@JoinColumns({
@JoinColumn(name =TERM_CODE,referencedColumnName =TERM_CODE),
@JoinColumn(name =SUBJECT_CODE,referencedColumnName =SUBJECT_CODE)
})
私人课程;

@Column(name =TERM_CODE)
private长期代码;

@Column(name =SUBJECT_CODE)
private long subjectCode;

@Id
私人字符串房间;

@Id
私人字符串日;

@Id
@Column(name =INSTRUCTOR_ID)
private String instructorId;

...

}

CourseId CourseScheduleId 是用于组合ID的简单类。)上述映射返回以下错误:

  org.hibernate.MappingException:外键(FK82D03688F590EF27:course_table [TERM_CODE,SUBJECT_CODE]))的列数必须与引用的列数相同主键(course_table [ROOM,DAY,INSTRUCTOR_ID] 

我不需要参照CourseSchedule课程,如果这有助于更容易。



任何想法?我唯一的想法是将它们定义为完全独立的实体(未加入),然后以某种方式将它们映射到一起使用JPQL。

解决方案

我一直在尝试一些东西,最接近你想要的是这个(在 CourseSchedule setCourse

课程

  @Embeddable 
public class Course implements Serializable {

@Column(name =TERM_CODE)
private LongCode;

@Column(name =SUBJECT_CODE)
private String subjectCode;

@Transient
集合< CourseSchedule> schedule = new ArrayList< CourseSchedule>();

public void setSchedule(Collection< CourseSchedule> schedule){
this.schedule = schedule;
}
public Collection< CourseSchedule> getSchedule(){
return schedule;
}

public Long getTermCode(){
return termCode;
}

public void setTermCode(Long termCode){
this.termCode = termCode;
}

public String getSubjectCode(){
return subjectCode;
}

public void setSubjectCode(String subjectCode){
this.subjectCode = subjectCode;


课程时间表

  @Entity(name =Course)
public class CourseSchedule {
private String room;
私人字符串日;

@Id
@GeneratedValue
private int id;

public int getId(){
return id;
}

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

@嵌入
私人课程;

公开课程getCourse(){
return course;
}

public void setCourse(Course course){
course.schedule.add(this);
this.course = course;
}

public String getRoom(){
return room;
}

public void setRoom(String room){
this.room = room;
}

public String getDay(){
return day;
}

public void setDay(String day){
this.day = day;




$ b

生成的数据库表

  id subject_code term_code日间房间
1EGRE201220TR0101
2EGRE201220W0103

基本上相反。这并不完全符合你的期望,但它可能会激励你获得更好的解决方案,如果你有更多的想法或者发现了一些有趣的事情,请保持更新。


I have a fairly unique situation trying to map a single table to multiple entities in JPA. I have read about @Embeddable and @ElementCollection, but I'm not sure how to use them in my situation (or if I can). The one database table holds Course information. There can be rows in the table where everything in the Course is the same except for a few values, such as the room number and day. For example:

TERM_CODE   SUBJECT_CODE    ROOM    DAY    INSTRUCTOR_ID
201220      EGRE            0101    TR     123
201220      EGRE            0103    W      124

Is there a way that I can pull data from two rows as above, and put the common data in one Object and the different values in a Collection of separate Objects? Here's an example of how I'd like to have the classes defined:

@Entity
public class Course implements Serializable {

    @Id
    @Column(name = "TERM_CODE")
    private Long termCode;

    @Column(name = "SUBJECT_CODE")
    private String subjectCode;

    @Embedded
    Collection<CourseSchedule> schedule;

    public Long getTermCode() {
     return termCode;
    }

    public void setTermCode(Long termCode) {
     this.termCode = termCode;
    }

    public String getSubjectCode() {
     return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
     this.subjectCode = subjectCode;
    }
}

CourseSchedule:

@Embeddable
public class CourseSchedule {
     private String room;
     private String day;

      public String getRoom() {
       return room;
      }

      public void setRoom(String room) {
       this.room = room;
      }

      public String getDay() {
       return day;
      }

      public void setDay(String day) {
       this.day = day;
      }

      public String getInstructorId() {
       return instructorId;
      }

      public void setInstructorId(String instructorId) {
       this.instructorId = instructorId;
      }
}

I also am confused as to what my JPQL would look like in this situation once I get them mapped.

EDIT:

If I add @Id to the TERM_CODE column, a Course object is returned without Hibernate errors, but the CourseSchedule Collection that is part of the Course is null.

EDIT 2:

I've tried to play around with treating Course and CourseSchedule as two separate tables (even though they're not), but I can't seem to get them joined using @OneToMany and @ManyToOne.

@Entity
@IdClass(CourseId.class)
@Table(name = "course_table")
public class Course implements Serializable {

    @OneToMany(mappedBy = "course")
    private Collection<CourseSchedule> schedule;

    @Id
    @Column(name = "TERM_CODE")
    private Long termCode;

    @Id
    @Column(name = "SUBJECT_CODE")
    private Long subjectCode;

    ...
}

@Entity
@IdClass(CourseScheduleId.class)
@Table(name = "course_table")
public class CourseSchedule implements Serializable {

    @ManyToOne
    @JoinColumns({
    @JoinColumn(name="TERM_CODE", referencedColumnName="TERM_CODE"),
    @JoinColumn(name = "SUBJECT_CODE", referencedColumnName="SUBJECT_CODE")
    })
    private Course course;

    @Column(name = "TERM_CODE")
    private Long termCode;

    @Column(name = "SUBJECT_CODE")
    private Long subjectCode;

    @Id
    private String room;

    @Id
    private String day;

    @Id
    @Column(name = "INSTRUCTOR_ID")
    private String instructorId;

    ...

}

(The CourseId and CourseScheduleId are simple classes used for the composite ID.) The above mapping return the following error:

org.hibernate.MappingException: Foreign key (FK82D03688F590EF27:course_table [TERM_CODE,SUBJECT_CODE])) must have same number of columns as the referenced primary key (course_table [ROOM,DAY,INSTRUCTOR_ID)

I don't need the CourseSchedule referring back to the Course if that helps make it easier.

Any ideas? My only other thought is to define them as completely separate entities (not joined), and then somehow map them together using JPQL.

解决方案

I have been trying some things, and the closest I get to what you want is this(check the setter setCourse in the CourseSchedule class):

Course

@Embeddable
public class Course implements Serializable {

@Column(name = "TERM_CODE")
private Long termCode;

@Column(name = "SUBJECT_CODE")
private String subjectCode;

@Transient
Collection<CourseSchedule> schedule = new ArrayList<CourseSchedule>();

public void setSchedule(Collection<CourseSchedule> schedule) {
    this.schedule = schedule;
}
public Collection<CourseSchedule> getSchedule() {
    return schedule;
}

public Long getTermCode() {
 return termCode;
 }

 public void setTermCode(Long termCode) {
 this.termCode = termCode;
 }

 public String getSubjectCode() {
return subjectCode;
 }

 public void setSubjectCode(String subjectCode) {
 this.subjectCode = subjectCode;
 }
}

CourseSchedule

@Entity(name="Course")
public class CourseSchedule {
private String room;
private String day;

@Id
@GeneratedValue
private int id;

public int getId() {
    return id;
}

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

@Embedded
private Course course;

public Course getCourse() {
    return course;
}

public void setCourse(Course course) {
    course.schedule.add(this);
    this.course = course;
}

public String getRoom() {
    return room;
}

public void setRoom(String room) {
    this.room = room;
}

public String getDay() {
    return day;
}

public void setDay(String day) {
    this.day = day;
}
}

generated database table

id    subject_code    term_code    day    room
1      "EGRE"           201220     "TR"   "0101"
2      "EGRE"           201220     "W"    "0103"

Basically the other way around. It isn't completely what you expect, but it might inspire you for a better solution, please keep me updated if you have more ideas or found something interesting...

这篇关于在JPA中将单表映射到可嵌入集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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