criteria-api

How to refer to a subclass specific field in a CriteriaQuery where the super class is queried?

帅比萌擦擦* 提交于 2019-11-30 07:37:11
I'm trying to achieve something like the following, using the JPA Criteria API: SELECT b FROM Box b JOIN SpecialItem s WHERE s.specialAttr = :specialAttr The objects are Box @Entity public class Box implements Serializable { ... @ManyToOne @JoinColumn( name = "item_id" ) Item item; ... } Item @Entity @Inheritance( strategy = InheritanceType.JOINED ) public class Item implements Serializable { @Id private String id; ... } SpecialItem @Entity public class SpecialItem extends Item { private String specialAttr; ... } My attempt EntityManager em = getEntityManager(); CriteriaBuilder cb = em

Using Java generics for JPA findAll() query with WHERE clause

点点圈 提交于 2019-11-30 06:32:43
问题 So, After a 10+ year break I'm coming back to Java and trying out stuff with JPA and Java generics. I've created a generics based findAll(other) JPA query that basically does SELECT * FROM source WHERE other_id = other.id; This is where I'm up to. It works, but I'm wondering if there's a better, cleaner way to do it. Using ManagedType was hard, and there's not much complete documentation or simple examples around. I've decided to keep my code as generic as possible (no pun intended) so I use

Spring Data JPA. How to get only a list of IDs from findAll() method

大兔子大兔子 提交于 2019-11-30 04:39:32
I have a very complicated model. Entity has a lot relationship and so on. I try to use Spring Data JPA and I prepared a repository. but when I invoke a method findAll() with specification for the object a have a performance issue because objects are very big. I know that because when I invoke a method like this: @Query(value = "select id, name from Customer ") List<Object[]> myFindCustomerIds(); I didn't have any problems with performance. But when I invoke List<Customer> findAll(); I had a big problem with performance. The problem is that I need to invoke findAll method with Specifications

Correct usage of JPA criteria API, Predicates and where method of CriteriaQuery

寵の児 提交于 2019-11-30 04:03:12
I am trying to test a JPA repository. Here is my client/test code: @Test public void testFindBoitesByMultiFieldWithIdentifiantSet() { BoiteDataOnDemand dod = new BoiteDataOnDemand(); Boite boite = dod.getSpecificBoite(0); boite.setIdentifiant("theIdentifiant"); boiteRepository.save(boite); assertEquals(10, Boite.countBoites()); BoiteQueryInfo boiteQueryInfo = new BoiteQueryInfo(); boiteQueryInfo.setIdentifiant("theIdentifiant"); List<Boite> boites = boiteRepository.findBoitesByMultiField(boiteQueryInfo, 1, 5, "identifiant", "desc"); assertEquals(1, boites.size()); } Here is the repository

Why Hibernate inlines Integer parameter list passed to JPA Criteria Query?

前提是你 提交于 2019-11-30 02:58:56
问题 I am building a query using JPA Criteria API. When I created two restriction predicates using javax.persistence.criteria.Path#in(Collection<?>) method the generated SQL query was a little bit different than I excpected. The first predicate which was build over int attribute produced SQL with all elements of parameter collection inlined: in (10, 20, 30) . The second predicate which was build over String attribute produced parametrized SQL: in (?, ?, ?) . Let me show: Entity: @Entity public

Initializing a transient attribute of a JPA entity during CriteriaQuery

老子叫甜甜 提交于 2019-11-30 02:36:43
问题 I'm wondering if it is possible to initialize a transient attribute of an entity during a criteria query. Example @Entity public SampleEntity{ @Id private long id; [more attributes] @Transient private String someTransientString; [getters and setters] } Now I want to compose a CriteriaQuery that loads all SampleEntity s and automatically sets someTransientString to imamightlyfinestring . I have something like the following SQL in mind: SELECT ID AS ID, [..], 'imamightilyfinestring' AS SOME

JPA 2 No explicit selection and an implicit one cold not be determined

ⅰ亾dé卋堺 提交于 2019-11-30 01:58:59
问题 I am trying to fetch all users for a folder where the user was created after a certain date. the relationship between the user and the folder lives in a seperate table. This is the query I came up with but it thorws the exception No explicit selection and an implicit one cold not be determined The code @Override public List<RetailPostUserTbl> getNewUsersForSiteSince( Date date, Integer siteId ) { List<RetailPostUserTbl> toReturn = new ArrayList<RetailPostUserTbl>(); EntityManager em =

truncate/delete from given the entity class

偶尔善良 提交于 2019-11-29 17:54:48
问题 I have my entity class available via a method. I'm trying to figure out, how via the JPA JPQL or Criteria API's I could issue a truncate or delete from. I think that the criteria api is more natural for working with classes, and truncate is a faster operation so these are prefered. This is what I put together so far, but not sure what to add/change about it. CriteriaBuilder cb = this._em().getCriteriaBuilder(); cb.createQuery( _entityClass() ).from( _entityClass() ); note: _entityClass

JPA Criteria Predicate Conditions

巧了我就是萌 提交于 2019-11-29 15:48:27
问题 I have the following code snippet for building criteria builder where condition. Would like to know are there any ways of making this better as I would have more where conditions and same conditions will be used for getting count of records. Any insight is highly appreciable private List <Product> getProducts(MultivaluedMap params) throws JSONException { CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery <Product> criteriaQuery = criteriaBuilder

How to find by date from timestamp column in JPA criteria?

牧云@^-^@ 提交于 2019-11-29 15:28:58
I want to find a record by date. In entity and database table, datatype is timestamp. I used Oracle database. @Entity public class Request implements Serializable { @Id private String id; @Version private long version; @Temporal(TemporalType.TIMESTAMP) @Column(name = "CREATION_DATE") private Date creationDate; public Request() { } public Request(String id, Date creationDate) { setId(id); setCreationDate(creationDate); } public String getId() { return id; } public void setId(String id) { this.id = id; } public long getVersion() { return version; } public void setVersion(long version) { this