criteria-api

Select records which doesn't have specific records in joined table

别来无恙 提交于 2019-12-24 20:41:04
问题 Hi I'm trying to select records from one table which doesn't have records in connected many-to-many table with specific values. I will explain on sample tables: documentation: id_documentation irrelevant_data user: id_user irrelevant_data documentation_user: id_documentation id_user role What I want to achieve is to select every single documentation which doesn't have user in specific role. Any ideas? The main problem is that I'm using java's CriteriaBuilder to create query so using

Spring boot JPA & Criteria API - Select single column

寵の児 提交于 2019-12-24 18:31:09
问题 I am trying to retrieve a single column from the table, but I am getting compilation error about return type. SQL select oComment from comment where oNote = note and version > 0; I have Comment table and Note table. Comment table has comment, note and version columns. The comment itself is a note. Now I want to retrieve all comments of the note which has version greater than 0. But here I want only comment column which of note type. Comment.java @Entity @Table(name="comment") @Cache(usage

JPA: How to filter an association?

房东的猫 提交于 2019-12-24 14:32:33
问题 My model: @Entity class Person { @Id long id; @OneToMany Set<Employment> employments = new HashSet<Employment>(); } @Entity class Employment { @Id long id; @ManyToOne Company company; Date from; Date until; } It's an association between a Person and a Company that's restricted by a time interval. I'm looking for a JPA criteria query to select all Person s and their employments at a given time. The expected result is a List<Person> containing all people, where the collection employments of

Generating correct and or condition with JPA Criteria API

三世轮回 提交于 2019-12-24 07:53:30
问题 I am trying to generate query with multiple brackets in 'or' and 'and' conditions but internal brackets are not getting generated. Brackets for outer predicates are generating properly but not for internal ones. Code: CriteriaBuilder criteriaBuilder = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<ConfigurationKey> query = criteriaBuilder.createQuery(ConfigurationKey.class); Root<ConfigurationKey> configurationKeyRoot = query.from(ConfigurationKey.class); Join<ConfigurationKey,

FindBugs: How to avoid “Unwritten public field” warning when using JPA meta model?

六月ゝ 毕业季﹏ 提交于 2019-12-24 06:58:08
问题 I've written quite a lot of DAO class and using the JPA criteria API and its meta model in them, like in this example: @Override public EntityA findByEntityB(EntityB entityB) { CriteriaBuilder builder = this.getCriteriaBuilder(); CriteriaQuery<EntityA> criteriaQuery = builder.createQuery(EntityA.class); Root<EntityA> root = criteriaQuery.from(EntityA.class); criteriaQuery.select(root); criteriaQuery.where(builder.and(builder.equal(root.get(EntityA_.entityB), entityB))); return this

JPA - How to query with a LIKE operator in combination with an AttributeConverter

时光怂恿深爱的人放手 提交于 2019-12-23 17:14:58
问题 For example Customer has a field of type PhoneNumber (value object). In the persistence.xml a PhoneNumberConverter is registered which implements javax.persistence.AttributeConverter . This converter converts a PhoneNumber to string and visa versa, so the JPA provider is able to store PhoneNumbers into the database. How to query a Customer with a LIKE operator on PhoneNumber with the Criteria API? PhoneNumber can only be a valid phone number. A PhoneNumber with a value like '+31%' is not

How to do JOIN ON query using Criteria API

拥有回忆 提交于 2019-12-23 16:38:00
问题 Since version 2.1 JPA supports join on . I found few examples how to use join on in JPQL but none for Criteria API, and here is my question: Is JOIN ON is implemented in Criteria APi? And if yes, Can anyone provide example? 回答1: Try something like this CriteriaQuery<Person> crit = cb.createQuery(Person.class); Root<Person> candidateRoot = crit.from(Person.class); Join<Person, Address> addrJoin = candidateRoot.join(Person_.address, JoinType.INNER); addrJoin.on({some predicate}); filling "{some

How to conver date type in hibernate criteria

霸气de小男生 提交于 2019-12-23 15:50:31
问题 I have a situation. I have two kinds of date in the mysql database. One is date and another is datetime. Now in hibernate criteria I have to check whether one date is greater than the other or not? criteria.add(Restrictions.lt("award.deadline", "submission.date_received")); But the different types are causing problems showing "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date". Even I tried to parse it using date parser but it is not taking as date as it is

EclipseLink JPA: Can I run multiple queries from one builder?

倖福魔咒の 提交于 2019-12-23 13:13:15
问题 I have a method that builds and runs a Criteria query. The query does what I want it to, specifically it filters (and sorts) records based on user input. Also, the query size is restricted to the number of records on the screen. This is important because the data table can be potentially very large. However, if filters are applied, I want to count the number of records that would be returned if the query was not limited. So this means running two queries: one to fetch the records and then one

NullPointerException at org.hibernate.ejb.criteria.path.AbstractPathImpl.get [duplicate]

和自甴很熟 提交于 2019-12-23 10:07:54
问题 This question already has answers here : JPA/Hibernate Static Metamodel Attributes not Populated — NullPointerException (8 answers) Closed 2 years ago . This works fine: public Predicate toPredicate(Root<Campaign> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return root.get("campState").get("statusId").in(campStatus); } but I change to : return root.get(Campaign_.campState).get(CampState_.campId).in(campStatus). And it throws exception: edit for: NullPointerException at org.hibernate