jpa-2.0

How does the UserTransaction and the EntityManager interact?

旧城冷巷雨未停 提交于 2019-12-03 07:08:19
问题 This is an academic question; I have no broken code in relation to this. I just want to expand my understanding of what is happening under the hood. The code pattern I have been using (copied from books and tutorials) in my JPA DAO for my typical JSF web applications is basically this: public class someDAO { @PersistenceContext protected EntityManager em; @Resource private UserTransaction utx; public void persist(Entity entity) { try { utx.begin(); em.persist(entity); utx.commit(); } catch (

jpa lazy fetch entities over multiple levels with criteria api

蓝咒 提交于 2019-12-03 06:56:47
I am using JPA2 with it's Criteria API to select my entities from the database. The implementation is OpenJPA on WebSphere Application Server. All my entities are modeled with Fetchtype=Lazy. I select an entity with some criteria from the database and want to load all nested data from sub-tables at once. If I have a datamodel where table A is joined oneToMany to table B, I can use a Fetch-clause in my criteria query: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<A> cq = cb.createQuery(A.class); Root<A> root = cq.from(A.class); Fetch<A,B> fetch = root.fetch(A_

Is it possible to have immutable fields in Hibernate/JPA?

天大地大妈咪最大 提交于 2019-12-03 04:59:19
In our application, we need to have fields that are assignable only once. At first we thought of encapsulating the fields and making the setters private. However, some questions arouse: Without a public setter, is Hibernate still able to map the field from the database? Can I strip out the setter and make the field mutable only in the entity constructor? Finally, is there any standard JPA way to make a field immutable? Thanks in advance. Ad. 1: I believe JPA uses plain private fields for both read and write if annotations are placed on fields and not on getters. Recently I discovered that

Hibernate or EclipseLink for JPA? [closed]

最后都变了- 提交于 2019-12-03 04:20:09
I was wondering if anyone has experience with the JPA2.0 implementation of any of those frameworks? Especially together with Spring3.x which comes with EclipseLink support. Do you use any of those frameworks and JPA2.0 for production? Any severe issues? Peter Krogh IMHO It is always better to use a standard api where possible. Your own example shows this perfectly. You were able to try your identical code on two providers when one failed to work as expected. Switching to any native API prevents you from doing this. If using EclipseLink as your JPA 2.0 provider works well for you, then use it.

Some basic questions on Criteria from JPA 2.0

别来无恙 提交于 2019-12-03 03:50:28
I discovered JPA 2.0 Criteria API today and want to learn it. Just went through some examples and try to do a hands on. I have a table fruit with columns: id, name, color, size, taste. The regular stuff: EntityManagerFactory emf = Persistence.createEntityManagerFactory("fruitManager"); EntityManager em = emf.createEntityManager(); //get the criteria builder CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Fruit> c = cb.createQuery(Fruit.class); How will the following query get built using criteria: select id, name, color where name like 'XY%' and color='orange' I.e. how to: fetch

How to write this query using JPA Criteria query?

别来无恙 提交于 2019-12-03 03:33:29
Can anyone help me to get the JPA criteria query for the JPA query mentioned below. SELECT p,l FROM Person p LEFT JOIN Language l ON (p.language = l.language and l.locale like :locale) AND p.name like :name AND p.time BETWEEN :startDate AND :endDate order by name asc Assuming that Person has a relation to Language here's what you would do in older Hibernate: Criteria criteria = entityManager.createCriteria(Person.class); Criteria languageCriteria = criteria.createCriteria("language"); languageCriteria.add(Restrictions.like("locale", locale)); criteria.add(Restrictions.like("name", name));

How to implement a temporal table using JPA?

二次信任 提交于 2019-12-03 03:10:31
问题 I would like to know how to implement temporal tables in JPA 2 with EclipseLink. By temporal I mean tables who define validity period. One problem that I'm facing is that referencing tables can no longer have foreign keys constraints to the referenced tables (temporal tables) because of the nature of the referenced tables which now their primary keys include the validity period. How would I map the relationships of my entities? Would that mean that my entities can no longer have a

JPA: is @PrimaryKeyJoinColumn(…) the same as @JoinColumn(…, insertable = ?, updatable = ?)?

≡放荡痞女 提交于 2019-12-03 02:46:59
Can you derive from the JPA spec, if @PrimaryKeyJoinColumn(...) , which doesn't have the insertable and updatable parameters, is the same as @JoinColumn(..., insertable = false, updatable = false) or @JoinColumn(..., insertable = true, updatable = true) when used on regular (non-inheritance) associations? Should they be interchangable? What are the insertable and updatable properties set to? Are they set to anything at all? Note, I'm only targeting the read-only attribute that both (seem to) implement... I'm getting rather inconsistent mapping exception with EclipseLink and Hibernate... Here's

JPQL limit query [duplicate]

帅比萌擦擦* 提交于 2019-12-03 01:04:15
This question already has an answer here: Limit number of results in JPQL 2 answers How can I limit in a select query of JPQL named query? I need the limit to be done in the query level itself and not in the java layer!!! I am trying to use @NamedQueries(value = { @NamedQuery(name = UserNotification.QueryName.NOTIFICATION_DISPLAYED, query = "SELECT un FROM UserNotification un " + "WHERE un.orgId IN (:orgList) " + "AND un.user.id = :userId LIMIT 5") but in vain!!! Please suggest Kevin Bowersox JPQL does not provide a mechanism to limit queries. This is most often achieved by using the

Criteria Builder Create new Object In Select Statement

久未见 提交于 2019-12-03 00:36:17
I was wondering if it's possible to create such query like : em.createQuery( "SELECT NEW EmpMenu(p.name, p.department.name) " + "FROM Project p ").getResultList(); also is it possible to do it via Specification: public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return ???; } Thanks in advance! Yes, Criteria API does have have construct similar to JPQL constructor expressions. Result class is set via construct method in CriteriaBuilder. Your JPQL query expressed as an criteria query is: CriteriaBuilder cb... CriteriaQuery<EmpMenu> q = cb.createQuery