小编Sil*_*nce的帖子

将列表参数设置为本机查询

我想将参数设置为本机查询,

javax.persistence.EntityManager.createNativeQuery
Run Code Online (Sandbox Code Playgroud)

这样的事情

Query query = em.createNativeQuery("SELECT * FROM TABLE_A a WHERE a.name IN ?");
List<String> paramList = new ArrayList<String>();
paramList.add("firstValue");
paramList.add("secondValue");
query.setParameter(1, paramList);
Run Code Online (Sandbox Code Playgroud)

尝试此查询会导致异常:

Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have  an error in your SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near
'_binary'??\0?sr\0?java.util.ArrayListx?????a??\0?I\0?sizexp\0\0\0?w?\0\0\0t\0
f' at line 1
Error Code: 1064
Call: SELECT * FROM Client a WHERE a.name IN ?
        bind => [[firstValue, secondValue]]
Query: ReadAllQuery(referenceClass=TABLE_A …
Run Code Online (Sandbox Code Playgroud)

java sql jpa nativequery

7
推荐指数
1
解决办法
9703
查看次数

如何使用@ConstructorResult注释

我有一些本机查询,并希望将查询执行的结果映射到非实体POJO类的列表:

@SqlResultSetMapping(
    name = "SomeMapping",
    classes = {
        @ConstructorResult(targetClass = SomeClass.class,
            columns = {
                @ColumnResult(name = "id", type = Integer.class),
                @ColumnResult(name = "NAME", type = String.class),
                @ColumnResult(name = "DATE_BEGIN", type = java.util.Date.class)
            }
        )
    }
)

public class SomeClass{
    private Integer id;
    private String name;
    private java.util.Date begDate;
    private java.util.Date endDate;

    public SomeClass(Integer id, String name, Date begDate){
        this.id = id;
        this.name = name;
        this.begDate = begDate;
    }

    //Getters & Setters ...
}
Run Code Online (Sandbox Code Playgroud)

一般来说我想如何检索查询执行的结果:

String query = "SELECT " + …
Run Code Online (Sandbox Code Playgroud)

java jpa nativequery

6
推荐指数
1
解决办法
7085
查看次数

标签 统计

java ×2

jpa ×2

nativequery ×2

sql ×1