因此,我尝试在spring数据中进行投影查询。这是我的模型(为了避免大量发布类,我将省略构造函数和一些注解):
public class TutorialDAO implements Serializable {
private UUID id;
private LocalDateTime created;
private String createdBy;
private LocalDateTime lastModified;
private String lastModifiedBy;
private int version;
private boolean exclusive;
@ManyToMany(mappedBy = "tutorials", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private Set<TopicDAO> topics = new HashSet<>();
@ElementCollection(fetch = FetchType.EAGER)
private Set<SectionDAO> sections = new HashSet<>();
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "tutorial_courses", joinColumns = @JoinColumn(name = "tutorial_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
private Set<CourseDAO> courses = new HashSet<>();
@OneToMany(mappedBy = "tutorial", cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, orphanRemoval = true, fetch = FetchType.EAGER)
@MapKey(name = "localizedId.locale")
private Map<String, LocalizableTutorial> localizations = new HashMap<>();
public class TopicDAO implements Serializable {
private UUID id;
private String topic;
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private Set<TutorialDAO> tutorials = new HashSet<>();
}
public class LocalizedTutorialDAO {
private UUID id;
private LocalDateTime created;
private String createdBy;
private LocalDateTime lastModified;
private String lastModifiedBy;
private int version;
private boolean exclusive;
private String name, description;
private Set<TopicDAO> topics = new HashSet<>();
private Set<SectionDAO> sections = new HashSet<>();
public LocalizedTutorialDAO(UUID id, LocalDateTime created, String createdBy, LocalDateTime lastModified,
String lastModifiedBy, int version, boolean exclusive, String name, String description,
Set<TopicDAO> topics) {
super(id, created, createdBy, lastModified, lastModifiedBy, version);
System.out.println(topics);
System.out.println();
this.name = name;
this.description = description;
this.topics = topics;
}
}
我试图做一个投影查询,它“填充”localizedtutorialdao类,这意味着我想要主题关系,例如(章节也会发生同样的情况,但因为它是一个类似的问题,所以我只会提到主题)。我有这个@query注解:
select new LocalizedTutorialDAO(t.id, t.created, t.createdBy, t.lastModified, t.lastModifiedBy, t.version, t.exclusive, (VALUE(l)).name, (VALUE(l)).description, topics) from tutorial t join t.localizations l join t.topics as topics where (VALUE(l)).name like %:name% and (KEY(l)) = :lang
要澄清的是,tutorialdao对象具有多语言支持,我的查询获取tutorialdao中的“normal”变量以及主题和部分关系。然后它从给定的语言中获取名称和描述,例如,我想看一个用葡萄牙语命名为“烹饪鱼”的教程-“pt”。现在,关于名称和描述的这一部分正在运行,我在没有获得主题和部分的情况下对其进行了测试,实体得到了正确填充。我这里的问题是我尝试了原生sql和spring数据非原生查询,但我永远无法让它工作。像这样,当前的查询,它说:
org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.fullstack.daos.projections.LocalizedTutorialDAO]. Expected arguments are: java.util.UUID, java.time.LocalDateTime, java.lang.String, java.time.LocalDateTime, java.lang.String, int, boolean, java.lang.String, java.lang.String, com.fullstack.daos.TopicDAO [select new com.fullstack.daos.projections.LocalizedTutorialDAO(t.id, t.created, t.createdBy, t.lastModified, t.lastModifiedBy, t.version, t.exclusive, (VALUE(l)).name, (VALUE(l)).description, topics) from com.fullstack.daos.TutorialDAO t join t.localizations l join t.topics as topics where (VALUE(l)).name like :name and (KEY(l)) = :lang]
如果我尝试本机,而不是把topic放在构造函数中,我会把topic.id,topic.topic放在构造函数中,它永远不会工作,因为它说它不能将topicdao转换为set。有没有办法告诉hibernate怎么做?例如,spring works自动生成的findbyid()查询生成以下sql查询:
select
tutorialda0_.id as id1_33_0_,
tutorialda0_.created as created2_33_0_,
tutorialda0_.created_by as created_3_33_0_,
tutorialda0_.last_modified as last_mod4_33_0_,
tutorialda0_.last_modified_by as last_mod5_33_0_,
tutorialda0_.version as version6_33_0_,
tutorialda0_.exclusive as exclusiv7_33_0_,
localizati1_.id as id1_8_1_,
localizati1_.locale as locale2_8_1_,
localizati1_.locale as formula413_1_,
localizati1_.id as id1_8_2_,
localizati1_.locale as locale2_8_2_,
localizati1_.description as descript3_8_2_,
localizati1_.name as name4_8_2_,
sections2_.tutorial_id as tutorial1_34_3_,
sectiondao3_.id as sections2_34_3_,
sectiondao3_.id as id1_22_4_,
sectiondao3_.parent_id as parent_i2_22_4_,
children4_.parent_id as parent_i2_22_5_,
children4_.id as id1_22_5_,
children4_.id as id1_22_6_,
children4_.parent_id as parent_i2_22_6_,
topics5_.tutorials_id as tutorial2_31_7_,
topicdao6_.id as topics_i1_31_7_,
topicdao6_.id as id1_27_8_,
topicdao6_.topic as topic2_27_8_
from
tutorials tutorialda0_
left outer join
localized_tutorial localizati1_
on tutorialda0_.id=localizati1_.id
left outer join
tutorials_sections sections2_
on tutorialda0_.id=sections2_.tutorial_id
left outer join
sections sectiondao3_
on sections2_.sections_id=sectiondao3_.id
left outer join
sections children4_
on sectiondao3_.id=children4_.parent_id
left outer join
topics_tutorials topics5_
on tutorialda0_.id=topics5_.tutorials_id
left outer join
topics topicdao6_
on topics5_.topics_id=topicdao6_.id
where
tutorialda0_.id=?
暂无答案!
目前还没有任何答案,快来回答吧!