jpql

How can i inner join a subquery in JPQL

一个人想着一个人 提交于 2019-12-04 10:05:37
I need a JPQL for the MySQL query: SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.table1.id INNER JOIN (SELECT * FROM table1 t3 INNER JOIN table2 t4 ON t3.id = t4.table1.id WHERE t3.name = 'xxx') subTable ON t1.number = subTable.number WHERE t1.number = '5' AND id = '3' Your query seems quite pathological, perhaps say what result you are trying to query, and include your object model. In general, JPQL does not support sub-selects in the from clause, so your query is not directly convertable to JPQL. You can always just execute it as a JPA native SQL query, since you seem to be

Return a subset of a JPA entity as a array of maps from a JPQL query?

戏子无情 提交于 2019-12-04 09:56:45
In JPQL it is possible to ask for a subset of an entity using a constructor expression such as SELECT NEW example.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e which returns a list of objects of type EmployeeDetails or using a projection select such as SELECT e.name, e.salary FROM Employee e which returns an Object[] result where result[0] is e.name and result[1] is e.salary is there a way to get JPA to return a Map which contains a subset of the entity for example is there a JPQL query that can return List<Map<String,Object>> result such that result.get(0).get("e.name")

Using @EmbeddedId with JpaRepository

好久不见. 提交于 2019-12-04 08:07:50
问题 I have simple Entitly class with the @EmbeddedId ( Integer and String fields in separate class). And I use the Spring Data ( org.springframework.data.jpa.repository.JpaRepository ) to access the database (MySql), with the normal Id the queries are working fine, both the generated by Spring and the ones wrote by myself. With the EmbeddedId I didnt manage to create the correct query. What I want to do is to select all the id (one of the fields of embeddedId for which some condition occurs) Here

Criteria Builder Create new Object In Select Statement

五迷三道 提交于 2019-12-04 08:06:49
问题 I was wondering if it's possible to create such query like : em.createQuery( "SELECT NEW EmpMenu(p.name, p.department.name) " + "FROM Project p ").getResultList(); also is it possible to do it via Specification: public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return ???; } Thanks in advance! 回答1: Yes, Criteria API does have have construct similar to JPQL constructor expressions. Result class is set via construct method in CriteriaBuilder. Your JPQL

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

青春壹個敷衍的年華 提交于 2019-12-04 08:02:02
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 BookRepository extends JpaRepository<Book, Long> { @Query("SELECT b FROM Book b INNER JOIN b.categories c

JPQL: What kind of objects contains a result list when querying multiple columns?

有些话、适合烂在心里 提交于 2019-12-04 07:23:30
I'm trying to do something which is easy as pie in PHP & Co: SELECT COUNT(x) as numItems, AVG(y) as average, ... FROM Z In PHP I would get a simple array like [{ numItems: 0, average: 0 }] which I could use like this: echo "Number of Items: " . $result[0]['numItems']; Usually in JPQL you only query single objects or single columns and get Lists types, for example List<SomeEntity> or List<Long> . But what do you get, when querying multiple columns? You get an Object[] (or a List<Object[]> ). From the section 4.8.1 Result Type of the SELECT Clause of the JPA 1.0 specification: The result type of

Transaction required exception on execute update for JPQL update query

六眼飞鱼酱① 提交于 2019-12-04 07:17:19
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"); setmain.setParameter("categoryId", catId); Department d; d=_entityManager.find(Department.class, deptId

JQPL Update Query to update entity without using the primary key

点点圈 提交于 2019-12-04 05:56:35
问题 This may be a simple question, but I'm trying to find out if there is a way that I can create a JPQL update query that would allow me to update a single Persisted Entity using a unique column identifier that is not the primary key. Say I have and entity like the following: @Entity public class Customer { @ID private Long id; @Column private String uniqueExternalID; @Column private String firstname; .... } Updating this entity with a Customer that has the id value set is easy, however, id like

JPA: java.lang.StackOverflowError on adding toString method in entity classes

大兔子大兔子 提交于 2019-12-04 05:17:17
Everything worked fine until I added toSting() in my entity classes. After which I start getting the following error in runtime: Exception in thread "main" java.lang.StackOverflowError at java.lang.AbstractStringBuilder.append(Unknown Source) at java.lang.StringBuilder.append(Unknown Source) at java.lang.StringBuilder.<init>(Unknown Source) at entity.Guide.toString(Guide.java:51) at java.lang.String.valueOf(Unknown Source) at java.lang.StringBuilder.append(Unknown Source) at entity.Student.toString(Student.java:45) ... @Entity public class Teacher { @Id @GeneratedValue(strategy=GenerationType

Google App Engine - DELETE JPQL Query and Cascading

廉价感情. 提交于 2019-12-04 03:57:53
I noticed that the children of PersistentUser are not deleted when using the JPQL query below. However, the children are deleted if I perform an entityManager.remove(object) . Is this expected? Why doesn't the JPQL query below also perform a cascaded delete? @OneToMany(mappedBy = "persistentUser", cascade = CascadeType.ALL) private Collection<PersistentLogin> persistentLogins; ... @Override @Transactional public final void removeUserTokens(final String username) { final Query query = entityManager.createQuery( "DELETE FROM PersistentUser p WHERE username = :username"); query.setParameter(