简体   繁体   中英

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. 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. Any advice how I can get a List of Message entities which have the id of the result of above query?

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)
  • Object for Message_.UPDATED (Date updated)

So, either create a custom object containing these fields with all args constructor. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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