gpt4 book ai didi

java - hibernate 查询。 Actor 类

转载 作者:行者123 更新时间:2023-11-30 06:12:10 26 4
gpt4 key购买 nike

有hql查询

List<Developer> developers = session.createQuery("from Developer d " +
"left join ProjectDeveloper pd on pd.developer.id = d.id " +
"where pd.project.id = " + project.getId()).getResultList();

它可以工作并获取对象列表,但不是开发人员

其实,i get 得到的对象看起来像是一个数组列表,其中包含了对象数组,其中又包含了开发者类对象和ProjectDeveloper(mtm类)。不幸的是,我无法放置与图像的链接,但架构如下所示:

developers = {ArrayList@4889} size = 4 
\/ 0 = {object[2]4897} //what does this do in ArrayList<**Developer**>>??
-> 0 = {Developer@4901} //This is exactly the class I expect in ArrayList
-> 1 = {ProjectDeveloper}//?????
-> 1 = {object[2]4898}

开发者类别:

@Table(name = "developers")
@Entity
public class Developer implements GenerallyTable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "first_name")
private String name;
@Column(name = "age")
private int age;
@Column(name = "sex")
private boolean sex;
@Column(name = "salary")
private BigDecimal salary;
//...getters + setters
}

这怎么可能(Developer.class 如何转换为结构未知的数组),以及如何才能准确获取 Developers 的 ArrayList?

最佳答案

尝试使用 createQuery 定义类型 https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html#createQuery-java.lang.String-java.lang.Class-

List<Developer> developers = session.createQuery("select d from Developer d " +
"left join ProjectDeveloper pd on pd.developer.id = d.id " +
"where pd.project.id = " + project.getId(), Developer.class)
.getResultList();

您还应该使用setParameter方法

List<Developer> developers = session.createQuery("select d from Developer d " +
"left join ProjectDeveloper pd on pd.developer.id = d.id " +
"where pd.project.id = :proj_id", Developer.class)
.setParameter("proj_id", project.getId())
.getResultList();

关于java - hibernate 查询。 Actor 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49997962/

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