gpt4 book ai didi

java - 迭代创建 createCriteria。出现错误 org.hibernate.QueryException : duplicate association path

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:31 24 4
gpt4 key购买 nike

我有一个图像表,每个图像与一个画廊表都有多对多的关系。我正在尝试使用 org.hibernate.Criteria 返回链接到给定画廊列表的所有图像。

我一直在尝试以下操作,我循环遍历画廊列表并为每个画廊创建一个新的createCriteria(例如,每个所需画廊一个内部连接),这仅适用于一个画廊。但是,当我尝试多次时,我收到 org.hibernate.QueryException:重复关联路径:画廊 错误...

ImagesDAOCustom.java

public Images getRandomImagesInGallerys(String[] galleryList) {             
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Images.class, "image");
// We want an image that is part of all galleries so we need to create
// a join per gallery.
for (String gallery: galleryList) {
criteria.createCriteria("image.galleries", gallery);
criteria.add(Restrictions.eq(gallery + ".name",gallery));
}
criteria.setProjection(Projections.distinct(Projections.property("id")));
List<Integer> imageIdList = criteria.list();

Images result;
if (imageIdList.size() > 0) {
int index = rand.nextInt(imageIdList.size());
result = imagesDAO.findOne(imageIdList.get(index));
} else {
result = null;
}
return result;
}

图像.java

public class Images  {

@Id
@GeneratedValue
@Column(name="ugc_images_id")
private int id;

@JsonIgnore
@JsonView(Views.Internal.class)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name="ugc_image_galleries", joinColumns={@JoinColumn(name="image_id")}, inverseJoinColumns={@JoinColumn(name="gallery_id")})
private Set<Galleries> galleries = new HashSet<>();

...
}

画廊.java

public class Galleries }

@Id
@GeneratedValue
@Column(name="ugc_galleries_id")
private int id;

@JsonManagedReference
@ManyToMany(mappedBy="galleries")
private Set<Images> images = new HashSet<>();

@Column(name="name")
private String name;

...

}

最佳答案

我遇到了同样的问题,我搜索了好几个小时。

这对我有用:

public Images getRandomImagesInGallerys(String[] galleryList) {

String query = "from Images ";
boolean first = true;

for(String string : galleryList) {
if(first){
query+="where '"+string+"' member of galleries ";
first=false;
} else {
query+="or '"+string+"' member of galleries ";
}
}

entityManager.createQuery(query, Images.class).getResultList();
}

希望它能帮助别人。

关于java - 迭代创建 createCriteria。出现错误 org.hibernate.QueryException : duplicate association path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38134955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com