Force Hibernate to always use a default no args constructor even when select parameters are specified

Description

The issue I'm facing is that when creating a query with SELECT, Hibernate expects the existence of a specific constructor that corresponds to the parameters given in SELECT. For example, if I specify that I'm only interested in id (long) and model (String) columns in my BasicInfo class, then Hibernate attempts to find and call BasicInfo(int, String) constructor. This is indicated by the error message shown below:

I'm looking for a way to force Hibernate to use a default class constructor and then using the setters of requested fields. If not that, maybe there is another way that would allow to not retrieve full row data from the database when each request is made (and then limiting it in the application code before sending back to the client).

Environment

JVM

Activity

Show:
Łukasz Ungier
October 1, 2020, 12:08 PM

Yes, it is worthwhile, because I want to create a generic solution, not only for a specific use.

Łukasz Ungier
2 days ago

So is there any way to achieve that goal?

Nathan Xu
2 days ago

TBH, I don’t think the request makes sense. It even has bad side effect. Creating a corresponding constructor makes more sense and is not a big deal, IMHO.

Łukasz Ungier
14 hours ago

Yes, it does make sense when we want to create a generic API implementation. My goal is to pass the list of selectors linked to a particular data class, not having prepared previously this class for this very list of selectors.

Nathan Xu
12 hours ago

Again, this is a feature request, not a bug. IMHO, it is not a generic feature (arguably it doesn’t make sense for constructor with parameter != default constructor + setter). You can achieve the goal without modifying Hibernate, I think.

Assignee

Unassigned

Reporter

Łukasz Ungier

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.4.12

Affects versions

Priority

Major
Configure