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

onMessageReceived() not being called | Discord Bot

1  Asked on February 7, 2021 by l0raxeo-c

     

Providing tabulated data to a SQL Update

0  Asked on February 7, 2021 by greg-b-roberts

     

Use list item index as index to another list in python

2  Asked on February 7, 2021 by jonna_983

     

how to create a persistence arraylist structure in java?

2  Asked on February 7, 2021 by pikachu-sir

   

IndexError: tuple index out of range when running python 3.9.1

1  Asked on February 7, 2021 by ahmad-nur-hasybi

       

Azure Functions in VS Code

2  Asked on February 7, 2021 by nje

     

Python __hash__ performance for bulky data

0  Asked on February 7, 2021 by user14848693

   

error: failed to push some refs to ‘https://github.com/

8  Asked on February 7, 2021 by koeradoera

     

Ask a Question

Get help from others!

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