AnswerBun.com

Error Could not locate appropriate constructor on class in SqlResultSetMapping?

Stack Overflow Asked by Sebastian Ruiz on December 16, 2020

Hello I am having problems, I have not found the solution for the following
I have a method that makes querys dynamically, I can’t alias the fields

@Override
    public RequestVo getDatosLocos(String query) {
        Query query = entityManager.createNativeQuery(query, "queryMapping");
    
        RequestVo requestVo = query.getSingleResult();
        return requestVo;
    }

the query that is currently running is

"select id, event_id, body, body_notification from bulletins where id = 11"

This is the object that I want to return, as you can see I have the column bodyNotification and eventId

public class RequestVo {
    
    private Long id;
    private Long eventId;
    private String title;
    private String body;
    private String bodyNotification;
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getEventId() {
        return eventId;
    }
    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getBodyNotification() {
        return bodyNotification;
    }
    public void setBodyNotification(String bodyNotification) {
        this.bodyNotification = bodyNotification;
    }
    public RequestVo(Long id, Long eventId, String title, String body, String bodyNotification) {
        super();
        this.id = id;
        this.eventId = eventId;
        this.title = title;
        this.body = body;
        this.bodyNotification = bodyNotification;
    }
    
    public RequestVo() {
        
        super();
    }
}

My entity look

@SqlResultSetMapping(name="queryMapping", classes = {
        @ConstructorResult(targetClass = RequestVo.class, 
        columns = {@ColumnResult(name="id"), @ColumnResult(name="event_id"),
                @ColumnResult(name="body"), @ColumnResult(name="body_notification")})
    })

but I get the following error Could not locate appropriate constructor on class

it is because in my constructor I have different names of the columns

how could i fix this? without adding an alias to the query since as indicated I can not move the query

One Answer

In the sqlResultSetMapping you have defined only 4 fields, and your constructor wn the RequestVo class has 5, define the constructor as follows, matching the parameters that you indicate in the sqlResultSetMap:

public RequestVo(Long id, Long eventId, String body, String bodyNotification) {
    super();
    this.id = id;
    this.eventId = eventId;
    this.body = body;
    this.bodyNotification = bodyNotification;
}

When defining the empty constructor and the 5 parameter constructor, it is necessary that you also implement the 4 constructor as it cannot find a way to instantiate the results.

Answered by JLazar0 on December 16, 2020

Add your own answers!

Related Questions

Bluehost hosted React pages not found

0  Asked on November 10, 2021 by paul-lindberg

   

Change a view margins programmatically

4  Asked on November 10, 2021 by m-r_dino

     

React returning view from switch statement

1  Asked on November 10, 2021 by mattblack

   

Find string then return different row value Python

0  Asked on November 10, 2021 by newtothecrew

       

React JS – route does not pick the required Component

0  Asked on November 10, 2021 by nirjal-paudel

       

get character after space of array value on javascript

9  Asked on November 7, 2021 by riko-pramayudi

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP