jpql

JPQL Like Case Insensitive

有些话、适合烂在心里 提交于 2020-01-01 07:52:22
问题 I want to search data in User table by name case insensitive. @Repository public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where lower(u.name) like %lower(?1)%") public List<User> findByNameFree(String name); } I got an error: unexpected token: % . Where should I place '%'? 回答1: You can use the concat operator: @Query("select u from User u where lower(u.name) like lower(concat('%', ?1,'%'))") public List<User> findByNameFree(String name); or

JPQL Like Case Insensitive

孤人 提交于 2020-01-01 07:51:46
问题 I want to search data in User table by name case insensitive. @Repository public interface UserRepository extends JpaRepository<User, Long> { @Query("select u from User u where lower(u.name) like %lower(?1)%") public List<User> findByNameFree(String name); } I got an error: unexpected token: % . Where should I place '%'? 回答1: You can use the concat operator: @Query("select u from User u where lower(u.name) like lower(concat('%', ?1,'%'))") public List<User> findByNameFree(String name); or

How to filter collection in JPA/JPQL?

徘徊边缘 提交于 2020-01-01 05:30:12
问题 I have two entities: @Entity public class Customer implements java.io.Serializable { ... @OneToMany(fetch=FetchType.EAGER, mappedBy="customer") private Set<CustomerOrder> customerOrders; ... @Entity public class CustomerOrder implements java.io.Serializable { .... private double cost; @ManyToOne @JoinColumn(name="CUST_ID") public Customer customer; ... Now in my JPQL, I want to return those customers with their CustomerOrder.cost>1000. For example, there are three customers A, B and C. A has

JPQL: The state field path cannot be resolved to a valid type

*爱你&永不变心* 提交于 2020-01-01 05:11:05
问题 I can't make this query work: Query query = eManager.createQuery("select c FROM News c WHERE c.NEWSID = :id",News.class); return (News)query.setParameter("id", newsId).getSingleResult(); and I got this exception: Exception Description: Problem compiling [select c FROM News c WHERE c.NEWSID = :id]. [27, 35] The state field path 'c.NEWSID' cannot be resolved to a valid type.] with root cause Local Exception Stack: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b

JPA - Batch/Bulk Update - What is the better approach?

为君一笑 提交于 2020-01-01 03:19:13
问题 I found that JPA does not support the following Update: Update Person p set p.name = :name_1 where p.id = :id_1, p.name = :name_2 where p.id = :id_2, p.name = :name_3 where p.id = :id_3 .... // It could go on, depending on the size of the input. Could be in 100s So I have two options: Option 1: Query q = em.createQuery("Update Person p set p.name = :name where p.id = :id"); For ( int x=0; PersonsList.length; x++ ) { // add name and id parameters em.executeUpdate(); } Questions: Is this all

Query using “CASE WHEN” statement in WHERE causes QuerySyntaxException: unexpected AST

纵饮孤独 提交于 2019-12-31 01:52:06
问题 I'm trying to make a query using Spring Data, but I cannot make it work: @Query(SELECT t FROM Thing t WHERE name LIKE :name AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END AND CASE WHEN (:maxVal <= 0) THEN TRUE ELSE (val >= :maxVal) END) Page<Thing> getThings(@Param("name") String name, @Param("maxVal") int maxVal, @Param("minVal") minVal); StackTrace: Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: CASE

Using DISTINCT keyword in JPA on individual columns

隐身守侯 提交于 2019-12-31 01:00:18
问题 I am reading some values from a database that is horribly un-normalized (which I can't control). The call retrieves announcements for university departments, and if a user is in multiple departments (which is possible), then the same results are returned multiple times for these users. However, some departments might have different announcements, while some have the same. Is there a way for me to use the DISTINCT keyword in JPA on individual columns? This is what I currently have for the

Criteria API: filter by class type

佐手、 提交于 2019-12-30 06:58:26
问题 I'm relativley new to relational databases and I have some problems concerning the creation of queries. First I want to explain the situation shortly. I have several entity classes. All of them extend AbstractEntity or EntityProperty . So entities can have properties and properties have owning entities, so there is a bidirectional relation. Now let's say ConcreteEntity extends AbstractEntity and I want to create queries like this: Get all entities of type ConcreteEntity which has at least on

JPQL: Enum literal in SELECT NEW query

纵然是瞬间 提交于 2019-12-30 06:34:46
问题 I have a descriptor class for a couple of domain classes. The descriptor class has a field 'type' which is an enum and indicates the type of the domain class. In some queries I want to return on or more descriptors and pass the type as constructor argument. So my idea was to pass it as a query parameter: String jpql = "SELECT NEW model.ModelDescriptor" + "(t.id, t.name, t.description, :modelType) ... "; TypedQuery<ModelDescriptor> query = em.createQuery(jpql, ModelDescriptor.class); query

Tool to Execute JPQL Queries?

ⅰ亾dé卋堺 提交于 2019-12-29 11:46:13
问题 Is there any sort of tool available which allows one to execute JPQL queries against a database "directly"? I would like to type JPQL queries directly into a window and execute them. Of course it would probably require me to do quite a bit of configuration so that it would be aware of my JPA entities, etc., but I guess it could be done... Anyone know of such a tool? Thanks. 回答1: Until Eclipse Dali gets a generic JPQL editor you can use Hibernate Tools. Hibernate Tools works with Dali and