java - hibernate 查询。 Actor 类

标签 java hibernate hql

有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/

相关文章:

java - HQL 中类似于 "contains any"

java - 将基于 JOIN 的聚合查询结果映射到 Hibernate 属性

Java ReplaceAll(..) 和正则表达式

hibernate - 带有Spring Data JPA + Hibernate的基于判别器的 Multi-Tenancy

java - Mac OSX Lion 上的 Eclipse 插件 Aptana 错误

java - 使用 JMX 运行 Hibernate 4.3(没有 Spring )

MySQL触发器以某种方式导致提交

java - Hibernate:如何编写 HQL 来获取没有其标识关系记录的实体的记录

java - 桌面 Java 程序中的 swf

java - j2ee 应用程序中用户友好的 url