简体   繁体   English

Hibernate找不到实体构造器

[英]Hibernate cannot find Entity Constructor

I have an other hibernate issue. 我还有另一个冬眠问题。 I am trying to receive all projects, which have the most recent messages. 我正在尝试接收所有具有最新消息的项目。 For that I try to get all messages, group them by their project id and apply the max function on the updated column. 为此,我尝试获取所有消息,按它们的项目ID对其进行分组,然后将max函数应用于更新的列。 My query is build as shown here: 我的查询构建如下所示:

query.select(from).orderBy(builder.desc(from.get(Message_.UPDATED))).multiselect(from.get(Message_.ID),builder.max(from.get(Message_.UPDATED))).groupBy(from.get(Message_.PROJECT));

But I receive an Exception, 但是我收到一个例外

[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: de.ls5.wt2.Message]

where it cannot transform the Result back into a Message Entity. 无法将结果转换回消息实体的地方。 Do I have to define a proper constructor? 我必须定义一个适当的构造函数吗? That would be very unfortunate, because it contains other entities, making the query very hard. 那将是非常不幸的,因为它包含其他实体,这使查询非常困难。 And joining all messages with jointype right with above query doesnt work, sicne jointype right is not supported. 并且无法将所有具有jointype权限的消息与上述查询一起使用,不支持sicne jointype权限。 Any advice how I can get a List of Message entities which have the id of the result of above query? 任何建议如何获取具有上述查询结果ID的Message实体列表?

Any help is apreciated. 任何帮助都非常感谢。

EDIT: Message class: 编辑:消息类:

@Entity
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    private Date created;
    private Date updated;
    @OneToOne
    private User creator;
    @OneToOne
    private Project project;
    private String content;

    public Message() {

    }
    //getter and setter
   }

Since you are multiselecting only these two columns: 由于您仅选择了以下两列:

.multiselect(
    from.get(Message_.ID),
    builder.max(from.get(Message_.UPDATED))
) 

You need to have a constructor for your query result made of two things: 您需要为查询结果提供一个构造函数,该构造函数由两件事组成:

  • Object for Message_.ID (long id) Message_.ID(长ID)的对象
  • Object for Message_.UPDATED (Date updated) Message_.UPDATED的对象(更新日期)

So, either create a custom object containing these fields with all args constructor. 因此,可以使用所有args构造函数创建一个包含这些字段的自定义对象。 This is also called projection. 这也称为投影。 Another and simpler way is to just create a constructor in the existing entity with these two parameters. 另一种更简单的方法是仅使用这两个参数在现有实体中创建构造函数。

public Message(long id, Date updated) {
     this.id = id;
     this.updated = updated;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM