@ManyToMany链接表中没有条目

时间:2013-08-20 15:55:22

标签: jsf jpa many-to-many entity jpa-2.0

也许这里有人可以给我一个错误所在的提示(JSF 2.2,Glassfish 4.0):

  • 我有两个具有多种关系的实体(参见示例)
  • 当我在glassfish中部署我的项目时,所有表(也是链接表)都正确生成(在persistence.xml中启用了create-tables):TAGUSERWISH,TAGUSERWISH_WISH(链接表),WISH
  • 当我执行一个persist(参见示例)时,实体“wish”和“tagUserWish”被正确保存,但是当我直接查看mysql表时,没有任何内容写入链接表。但是当我用JPA读出“wish”时,List<TagUserWish>已填满
  • 一旦新会话开始(重新部署),List<TagUserWish>在使用JPA读出时也是空的

所有者实体:

@Entity
public class Wish implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String subject;
    private String abstractT;

    @OneToOne
    private User user;

    @ManyToMany(mappedBy = "wishes", cascade = {CascadeType.ALL} )
    private List<TagUserWish> tags = new LinkedList<>();

    public void addTag(TagUserWish tag){
        tags.add(tag);
    }

    public void setTags(List<TagUserWish> tags) {
        this.tags = tags;
    }       

    public void removeTag(TagUserWish tag){
        tags.remove(tag);
    }

    public List<TagUserWish> getTags(){
        return tags;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getAbstractT() {
        return abstractT;
    }

    public void setAbstractT(String abstractT) {
        this.abstractT = abstractT;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public Long getId() {
        return id;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Wish)) {
            return false;
        }
        Wish other = (Wish) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "eu.citato.main.model.Wish[ id=" + id + " ]";
    }

}

实体2:

@Entity
public class TagUserWish implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;        
    private String name;

    public TagUserWish() {
    }          

    public TagUserWish(String name) {
        this.name = name;
    }          

    @ManyToMany
    private List<Wish> wishes = new LinkedList<>();

    public void addWish(Wish wish){
        wishes.add(wish);
    }

    public void setWishes(List<Wish> wishes) {
        this.wishes = wishes;
    }       

    public void removeWish(Wish tag){
        wishes.remove(tag);
    }

    public List<Wish> getWishes(){
        return wishes;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    } 

    public Long getId() {
        return id;
    }

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


    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TagUserWish)) {
            return false;
        }
        TagUserWish other = (TagUserWish) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "eu.citato.main.model.Tag[ id=" + id + ", name="+name+" ]";
    }     
}

我如何坚持下去:

@javax.inject.Named 
@SessionScoped
public class WishPM implements Serializable {

    @EJB
    private WishService wls;

    public void commitEditWish(){        
        List<TagUserWish> selTags = new ArrayList<>();
        selTags.add(new TagUserWish("Tag1"));
        selTags.add(new TagUserWish("Tag2"));
        currentWish = new Wish();
        currentWish.setSubject("wishSubject");
        currentWish.setAbstractT("wishAbstract");
        currentWish.setTags(selTags);
        wls.createWish(currentWish);
    }
}

祝愿服务:

@Stateless
public class WishService implements Serializable{

    @PersistenceContext(unitName = "WishlistPU")
    private EntityManager em;

    public void createWish(Wish entity){
        em.persist(entity);
    }
}

1 个答案:

答案 0 :(得分:1)

关系是基于关系的所有者方持续存在的。双向关系的所有者是反面的mappedBy的值。在以下情况中,关系的所有者是wishes实体

中的TagUserWish字段
@ManyToMany(mappedBy = "wishes", cascade = {CascadeType.ALL} )
private List<TagUserWish> tags = new LinkedList<>();

由于TagUserWish的实例确实有空wishes集合,因此关系不会保留。可以通过向TagUserWish的实例添加相关的Wish来解决问题,例如如下:

...
TagUserWish tuw1 = new TagUserWish("Tag1")
TagUserWish tuw2 = new TagUserWish("Tag2")

selTags.add(tuw1);
selTags.add(tuw2);

currentWish = new Wish();
tuw1.addWish(currentWish); //setting to owner side of relationship
tuw2.addWish(currentWish); //setting to owner side of relationship
...