Java 休眠@CollectionFelments和@Parent

Java 休眠@CollectionFelments和@Parent,java,hibernate,jpa,jakarta-ee,parent-child,Java,Hibernate,Jpa,Jakarta 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 /

我已经完成了以下映射:

@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;
    }



}
我的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");
    }

}
实际上我不明白为什么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);
News news1=新新闻();
新闻2=新新闻();
List picList=new ArrayList();
图片picture1=新图片();
图片列表。添加(图片1);
图1.赛特新闻(新闻2);
新闻1.设置图片(图片列表);
this.newsDAO.saveOrUpdate(news1);
this.newsDAO.saveOrUpdate(news2);
既然同一张图片将出现在news1列表中,但其父图片也被设置为news2,会发生什么

我想我可以没有那个家长,我不需要那么多,但这只是好奇。。。 谢谢

顺便说一句,我想只有一个新闻图片的大小->不能有两个相同的新闻小图片。
那么,是否可以在嵌入实体中的{news_id,imageSize}上添加唯一约束?我不知道怎么做,因为我的图片中没有声明id字段可嵌入实体

我不熟悉@Embeddeble的@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);
}
既然同一张图片将出现在news1列表中,但其父图片也被设置为news2,会发生什么


@embeddeble是一个“嵌入”到另一个对象中的对象,这意味着它只属于另一个对象(父对象)。因此,它应该只包含一个父级。如果更改父项,它将只属于此新父项。如果您需要一个对象(
Picture
)来与其他对象(
News
)建立关系,那么您需要一个@manytomy(如果另一个对象,
News
,也可能链接到多个
Picture

对于@embeddeble,我不熟悉@Parent注释,但是对于“real”关系始终建议您执行以下操作:

// 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);
}
既然同一张图片将出现在news1列表中,但其父图片也被设置为news2,会发生什么

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

// 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);
}
// what you have:
newsToSave.setPictures(picturesList);

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