jpa-2.0

How to make a CriteriaBuilder join with a custom “on” condition?

本小妞迷上赌 提交于 2019-11-27 14:32:48
问题 I want make a query where I join 2 tables, using the CriteriaBuilder. In MySQL the query I'm trying to make would look like this: SELECT * FROM order LEFT JOIN item ON order.id = item.order_id AND item.type_id = 1 I want to get all orders and if they have an item of type #1, I want to join with this item. However, if no item of type #1 is found, I still want to get the order. I can't figure out how to make this with the CriteriaBuilder. All I know how to make is: CriteriaBuilder cb = em

EntityManager ThreadLocal pattern with JPA in JSE

▼魔方 西西 提交于 2019-11-27 14:08:50
问题 I'm developing a simple "Book Store" project using Struts 1.3 + JPA (with Hibernate as persistence provider). I cannot switch to Spring or any other more sophisticated development environment (e.g., Jboss) and I cannot use any Hibernate-specific technique (e.g., Session class). Given the fact that I'm in a JSE Environment, I need to explicitly manage the whole EntityManager's lifecycle. The Book entity is defined as follows: @Entity public class Book { @Id private String isbn; private String

JPA Select latest instance for each item

痴心易碎 提交于 2019-11-27 13:33:46
Let's say I have a Meeting entity. Each meeting has a single attendee and a meeting date. Within my meeting table I may have multiple meetings for each attendee, with different dates for each. I need a JPA query that will select only the latest meeting for all attendees. For instance, if my table looks like this Meeting ID | Attendee ID | Meeting Date 1 | 1 | 6/1/2011 2 | 2 | 6/1/2011 3 | 1 | 6/6/2011 4 | 3 | 6/6/2011 My result should be Meeting ID | Attendee ID | Meeting Date 2 | 2 | 6/1/2011 3 | 1 | 6/6/2011 4 | 3 | 6/6/2011 Using JPA 2 against postgres. Meeting has 1-1 to attendee and a

Compare Date entities in JPA Criteria API

纵饮孤独 提交于 2019-11-27 13:01:52
Using JPA 2 with EclipseLink implementation. I'm trying to build a dynamic query which should bring me some records persisted after a given date. CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Event> criteria = builder.createQuery(Event.class); Root<Event> root = criteria.from(Event.class); criteria.select(root); criteria.distinct(true); List<Predicate> predicates = new ArrayList<Predicate>(); //... if (dateLimit != null){ ParameterExpression<Date> param = builder.parameter(Date.class, "dateLimit"); predicates.add(builder.lessThanOrEqualTo(root.get("dateCreated"), param)); }

Rollback on every checked exception, whenever I say @Transactional

三世轮回 提交于 2019-11-27 11:47:32
Since the programmer is forced to catch all checked exception, I to throw checked exception in case of any problem. I would like to rollback on any of those expections. Writing rollbackFor=Exception.class on every @Transactional annotation is very error-prone, so I would like to tell spring, that: "whenever I write @Transactional , I mean @Transactional(rollbackFor=Exception.class) ". I know, that I could create a custom annotation, but that seems unnatural. So is there a way to tell spring how it should handle checked excpetions globally ? Custom Shortcut Annotations I know, that I could

How to define unidirectional OneToMany relationship in JPA

流过昼夜 提交于 2019-11-27 11:00:39
I have a following problem with entity mapping in JPA. I have two entities, first one is Lookup and the second is Text which represents translations for entities. Now I need to bound Lookup to the Text but I don't want Text to have reference to Lookup. To make this more complicated, Text does not use its primary key in this relationship but a metacode defined in a TXTHEAD_CODE column. Lookup.java @Entity @Table(name = "DATREG") public class Lookup implements PersistableEntity { @Id @Column(name = "DATREG_META_CODE") private String metaCode; @OneToMany @JoinTable(name="TXT", joinColumns=

JPA: When to choose Multivalued Association vs. Element Collection Mapping

百般思念 提交于 2019-11-27 10:46:12
问题 I would like to better understand the differences between (1) a traditional Multivalued Relationship/Association @Entity -> @OneToMany -> @Entity and (2) the JPA2 Collection of Embeddable (and basic) Types @Entity -> @ElementCollection -> @Embeddable I see the syntactical differences, but wonder whether there are also performance implications . Under the hood, the database implementation looks very similar. Intuitively, I would typically use the @ElementCollection for composition scenarios .

JPA2: Case-insensitive like matching anywhere

人走茶凉 提交于 2019-11-27 10:42:25
问题 I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike("column","keyword", MatchMode.ANYWHERE) which tests if the keyword matching the column anywhere and it is case-insensitive. Now, I am using JPA 2.0 with EclipseLink as driver so I have to use "Restrictions" build-in JPA 2.0. I found CriteriaBuilder and method like , I have also found out how to make it matching anywhere ( although it is aweful and manual ), but still I haven't figured

How to use JPA2's @Cacheable instead of Hibernate's @Cache

冷暖自知 提交于 2019-11-27 10:39:41
Typically , I use Hibernate's @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) to cache an @Entity class , and it works well. In JPA2 , there's another @Cacheable annotation that seems to be the same functionality with Hibernate's @Cache. To make my entity class independent of hibernate's package , I want to give it a try. But I cannot make it work. Each time a simple id query still hits the DB. Can anybody tell me where goes wrong ? Thanks. Entity class : @Entity //@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Cacheable(true) public class User implements

JPA Criteria Tutorial [closed]

女生的网名这么多〃 提交于 2019-11-27 10:39:00
I've been trying to find a JPA Criteria API tutorial but haven't been much successful. Do you know about any for beginners? I'd like to start using it in an Java5/Maven app to build complex search queries. The Dynamic, typesafe queries in JPA 2.0 article is a very good one on this topic, actually the best one I've found so far online , even better than the Chapter 23 Using the Criteria API to Create Queries from the Java EE 6 tutorials (that contains some mistakes). Examples of common queries are here All examples are in this form: CriteriaBuilder cb = em.getCriteriaBuilder(); // Query for a