【问题标题】:Many To Many relationship annotation in Jboss 7.1.1 using Hibernate?Jboss 7.1.1中使用Hibernate的多对多关系注释?
【发布时间】:2013-04-30 14:43:42
【问题描述】:

我一直试图在我的用户类和事件类之间获得@ManyToMany 关联。我没有这样的运气。我需要创建一个中间表,以便能够将事件与跟随它的任意数量的用户相关联,并且用户可以关注任意数量的事件。这就是我迄今为止所拥有的。我已经尝试了几个不同的例子,但没有太多运气。

User.java

@Entity
@Table(name="users")
public class User implements Serializable {

    @Column(name="name", nullable=false)
    private String name;
    @Column(name="school", nullable=false)
    private String school;
    @Column(name="email", nullable=false)
    private String email;

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;
    @Transient
    private int reputation;

    //@ManyToMany(mappedBy="followers")
    List<Event> followedEvents = new ArrayList<Event>();

    @ManyToMany(
        cascade={CascadeType.PERSIST, CascadeType.MERGE},
        mappedBy="followers",
        targetEntity=Event.class
    )
    public List<Event> getEventList(){
      return followedEvents;
    }
    public void setEventList(List<Event> list){
      followedEvents = list;
    }

Event.java

@Entity
@Table(name="events")
public class Event implements Serializable{

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;

    @Column(name="title", nullable=false)
    private String title;
    @Column(name="location", nullable=false)
    private String location;
    @Column(name="description", nullable=false)
    private String description;
    @Column(name="date", nullable=false)
    private Calendar cal = Calendar.getInstance();

    List<User> followers = new ArrayList<User>();

    @ManyToMany(cascade = {CascadeType.ALL}, targetEntity=User.class)
    @JoinTable(name = "EVENT_USER",
            joinColumns = {@JoinColumn(name = "EVENT_ID")},
            inverseJoinColumns = {@JoinColumn(name = "USER_ID")}
    )
    public List<User> getFollowers(){
        return followers;
    }
    public void setFollowers(List<User> followers){
        this.followers = followers;
    }

代码运行,但它没有创建一个中间表来保存两者之间的关联。而是保留序列化的 ArrayList。任何帮助理解为什么它没有做预期的事情将不胜感激。

如果重要的话,我会说明我实际持久化事件的位置:

public boolean attemptAttending(int eventId, String userName){
        User u = null;
        Event e = null;

        try {
            String query = "SELECT * FROM users WHERE name = ?";
            Query q = em.createNativeQuery(query, User.class);
            q.setParameter(1, userName);
            for(User user : (ArrayList<User>)q.getResultList())
                u = user;

            query = "SELECT * FROM events WHERE id = ?";
            q = em.createNativeQuery(query, Event.class);
            q.setParameter(1, eventId);
            for(Event event : (ArrayList<Event>)q.getResultList())
                e = event;
        }
        catch(Exception ex) {
            return false;
        }
        e.getFollowers().add(u);
        em.persist(e);

        return true;

    }

【问题讨论】:

    标签: hibernate jakarta-ee persistence jboss7.x


    【解决方案1】:

    您不能使用 ArrayList 来保持关联。您必须改用集合接口:CollectionListSet。 Hibernate 用它自己的 PersistentList 替换您的 ArrayList,它处理脏检查、延迟加载等。无论如何,在接口而不是实现上编程总是一个好主意。

    private List<Event> followedEvents = new ArrayList<Event>();
    
    @ManyToMany(...)
    public List<Event> getEventList(){
        return followedEvents;
    }
    public void setEventList(List<Event> list){
        followedEvents = list;
    }
    

    【讨论】:

    • 这是有道理的。感谢您澄清这一点!虽然我得到了一个不同的错误。我做错了什么吗?我正在正确定义类型......我认为Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: users, for columns: [org.hibernate.mapping.Column(followedEvents)]
    • 向我们展示完整的实体。似乎您的 Id 注释在现场,但您的 ManyToMany 注释在 getter 上。你必须始终如一。
    • 我其实不明白你的意思。就像我提到的那样,我经历过的大多数教程都仅限于整体信息。我添加了其他重要字段,除非您希望我添加实体中的所有内容。
    • 所有注释(@Id、@Column 等)都放置在实体的字段中。如果您愿意,可以使用实例变量。如果您愿意,可以选择属性。除了 ManyToMany 注释,它放在 getter 方法 (getEventList()) 上。与所有其他注释一样,它应该放在字段 (private List&lt;Event&gt; followedEvents) 上。当然,Event 实体也是如此。
    • 谢谢。很多例子都显示了两种方法都被使用了。我确实有一个问题。因此,如果我决定将我的注释放在 getter 上,它们只需要始终存在吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多