jpa-2.0

javax.persistence.PersistenceException - JPA+Hibernate

杀马特。学长 韩版系。学妹 提交于 2019-11-27 23:23:20
I am new to JPA, when I tried to run the following code, it showing error as " cvc-elt.1: Cannot find the declaration of element 'persistence'. " I cant able to fix this error, could u pls help me out to solve this issue. Cheers Rajesh Persistance.xml <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="jpa" transaction-type="JTA">

How to countDistinct on multiple columns

邮差的信 提交于 2019-11-27 23:02:46
How can I, using the JPA criteria API do the following: select count(distinct column1, column2) from table Doing this on one column/path is simple using CriteriaBuilder.countDistinct, but how can I do this on two paths/columns? ryenus Here is a late answer :-) though I'm not sure if things had changed. Recently I encountered the very same need, and worked around it using concat, i.e., by concatenating the columns into a pseudo column , then countDistinct on the pseudo column. But I couldn't use criteriaBuilder.concat because it generated JPQL using || for the concatenation, which Hibernate had

JPA2 Criteria-API: select… in (select from where)

浪子不回头ぞ 提交于 2019-11-27 22:43:51
问题 I have the following database model: A aId AB aId bId B bId status In a Spring data Specification, I want to return the instances of A when B.status is 'X'. The JPQL code is the following: select a from A a where a in (select ab.id.a from AB ab where ab.id.b.status= :status) These are the model classes: @Entity public class A { private Long aId; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.a") private Set<AB> ab; } @Entity public class B { private Long bId;

How to create specification using JpaSpecificationExecutor by combining tables?

女生的网名这么多〃 提交于 2019-11-27 22:23:40
问题 I am using JpaSpecificationExecutor for creating custom queries. How do I create a Specification for the following SQL? select * from employee e, address a where e.id=23415 and e.name="Deepak" and a.city="Leeds"; Java Class : public static Specification<Employee> searchEmployee(final Map<String,String> myMap) { return new Specification<Employee>(){ @Override public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) { //Need to query two tables Employee and

Change Table Name of an Entity on runtime?

只谈情不闲聊 提交于 2019-11-27 22:00:53
There is this table that is being generated on a monthly basis. Basically the table structure of all monthly tables is the same. Since it would be a lot of work to map the same entity just with a different table name, Is it possible to change the table name of an entity as follows on runtime since they have the same table structure after all? @Entity @Table(name="FOO_JAN2010") // any other ways to generate this dynamically? public class FooJan2010Table { // if we can dynamically set the table name this can be simply named FooTable ... } If not, what approach can you suggest? Pascal Thivent Is

How to query an M:N relationship with JPA2?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 21:40:42
I have an an object (BlogPost) that contains an M:N collection of elements (Tags). How to query for an object (BlogPost) where at least one it its Tags matches an element in a set of Tags (defined by the user) with JPA2 (Hibernate). findBlogPostWithAtLeastOneMatchingTag(Collection<Tag> tags){ ???? } My main problem is, that I actually need to compare two collections of tags: - the collection of tags of the BlogPost. - the collection I search for I tried Select p from Post p where p.tags in(:tags) but it does not work, as my post entities have more than just one tag. So what could I do instead?

JPA - Criteria API and EmbeddedId

三世轮回 提交于 2019-11-27 21:08:52
I want to use criteria to make the following query. I have an Entity with EmbeddedId defined: @Entity @Table(name="TB_INTERFASES") public class Interfase implements Serializable { @EmbeddedId private InterfaseId id; } @Embeddable public class InterfaseId implements Serializable { @Column(name="CLASE") private String clase; } And the criteria query that i am trying to do is: CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery<Interfase> criteriaQuery = criteriaBuilder.createQuery(Interfase.class); Root<Interfase> entity = criteriaQuery.from(Interfase.class);

Triggers versus JPA Event

你离开我真会死。 提交于 2019-11-27 20:31:50
问题 I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider, MySql 5.1.x. The application runs on Tomcat 7.X. In my entities I have some date like last update date: @Column(name = "last_update_date", insertable = false, updatable = false) @Temporal(TemporalType.TIMESTAMP) private Date lastUpdateDate; For the moment I have a trigger that updates: CREATE TRIGGER upd_site BEFORE UPDATE ON site FOR EACH ROW SET NEW.last_update_date = CURRENT_TIMESTAMP(); It

Spring Data JPA: How can Query return Non- Entities Objects or List of Objects?

拜拜、爱过 提交于 2019-11-27 20:25:02
I am using spring data JPA in my project. I am playing with millions of records. I have a requirement where I have to fetch data for various tables and build a object and then paint it on a UI. Now how to achieve this my Spring data Repositories. I have read that it can be achieved by Named native queries. If the named native query does not return an entity or a list of entities, we can map the query result to a correct return type by using the @SqlResultSetMapping annotation. But when I am trying to use @SqlResultSetMapping it is taking another entityResult . Mean what I understand is that it

What are some of the real world example where JPA2 Criteria API is more preferable?

£可爱£侵袭症+ 提交于 2019-11-27 20:19:27
问题 I have taken a look at JPA 2.0 Criteria API, but I found it to be too cumbersome unlike Hibernate Criteria. Is there any good reason to use JPA 2.0 Criteria API rather than using JPA-QL? Thanks for your advise. 回答1: Like the Hibernate Criteria API, the JPA 2.0 Criteria API is especially nice to build queries dynamically , to handle cases where the query structure varies depending upon runtime conditions. But there is more. While being more verbose than Hibernate's Criteria API, the JPA