createNativeQuery设置参数

时间:2017-02-07 17:36:05

标签: jpa parameter-passing nativequery

我有以下内容,其中包含一个NativeQuery,我需要设置一个参数,但是somothing是错误的,因为参数未设置,所以查询是

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

所以返回所有行。有什么不对

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();
    }

2 个答案:

答案 0 :(得分:3)

<ClusterConnectionParameters ConnectionEndpoint="{myBox}.{MyDomain}.net:19000" WindowsCredentials="true" ServerCertThumbprint="0123456789012345678901234567890123456789" />

如果您想使用位置参数,请使用以下语法:

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();
}

答案 1 :(得分:0)

假设您的查询是“ SELECT movieId,title,genres FROM movies其中title = = ”。

您的Response类如下:

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;
    }
}

这里MovieObject构造函数会将位置数据映射到对象。

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 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;
    }
}

这里的输入是:

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

现在你的主类,你必须调用这个函数。首先获取EntityManager并调用此getMovieByTitle(EntityManager entityManager,String title)函数。呼叫程序如下:

导入

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

以这种方式获取EntityManager

@PersistenceContext
private EntityManager entityManager;

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

现在你在这个movieObjects中有数据。

更多信息请访问此thread

谢谢:)