jpql

Access field with @Transient in JPA query

♀尐吖头ヾ 提交于 2019-12-12 15:54:36
问题 I have an entity with a transient attribute: @Entity @Table(name = "asset") public class Asset { @Transient private String locationIdentifier = "N/A"; @SuppressWarnings("unused") @PostLoad private void onPostLoad() { if (location != null) { locationIdentifier = location.getIdentifier(); } } [other stuffs] } I tried to access locationIdentifier this way in JPA: String sqlString = "SELECT asset FROM Asset WHERE asset.locationIdentifier = :inputstr"; Query query = entityManager.createQuery

How do I delete a row in a join table automatically, to avoid a ConstraintViolationException?

半城伤御伤魂 提交于 2019-12-12 13:17:15
问题 This seems like it should be a pretty simple question, or at least have a simple answer. But - I'm really not a database guy, and I'm still pretty far down on the Hibernate learning curve. That said, here's the setup: Consider a unidirectional many-to-many relationship between two entities, from Foo to Bar : (pardon any typos below, the following is obviously a simplification of the actual code) FooDTO.java: @Entity @Table(name = "MyDB.dbo.Foo") class FooDTO implements Serializable { private

Spring Data and how to sort by a column not in an Entity

╄→尐↘猪︶ㄣ 提交于 2019-12-12 12:26:13
问题 I'm new to Java Spring and trying to understand how it works as I go. My goal here is to apply a sort to a JPQL query that is not in an Entity. References I've been looking at: Spring Sort Class Docs Spring Sort crash course If I declare the following query @Query("SELECT a, b.someColumn FROM TableA a INNER JOIN a.tableB b where a.search like %?1%" public Page<Object[]> findSomething(String search, Pageable pageable); This will result in a list of objects with a form of Object[0] = TableA

Spring Data JPA - How to convert Query result to entity class

自古美人都是妖i 提交于 2019-12-12 10:46:49
问题 Am using Spring Data JPA with spring boot application. I have an entity class with few properties. Consider I have 10 properties associated with the entity User and I want to retrieve only few of them (username,password,firstname,lastname,email). So I have wrote a query to get the 5 fields only, but the method does not return entity object, instead it returns a plain object. How can I cast the query result to entity in Spring Data JPA ? @Query("select userName,password,firstName,lastName

LEFT JOIN ON() in JPQL

a 夏天 提交于 2019-12-12 10:44:19
问题 I have two entities: User : id: long , name: String Player : id: long , owner: User , points: int Now I want to select a User and his associated Player in one JPQL query. In SQL I'd do it like this: SELECT u.*, p.* FROM User u LEFT JOIN Player p ON (p.owner_id = u.id) WHERE u.name = ... My first instinct was to do it like this in JPQL SELECT u, p FROM User u LEFT JOIN Player p ON (p.owner = u) WHERE u.name = ... But I don't think the ON clause is supported in JPQL. I do need it however,

Spring data JPA with dynamic where clause

牧云@^-^@ 提交于 2019-12-12 08:16:59
问题 I have a base Repository, say IBaseRepository which is public interface IBaseRepository<T extends BaseEntity<PK>, PK extends Serializable> extends JpaRepository<T, PK>, JpaSpecificationExecutor<T> { } now every repository class, for example UserRepository extends from this base repository. How can I add a general method like T findOne(String filter, Map<String, Object> params); for all inherited classes so that calling Map<String,Object> params = new HashMap<String,Object>(); params.put(

jpa constructor expressions with multiple SELECT NEW statements

◇◆丶佛笑我妖孽 提交于 2019-12-12 07:49:04
问题 Is there a way to have multiple SELECT NEW statements in a jpql query (Hibernate)? This works for me: @Query("SELECT NEW com.test.project.dto.ItemService(g,s,l,r) " +" FROM Item g, Service s, Service l , Service r" +" WHERE s.id = g.id" +" AND s.location = l.name" +" AND s.serviceType = 'type'" +" AND l.serviceType = 'Location'" +" AND l.area = r.name" +" AND r.serviceType = 'Region'") public List<Item> getAllItemsWithServices(); I get the expected Result in my DTO . @Component public class

org.hibernate.QueryException: JPA-style positional param was not an integral ordinal

让人想犯罪 __ 提交于 2019-12-12 07:17:16
问题 I have the following JPQL request; @Query(value = "select req_t " + "from TransactionRelation tr " + "inner join tr.requestTransaction req_t " + "inner join req_t.transactionStateHistory req_t_h " + "inner join tr.responseTransaction resp_t " + "inner join resp_t.transactionStateHistory resp_t_h " + "where req_t.id >?1 " + "and req_t.receiver.id=?2 and req_t.requestType in ?3" + "and NOT EXISTS (select t from resp_t_h t where t.transactionState = 'REPLIED' )" + "and EXISTS (select tt from req

OpenJPA 1.2.x select tree structure using JPQL

帅比萌擦擦* 提交于 2019-12-12 05:49:04
问题 I'm using OpenJPA 1.2.x (JPA1). The problem is that I can't get on with querying tree structure using JPQL. Please, see my entity: @NamedQueries( { @NamedQuery( name="Department.getFullTree", query="SELECT dep FROM Department dep LEFT JOIN fetch dep.children" ) } ) @Entity public class Department { public static final Long ROOT_ID = 0L; @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEPARTMENT_SEQ") @SequenceGenerator(name="DEPARTMENT_SEQ", sequenceName="DEPARTMENT_SEQ",

JPA,JPQL: unexpected token: LEFT near line 1

五迷三道 提交于 2019-12-12 05:28:57
问题 I want to display all the checklists that are not answered and not answered (response checklists is in the ResponsesCheckLists table) using the following parameters: idequipement and idMission. @Query("SELECT check,resp,eq FROM Equipements eq INNER JOIN CheckLists check WHERE eq.idEquipements = check.equipements.idEquipements LEFT JOIN ResponsesCheckLists resp ON check.idCheckLists=resp.CheckLts.idCheckLists AND resp.Respmission.idMission = :idmiss WHERE eq.idEquipements = :idEqp ") public