Vitaliy Borisok
Vitaliy Borisok

Reputation: 861

Use SELECT NEW CONSTRUCTOR with parameter not from tables

I'm using something like this now in my HQL:

"SELECT NEW com.somepackage.dto.SomeClass(myObj) "

But now I want to add a boolean parameter to constructor. I've added it to my DAO method with HQL and to constructor of my dto object:

"SELECT NEW com.somepackage.dto.SomeClass(myObj, :param) "
...
.setParameter("param", param)

After adding parameter I got an exception:

Unable to locate appropriate constructor on class

Is there a way to add param to constructor? Or I was made something wrong? Thx for your replies and sorry for my English.

Update (Simple copy of my SomeClass):

public class SomeClass extends SomeClassParent {

private final String someParam;
private final List<MyObject> myObjects;

public SomeClass(MyObject myObject) {
    super(myObject.getFirstField,
          myObject.getSecondField, ...);
    this.someParam = myObject.getSomeParamValue();
    StringBuilder bodyBuilder = new StringBuilder();
    ...

I want it to be

public SomeClass(MyObject myObject, boolean myBoolean) {

Upvotes: 2

Views: 1757

Answers (4)

Joe Lee-Moyet
Joe Lee-Moyet

Reputation: 1845

I'm having a similar problem with constructing a result entity using a query parameter as a constructor argument - like "select new com.example.ResultType(t.id, ?1) from Table t where ...". It seems such bare query params just get ignored when looking for constructors - in that example it would look for a constructor that takes a single argument matching t.id.

The only work-around I've found so far is to wrap the parameter with a cast (or some other non-trivial expression), e.g. "select new com.example.ResultType(t.id, cast(?1 as string)) ...".

Upvotes: 0

JCalcines
JCalcines

Reputation: 1286

I'm pretty sure that your problem is with the boolean field. I worked with Hibernate HQL and I had a similar problem with a field. At the end I realized that I have to use nullable files for every field.

So, I think this problem could be solved turning boolean primitive field into Boolean Object class.

Upvotes: 0

Vitaliy Borisok
Vitaliy Borisok

Reputation: 861

I don't know exactly what was the problem with boolean, but now I'm using String parameter against boolean and use it like this:

"SELECT NEW com.somepackage.dto.SomeClass(myObj, '" + param + "') "...

setParameter doesn't want to work with String, because it requires quotes to be result string like this:

"SELECT NEW com.somepackage.dto.SomeClass(myObj, 'Some string') "...

against

"SELECT NEW com.somepackage.dto.SomeClass(myObj, Some string) "...

Upvotes: 1

Typo
Typo

Reputation: 1900

Ok, what it wasn't clear to me was if you had the SomeClass definition, you can define as many constructors as you want as long as they have different types, or quantities of parameters passed by.

It can be see as a kind of override (although it is not!)

It's up to you to define it, and do whatever you want with that Boolean, in fact you can copy and paste the original constructor (just leave that one there, don't erase it) with the Boolean's addition and it would be as valid as the previous one.

Upvotes: 0

Related Questions