createNativeQuery set parameter

半腔热情 提交于 2019-12-07 21:47:31

问题


I have the following that contains a NativeQuery where i need to set a parameter but somothing is wrong beacause parameter not set so the query is

SELECT movieId, title, genres FROM movies where title like '%%'"

so return all the rows. What is wrong

public List<T> findMovie(String keyword) {
        Query q = getEntityManager().createNativeQuery("SELECT movieId, title, genres FROM movies where title like '%?%'", entityClass);
        q.setParameter(1, keyword); //etc
        return q.getResultList();
    }

回答1:


public List<T> findMovie(String keyword) {
    Query q = getEntityManager().createQuery("SELECT movieId, title, genres FROM movies where title like :keyword", entityClass);
    q.setParameter("keyword", keyword); //etc
    return q.getResultList();
}

If you want to use positional params, use this syntax:

public List<T> findMovie(String keyword) {
    Query q = getEntityManager().createQuery("SELECT movieId, title, genres FROM movies where title like ?1", entityClass);
    q.setParameter(1, keyword); //etc
    return q.getResultList();
}



回答2:


Suppose your query is "SELECT movieId, title, genres FROM movies where title like = thor".

Here, your query will return a list with movieId, title, genres parameters. In your native query it will return a list of Object[]. Here Object[] has your desired data with specific position.

You can follow steps:

Your Projected Response class will like bellow:

public class MovieObject{
    int movieId;
    String title;
    String genres;

    public MovieObject(Object[] columns) {
        this.movieId = (columns[0] != null)?((BigDecimal)columns[0]).intValue():0;
        this.title = (String) columns[1];
        this.genres = (String) columns[2];
    }

    public int getMovieId() {
        return movieId;
    }

    public void setMovieId(int movieId) {
        this.movieId = movieId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGenres() {
        return genres;
    }

    public void setGenres(String genres) {
        this.genres = genres;
    }
}

here MovieObject constructor will map position-wise data of Movie Object[].

public MovieObject(Object[] columns) {
            this.movieId = (columns[0] != null)?((BigDecimal)columns[0]).intValue():0;
        this.title = (String) columns[1];
        this.genres = (String) columns[2];
        }

Your query execution function will like bellow :

public List<MovieObject> getMovieByTitle(EntityManager entityManager,String title) {

    String queryStr = "SELECT movieId, title, genres FROM movies where title like = ?1";
    try {
        Query query = entityManager.createNativeQuery(queryStr);
        query.setParameter(1, title);

        List<Object[]> objectList = query.getResultList();

        List<MovieObject> result = new ArrayList<>();
        for (Object[] row : objectList) {
            result.add(new MovieObject(row));
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

Here imports are:

import javax.persistence.Query;
import javax.persistence.EntityManager;

Now your main class, you have to call this function. First get EntityManager and call this getMovieByTitle(EntityManager entityManager,String title) function. Calling procedure is given bellow:

Import this

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

Now you have to call the function that will execute the query and return the value. Your execution code is like bellow:

@PersistenceContext
private EntityManager entityManager;

List<MovieObject> movieObjects=getMovieByTitle(entityManager,"thor");

Now, your processed data is in the List movieObjects.

If you want more details then visit this thread

Thanks :)



来源:https://stackoverflow.com/questions/42096354/createnativequery-set-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!