如何使用JPA / EclipseLink与Junction Table建立多对多关系 [英] How do you Set Up a Many-to-Many Relationship with Junction Table using JPA/EclipseLink

查看:149
本文介绍了如何使用JPA / EclipseLink与Junction Table建立多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子:

电影:
movieID

Movies: movieID

用户:
userID

Users: userID

这些表通过Queue表有多对多的关系,
是一个额外的属性,listOrder:

These tables have a many to many relationship through the Queue table, with an additional attribute, listOrder:

队列:
movieID,
userID,
listOrder

Queue: movieID, userID, listOrder

我正在尝试使用EclipseLink对此进行建模,但我得到一个
无法比较的映射错误。以下是我的代码示例:

I'm attempting to model this using EclipseLink, but am getting an "incompattible mapping" error. Here is a sampling of my code:


@Entity
@Table(name="movieinventory")
public class Movie implements Serializable
{
       private static final long serialVersionUID = 1L;

       @Id
       @GeneratedValue
       private Integer movieID;

       @OneToMany(mappedBy="movie")
       private Set moviesInQueue;

       ...Getters/Setters...
}

@Entity
@Table(name="Users")
public class User implements Serializable
{
       private static final long serialVersionUID = 1L;

       @Id
       @GeneratedValue
       private Integer userID;

       @OneToMany(mappedBy="user")
       private Set moviesInQueue;

      ...Getters/Setters...
}

@IdClass(QueueItemPK.class)
@Entity
@Table(name="queue")
public class QueueItem
{
       @Id
       @ManyToOne
       @JoinColumn(name="movieID")
       private Movie movie;

       @Id
       @ManyToOne
       @JoinColumn(name="userID")
       private User user;

       @Basic
       private String listOrder;

       ...Getters/Setters...
}

public class QueueItemPK implements Serializable
{
       private static final long serialVersionUID = 1L;

       private Movie movie;
       private User user;

       ...Getters/Setter...

       public int hashCode()
       {
           return (movie.getMovieID() + "|" + user.getUserID()).hashCode();
       }

       public boolean equals(Object obj)
       {
           if (obj == this) return true;
           if (obj == null) return false;
           if (!(obj instanceof QueueItemPK)) return false;
           QueueItemPK pk = (QueueItemPK) obj;
           return pk.movie.getMovieID() == movie.getMovieID() 
               && pk.user.getUserID() == user.getUserID();
       }
}

QueueItemPK的目的是让我可以拥有movieID和userID的复合主
键。我不完全确定这是
的正确方法。

The purpose of the QueueItemPK is so that I can have the composite primary key of movieID and userID. I'm not absolutely sure this is the correct way to do it.

这是错误:
例外说明:
[类电影]和[类QueueItem]之间遇到不兼容的映射。这通常发生在映射的
基数与其
backpointer的基数不对应时。
我对User类有相同的错误(错误交替)。

This is the error: Exception Description: An incompatible mapping has been encountered between [class Movie] and [class QueueItem]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer. I have the same error with the User class (the errors alternate).

当我从
QueueItem中的电影和用户变量中取出@Id注释并使其他键成为主键时,它会编译
否错误。

When I take the @Id annotations off of the movie and user variables in QueueItem and make some other key the primary key, however, it compiles with no errors.

任何建议都将不胜感激。

Any suggestions would be appreciated.

谢谢,
BJ

Thanks, B.J.

推荐答案

首先,正如Mike Cornell所建议的那样,EmbeddedId / Class可能是更容易使用的选择。尽管如此,要回答你的问题和更正的代码:

First of all, as suggested by Mike Cornell, the EmbeddedId/Class is probably the easier-to-use choice. Nonetheless, to answer to your question and corrected code:

@IdClass(QueueItemPK.class)
@Entity
@Table(name="queue")
public class QueueItem
{
       @Id
       @ManyToOne(optional=false)
       @PrimaryKeyJoinColumn(name="movieID")
       private Movie movie;

       @Id
       @ManyToOne(optional=false)
       @PrimaryKeyJoinColumn(name="userID")
       private User user;

       @Basic
       private String listOrder;

       ...Getters/Setters...
}

public class QueueItemPK implements Serializable
{
       private static final long serialVersionUID = 1L;

       @Id
       @Column(name="movieID")
       private Integer movie;
       @Id
       @Column(name="userID")
       private Integer user;

       ...Getters/Setter...

       public int hashCode()
       {
           return (movie.getMovieID() + "|" + user.getUserID()).hashCode();
       }

       public boolean equals(Object obj)
       {
           if (obj == this) return true;
           if (obj == null) return false;
           if (!(obj instanceof QueueItemPK)) return false;
           QueueItemPK pk = (QueueItemPK) obj;
           return pk.movie == movie 
               && pk.user == user;
       }
}

正如你所知,他们有必要他们要匹配的id的类型。不是很漂亮,但工作。我建议在你的套装中使用泛型;使得更容易阅读,更安全的代码。

As you can see it is neccessary that they have the type of the id's they've to match. not very beautiful, but working. And i suggest using generics for your Sets; makes ist easier to read and more secure to code.

这篇关于如何使用JPA / EclipseLink与Junction Table建立多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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