Hibernate @CollectionOfElements和@Parent

时间:2010-12-24 10:38:41

标签: java hibernate jpa java-ee parent-child

我完成了以下映射:

@Entity
@Table(name = "NWS_NEWS")
public class News implements Serializable {

    private static final long serialVersionUID = 5246618151933389186L;

    private String id;
    private List<Picture> pictures;

    + OTHER fields / getters / setters, no matter

    @Id
    @GeneratedValue(generator = "juuid")
    @Column(length = 36)
    public String getId() {
        return id;
    }


    @CollectionOfElements
    @JoinTable(name = "NWS_PICTURES",joinColumns = @JoinColumn(name="NEWS_ID"))
    @CollectionId(
            columns= @Column(name="PICTURE_ID"),
            type=@Type(type="long"),
            generator="sequence")
    public List<Picture> getPictures() {
        return pictures;
    }

    public void setPictures(List<Picture> pictures) {
        this.pictures = pictures;
    }



}

我的照片是:

@Embeddable
public class Picture implements Serializable {

    private static final long serialVersionUID = -1397366206984323622L;

    private News news;

    private String path;

    private ImageSize imageSize;


    @Parent
    public News getNews() {
        return this.news;
    }

    @Column(name = "path", nullable=false)
    public String getPath() {
        return path;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "size", nullable=false)
    public ImageSize getImageSize() {
        return imageSize;
    }


    public void setImageSize(ImageSize imageSize) {
        this.imageSize = imageSize;
    }
    public void setNews(News news) {
        this.news = news;
    }
    public void setPath(String path) {
        this.path = path;
    }


}

我的dao测试是:

@Test
public void testAddPicturesToNews() {

    News newsToSave = new News();
    // Create big picture
    Picture pBig = new Picture();
    pBig.setImageSize(ImageSize.BIG);
    pBig.setPath("/tmp/blabla_big.jpg");
    // Create medium picture
    Picture pMedium = new Picture();
    pMedium.setImageSize(ImageSize.MEDIUM);
    pMedium.setPath("/tmp/blabla_med.jpg");
    // Set the pictures in the news
    List<Picture> picturesList = new ArrayList<Picture>();
    picturesList.add(pBig);
    picturesList.add(pMedium);
    newsToSave.setPictures(picturesList);
    // Save the news
    this.newsDAO.saveOrUpdate(newsToSave);
    String newsId = newsToSave.getId();
    News newsLoaded = this.newsDAO.findById(newsId);
    List<Picture> picturesLoaded = newsLoaded.getPictures();
    for ( Picture pictureLoaded : picturesLoaded ) {
        System.out.println(pictureLoaded.getPath());
        System.out.println(pictureLoaded.getImageSize());
        System.out.println(pictureLoaded.getNews());
        System.out.println("\n");
    }

}

但输出是:

/tmp/blabla_big.jpg
BIG
null


/tmp/blabla_med.jpg
MEDIUM
null

实际上我不明白为什么getNews()在子实体实体中返回null,而它具有“@Parent”注释。难道我做错了什么?

无论如何,将父母置于子实体中的概念对我来说似乎有点奇怪,因为如果我这样做会发生什么:

News news1 = new News();
News news2 = new News();
List<Picture> picList = new ArrayList<Picture>();
Picture picture1 = new Picture();
picturesList.add(picture1);
picture1.setNews(news2);
news1.setPictures(picList);
this.newsDAO.saveOrUpdate(news1);
this.newsDAO.saveOrUpdate(news2);

由于同一张照片将出现在news1列表中,但其父节点也设置为news2 ???

我想我没有那个父母,我不需要那么多,但这只是好奇心...... 感谢

顺便说一句,我想为每个新闻只提供一张图片 - &gt;同一个新闻不能有2张小图片。 那么可以在我的嵌入式实体中的{news_id,imageSize}上添加一个唯一约束吗?我没有看到如何做到这一点,因为我的Picture embeddable实体中没有声明id字段

1 个答案:

答案 0 :(得分:0)

我不熟悉@Embeddable的@Parent注释,但对于“真实”关系,我们总是建议这样做:

// News class
public void setPictures(List<Picture> pictures) {
  this.pictures = pictures;
  for (Picture picture : pictures) {
    picture.setNews(this);
  }
}

public void addPicture(Picture picture) {
  this.pictures.add(picture);
  picture.setNews(this);
}

请记住,与关系模型相反,OOP只具有“单向”导航的概念,并且您应该自己构建“双向”。在setter中封装此行为会使您对使用者透明。所以,我不确定为什么你的@Parent不工作,但我会尝试相反的做法:

// what you have:
newsToSave.setPictures(picturesList);

// what I'd try:
pMedium.setNews(newsToSave);
  

由于同一张照片将出现在news1列表中,但其父节点也设置为news2 ???

嗯,@ Embeddable是一个“嵌入”到另一个对象的对象,这意味着它只属于另一个对象(父对象)。因此,它应该只包含一个父项。如果您更改父级,则它将仅属于此新父级。如果您需要一个对象(Picture)与其他对象(News)建立关系,则需要@ManyToMany(如果另一个对象News)也可以链接到几个Picture