0

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

用户.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;
    }

事件.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;

    }
4

1 回答 1

3

您不能使用 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;
}
于 2013-04-30T15:39:43.330 回答