jpql

Indexed element access in JPQL

[亡魂溺海] 提交于 2019-12-06 05:19:11
Is it possible to do indexed element access in JPQL like in HQL : select o from Order o where o.items[0].id = 1234 I couldn't find something related in the JPA 2 specs, I am targeting EclipseLink JPA here, so if you come up with an EclipseLink solution, that's ok as well, although a JPQL standard solution is preferred. The INDEX function should do the trick (actually I tested it and it does): SELECT o FROM Order o JOIN o.items i WHERE i.id = 1234 AND INDEX(i) = 0 From the JPA 2.0 specification ( 4.6.17.2.2 Arithmetic Functions ): The INDEX function returns an integer value corresponding to the

Transaction required exception on execute update for JPQL update query

巧了我就是萌 提交于 2019-12-06 02:54:24
问题 I get this error when I try to run this code. Error: javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager Code: (_ut is a UserTransaction object) public void setMainCategory(Integer deptId, Integer catId) { try { Query setmain = _entityManager.createNamedQuery("Category.setAsMain"); Query removeMain = _entityManager.createNamedQuery("Category.removeMain");

Generation query when the ManyToMany relationship is used by Spring Data Jpa project

China☆狼群 提交于 2019-12-06 02:26:45
问题 I've the following entities mapping: @Entity @Table(name = "books") public class Book implements Serializable { @ManyToMany @JoinTable(name="books2categories", joinColumns=@JoinColumn(name="book_id"), inverseJoinColumns=@JoinColumn(name="category_id")) Collection<Category> categories; ... @Entity @Table(name = "categories") public class Category implements Serializable { @ManyToMany(mappedBy="categories") private Collection<Book> books; BookRepository interface is looked: public interface

Why “SELECT c” instead of “SELECT * ” in JPQL?

爷,独闯天下 提交于 2019-12-06 02:20:31
Example query in JPQL looks like this: SELECT c FROM Customer c; I read JPA specification, books and I can't find what this c means. In SQL we simply write: SELECT * FROM customer; I know we can use this c as alias i.e. in WHERE clause like this: SELECT c FROM Customer c WHERE c.name = ... but I still don't understand what this c actually is, how to call it (alias? object?) and why it has to be just after SELECT instead of *. SQL returns rows, and rows contain columns. JPQL is a bit more complex: it can return rows of columns, but also entity instances. So, suppose you have an (invalid) JPQL

Spring Data JPA - custom @Query with “@Param Date” doesn't work

时间秒杀一切 提交于 2019-12-06 00:24:19
I create custom query for getting Invoice data by Date , but it returns null . I'm sure that I set exactly the same Date for query that exists in database. I use custom query because I want more advance query to write. But issue exist in this simple query. Here is my sample code: @Query("select i from Invoice i where " + "i.expirationDate = :expDate") Invoice findCompanyInvoiceByDate(@Param("expDate") Date expDate); I tried this code but it does not work also: Invoice findByExpirationDate(Date expirationDate); I also tried to add @Temporal(TemporalType.DATE) before Date and @Param but result

JPQL (JPA) Find Object if list have intersection

最后都变了- 提交于 2019-12-05 23:11:09
I have two Classes each of which hold a list of Labels. Now I want to find every object of ClassA that holds any item of the list of ClassB . Is this possible with JPQL? Or using a single query? public class ClassA { private List<Label> labels; } public class ClassB { private List<Label> labels; } @Repository public interface ClassARepository extends JpaRepository<ClassA, Long> { @Query("SELECT c FROM ClassA c WHERE :labels ____ c.labels") public List<ClassA> findAllByLabels(@Param("labels") List<Label> labels); } thanks Try the following (I did not try it): SELECT DISTINCT(a) FROM ClassA a,

JPQL Selecting Record with Max Value

笑着哭i 提交于 2019-12-05 21:50:38
I'm trying to select the record that has the max value for a particular column and here's what I have so far: select o from Order o where o.orderNumber = :orderNumber and o.version = (select max(o.version) from Order o where o.orderNumber = :orderNumber) This seems to be generating the SQL: SELECT * FROM Order WHERE ordernumber = :orderNumber AND orderversion = (SELECT Max(orderversion) FROM order WHERE ordernumber = :orderNumber); I think the following SQL would be more efficient: SELECT * FROM order ord INNER JOIN (SELECT ordernumber, Max (version) AS version FROM order WHERE ordernumber =

Blank FROM clause in JPQL?

那年仲夏 提交于 2019-12-05 19:32:25
I'm using a JPQL select to access a Oracle DB function: select FUNCTION('function_name', 'foo', 1234) from com_mycompany_bar obj This works as expected and the function is indeed called. The thing is I actually don't need the FROM clause and would rather have it empty instead of having to access an entity just to conform to the syntax. What is the best option I have here? gpeche I see several possible answers: Within standard JPA there are two options, both involving Oracle DUAL "table" Map DUAL to an entity and use it in the FROM clause. It is the more "JPQL" way, but it is more involved: In

JPA / JPQL - bulk update

旧城冷巷雨未停 提交于 2019-12-05 16:56:04
I have to perform a bulk update on a table. Making a fast example : UPDATE Book b SET b.amount = b.amount + 1 WHERE b IN ( :books ) The problem is that b.amount can be or a NULL value or an int, and if there is a NULL value it should behave as b.amount would be equal to 1. Is there any "cast" in JPA/JPQL or any other way to work-around this problem, Thank you in advance, Regards, P You should be able to use COALESCE : UPDATE Book b SET b.amount = COALESCE(b.amount, 1) + 1 WHERE b IN ( :books ) I would go and fix the nulls first with a separate query: UPDATE Book set b.amount = 0 WHERE b.amount

Hibernate JPQL throws Update/delete queries cannot be typed

不羁的心 提交于 2019-12-05 14:28:29
I'm trying to delete an entity using HQL and it's failing. TypedQuery<Seller> query = Seller.entityManager().createQuery( "DELETE FROM Seller AS o WHERE o.company=:company AND o.id=:id", Seller.class); query.setParameter("company", company); query.setParameter("id", id); int result = query.executeUpdate(); The stacktrace I'm getting: Update/delete queries cannot be typed; nested exception is java.lang.IllegalArgumentException: Update/delete queries cannot be typed at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:296) at