Skip to content
Advertisement

Unable to locate appropriate constructor on class [ClassName]

I want to send a sql query by using Spring JPA like :

However, when I remove some fields like “ms.width”, I get the error:

I understand that return Object[] cannot be parsed to DTO object. If I write constructor without the parameter – “Width”, it will work properly. However, I want to provide that a query can be sendable without some parameters(sometimes one of them, sometimes five of them) and a result can be parsable with FamilyMaterialDTO.

How can I do? I don’t have to use DTO, if there is another solution for this problem, please recommend.

Advertisement

Answer

I think you can make the query to return a map and you can create a constructor for FamilyMaterialDTO which takes the argument as a map. And based on the keys present in the map you can set the values… For simplicity let me create my own class.

Class foo { String a; Integer b; Boolean c;}

And a sample query

"select new map (a as a, b as b) from Foo f"

Now this query will return a list of maps and the size of the list depends on how many rows the query returns.

Now you can create a constructor like this. Assume the size of list is 1.

foo (List<Map<?,?> list) { Map map = list.get(0);

If(map.containsKey("a")) this.a = map.get("a");

If(map.containsKey("b")) this.b = map.get("b");

If(map.containsKey("c")) this.c = map.get("c"); }

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement