相关疑难解决方法(0)

HQL或Java持久性查询语言中的IN子句

我有以下参数化的JPA或Hibernate查询:

SELECT entity FROM Entity entity WHERE name IN (?)
Run Code Online (Sandbox Code Playgroud)

我想将参数作为ArrayList <String>传递,这可能吗?Hibernate电流告诉我,那

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
Run Code Online (Sandbox Code Playgroud)

这有可能吗?

解答:作为参数的集合仅适用于像" :name" 这样的命名参数,而不适用于像" ?" 这样的JDBC样式参数.

java hibernate jpa hql jpql

75
推荐指数
3
解决办法
13万
查看次数

JPA将列表传递给命名本机查询中的IN子句

我知道我可以在JPA中将列表传递给命名查询,但NamedNativeQuery怎么样?我尝试了很多方法,但仍然无法将列表传递给NamedNativeQuery.任何人都知道如何将列表传递给NamedNativeQuery中的in子句?非常感谢你!

NamedNativeQuery如下:

@NamedNativeQuery(
   name="User.findByUserIdList", 
   query="select u.user_id, u.dob, u.name, u.sex, u.address from user u "+
         "where u.user_id in (?userIdList)"
)
Run Code Online (Sandbox Code Playgroud)

它被称为这样:

List<Object[]> userList = em.createNamedQuery("User.findByUserIdList").setParameter("userIdList", list).getResultList();
Run Code Online (Sandbox Code Playgroud)

但结果并不像我预期的那样.

System.out.println(userList.size());  //output 1

Object[] user = userList.get(0);
System.out.println(user.length);   //expected 5 but result is 3
System.out.println(user[0]);       //output MDAVERSION which is not a user_id
System.out.println(user[1]);       //output 5
System.out.println(user[2]);       //output 7
Run Code Online (Sandbox Code Playgroud)

jpa arraylist in-clause nativequery

28
推荐指数
5
解决办法
6万
查看次数

将参数设置为IN表达式的列表

每当我尝试将列表设置为在IN表达式中使用的参数时,我都会得到一个Illegal参数异常.互联网上的各种帖子似乎表明这是可能的,但它肯定不适合我.我正在使用Glassfish V2.1和Toplink.

有没有其他人能够让这个工作,如果是这样的话怎么样?

这是一些示例代码:

List<String> logins = em.createQuery("SELECT a.accountManager.loginName " +
    "FROM Account a " +
    "WHERE a.id IN (:ids)")
    .setParameter("ids",Arrays.asList(new Long(1000100), new Long(1000110)))
    .getResultList();
Run Code Online (Sandbox Code Playgroud)

以及堆栈跟踪的相关部分:

java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.Arrays$ArrayList for parameter accountIds with expected type of class java.lang.Long from query string SELECT a.accountManager.loginName FROM Account a WHERE a.id IN (:accountIds).
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.setParameterInternal(EJBQueryImpl.java:663)
at oracle.toplink.essentials.internal.ejb.cmp3.EJBQueryImpl.setParameter(EJBQueryImpl.java:202)
at com.corenap.newtDAO.ContactDaoBean.getNotificationAddresses(ContactDaoBean.java:437)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)
at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2920) …

java jpa glassfish jpql toplink-essentials

23
推荐指数
3
解决办法
6万
查看次数

jpa命名查询:列表的命名绑定变量

如果你有一个带有列表的命名查询,例如:

@NamedQuery(name="selection" , query=" SELECT x FROM Employee x WHERE x.name IN ('Jack', 'Jill')")
Run Code Online (Sandbox Code Playgroud)

是否可以将列表设置为命名绑定变量,以便您设置所需的内容:

q.setParameter( .......  );
Run Code Online (Sandbox Code Playgroud)

欢迎大家提出意见

java sql jpa

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

Spring Data JPA存储库:派生查询中的IN子句不起作用

我有一个看起来像这样的存储库:

public interface UserRepository extends JpaRepository<User, Long>
{
    User findByEmailIgnoreCase(String email);

    @Query("select u from User u where u.id in (:ids)")
    Set<User> getByIdInSet(@Param("ids") Set<Long> ids);
}
Run Code Online (Sandbox Code Playgroud)

当我打电话时,getByIdInSet我收到以下错误:

Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class org.eclipse.persistence.indirection.IndirectSet for parameter ids with expected type of class java.lang.Long from query string select u from User u where u.id in (:ids).
    at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:933)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:593)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
Run Code Online (Sandbox Code Playgroud)

显然我不知道是什么意思,因为我尝试更改各种数据类型,仍然得到相同的错误.我传递的id组只包含一个id.请指出我正确的方向.

java jpa spring-data spring-data-jpa

6
推荐指数
2
解决办法
2万
查看次数

如何将空列表发送到 IN 子句

我想使用这个 SQL 查询:

String hql = "select e from " + Terminals.class.getName() + " e WHERE e.merchantId IN :merchant_ids";
        TypedQuery<Terminals> query = entityManager.createQuery(hql, Terminals.class).setParameter("merchant_ids", merchant_ids);
        List<Terminals> merchants = query.getResultList();
Run Code Online (Sandbox Code Playgroud)

但我收到错误:the right syntax to use near ')所以 IN 子句列表IN (....)不能为空。这个问题有解决办法吗?

java jpa jpa-2.0 spring-data-jpa

5
推荐指数
2
解决办法
3612
查看次数

java.sql.SQLException:ORA-00932:不一致的数据类型:预期NUMBER得到了BINARY

我在Dao类中有一个返回的方法List<Object[]>,我使用的是命名查询

public List<Object[]> getListByCustomer(Session session, int customerId, List<Integer> strIds) {
  Query namedQuery = session.createSQLQuery(QueryConstants.EXPORT);
  namedQuery.setParameter("customer", customerId);
  namedQuery.setParameter("stringId", strIds);
  List<Object[]> objects = namedQuery.list();
  return objects;
}
Run Code Online (Sandbox Code Playgroud)

我想将List<Integer> strIdsstringId 传递给命名查询,如下所示:

public class QueryConstants {
  public static final String EXPORT = 
    "SELECT sv.NAME, sv.TYPE, sv.CLIENT_ADDRESS, sv.NAME_REDUNDANT, sv.DEPARTURE_DATE, s1.CODE,sv.STATE, sv.CODE "
    + "FROM VIEW sv, PROCESS p1, SET s1 " 
    + "WHERE sv.R_ID = p1.R_ID and p1.ISSUER_ID = s1.USER_ID and sv.CUSTOMER_ID = :customer and sv.R_ID IN (:stringId)";
}
Run Code Online (Sandbox Code Playgroud)

但我明白了 ORA-00932: …

java sql oracle named-query

4
推荐指数
3
解决办法
4万
查看次数