TransWikia.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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP