Java 如何将Hibernate自定义包装器对象映射为多个关系?
我试图从一个数据库中收集数据,其中图像和项目类之间有很多关系,我需要从数据库中获取图像列表,并将结果映射到一个包含其他值的包装类中, 但问题是,我无法将多个关系的结果列表映射到该包装器中,因为我无法进行join 以下是项目实体:Java 如何将Hibernate自定义包装器对象映射为多个关系?,java,hibernate,jpa,spring-data-jpa,hql,Java,Hibernate,Jpa,Spring Data Jpa,Hql,我试图从一个数据库中收集数据,其中图像和项目类之间有很多关系,我需要从数据库中获取图像列表,并将结果映射到一个包含其他值的包装类中, 但问题是,我无法将多个关系的结果列表映射到该包装器中,因为我无法进行join 以下是项目实体: @Entity @Table(name = "item") public class Item implements Serializable { private static final long serialVersionUID = 1L;
@Entity
@Table(name = "item")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "item_image", joinColumns = @JoinColumn(name = "item_id"), inverseJoinColumns = @JoinColumn(name = "image_id"))
private Set<Image> images;}
这是包装器类,我需要从数据库中获取设置的图像值:
public class ItemWrapper {
private Long id;
private Long subcategoryId;
private String name;
private String sectionNumber;
private boolean featured = false;
private boolean viewed = true;
private boolean favoured = false;
private String enDescription;
private String arDescription;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "UTC")
private LocalDate startDate;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "UTC")
private LocalDate endDate;
private Boolean isNew = false;
private String comment;
private Boolean active = true;
private Set<Image> images;
public ItemWrapper(Long id,
Long subcategoryId,
String sectionNumber,
boolean featured,
String enDescription,
String arDescription,
String name,
LocalDate startDate,
LocalDate endDate,
Boolean isNew,
String comment,
Boolean active,
boolean viewed,
boolean favoured,
Set<Image> images
) {
this.id = id;
this.subcategoryId = subcategoryId;
this.sectionNumber = sectionNumber;
this.featured = featured;
this.enDescription = enDescription;
this.arDescription = arDescription;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
this.isNew = isNew;
this.comment = comment;
this.active = active;
this.viewed = viewed;
this.favoured = favoured;
this.images = images; }}
public类ItemWrapper{
私人长id;
私有长子类别ID;
私有字符串名称;
私有字符串段号;
私有布尔值=false;
私有布尔值=真;
private boolean favored=false;
私有字符串描述;
私有字符串描述;
@JsonFormat(pattern=“yyyy-MM-dd”,timezone=“UTC”)
私有LocalDate开始日期;
@JsonFormat(pattern=“yyyy-MM-dd”,timezone=“UTC”)
私有LocalDate-endDate;
私有布尔值isNew=false;
私有字符串注释;
private Boolean active=true;
私人设置图像;
公共ItemWrapper(长id,
长子类别ID,
字符串节号,
布尔特征,
字符串描述,
字符串描述,
字符串名,
LocalDate开始日期,
LocalDate endDate,
布尔值是新的,
字符串注释,
布尔活动,
从布尔值来看,
我赞成,
设置图像
) {
this.id=id;
this.subcategoryId=子类别ID;
this.sectionNumber=sectionNumber;
特色的;特色的;
this.endDescription=endDescription;
this.arDescription=arDescription;
this.name=名称;
this.startDate=startDate;
this.endDate=endDate;
this.isNew=isNew;
this.comment=注释;
这个.active=active;
this.viewed=已查看;
this.favored=favored;
this.images=images;}
这是我试图在注释行中的两个实体之间创建联接表的存储库接口,我需要在构造函数中设置最后一个参数:
public interface ItemRepository extends JpaRepository<Item, Long> {
final String GET_ITEM_BY_ID = "SELECT new com.suncodex.elepedia.wrapper.ItemWrapper("
+ " it.id, it.subcategory.id, "
+ " it.sectionNumber, "
+ " it.featured, "
+ " it.enDescription, "
+ " it.arDescription, "
+ " gi.name, "
+ " gi.startDate, "
+ " gi.endDate, "
+ " gi.isNew, "
+ " gi.comment, "
+ " gi.active, "
+ " usd.viewed, "
+ " usd.favoured, "
+ " it.images) "
+ " FROM Item it "
+ " LEFT JOIN UserSpecialData usd "
+ " ON usd.generalInfo.id = it.generalInfo.id "
+ " LEFT JOIN it.generalInfo gi "
// + " LEFT JOIN ItemImage im "
// + " ON im.item.id = it.id "
+ " WHERE usd.user.id =:userId "
+ " AND it.id = :itemId";
@Query(GET_ITEM_BY_ID)
ItemWrapper getItemById(@Param("itemId") long itemId, @Param("userId") long userId);}
public interface ItemRepository扩展了JpaRepository{
最后一个字符串GET_ITEM_BY_ID=“SELECT new com.suncodex.elepedia.wrapper.ItemWrapper(”
+it.id,it.subcategory.id
+“它是一个数字,”
+“它是我的特色,”
+“它是一种描述,”
+“这是我的描述,”
+“gi.name,”
+“开始日期,”
+“gi.endDate,”
+“我是新来的,”
+“gi.comment,”
+“gi.active,”
+“美元,”
+“美元优惠,”
+“it.images)”
+“从项目it”
+“LEFT JOIN UserSpecialData usd”
+“ON usd.generalInfo.id=it.generalInfo.id”
+“LEFT JOIN it.generalInfo gi”
//+“左连接项图像im”
//+“ON im.item.id=it.id”
+“其中usd.user.id=:userId”
+“AND it.id=:itemId”;
@查询(按项目ID获取项目)
ItemWrapper getItemById(@Param(“itemId”)long itemId,@Param(“userId”)long userId);}
public interface ItemRepository extends JpaRepository<Item, Long> {
final String GET_ITEM_BY_ID = "SELECT new com.suncodex.elepedia.wrapper.ItemWrapper("
+ " it.id, it.subcategory.id, "
+ " it.sectionNumber, "
+ " it.featured, "
+ " it.enDescription, "
+ " it.arDescription, "
+ " gi.name, "
+ " gi.startDate, "
+ " gi.endDate, "
+ " gi.isNew, "
+ " gi.comment, "
+ " gi.active, "
+ " usd.viewed, "
+ " usd.favoured, "
+ " it.images) "
+ " FROM Item it "
+ " LEFT JOIN UserSpecialData usd "
+ " ON usd.generalInfo.id = it.generalInfo.id "
+ " LEFT JOIN it.generalInfo gi "
// + " LEFT JOIN ItemImage im "
// + " ON im.item.id = it.id "
+ " WHERE usd.user.id =:userId "
+ " AND it.id = :itemId";
@Query(GET_ITEM_BY_ID)
ItemWrapper getItemById(@Param("itemId") long itemId, @Param("userId") long userId);}