jpa-2.0

JPA: unidirectional many-to-one and cascading delete

心已入冬 提交于 2019-11-26 17:10:51
Say I have a unidirectional @ManyToOne relationship like the following: @Entity public class Parent implements Serializable { @Id @GeneratedValue private long id; } @Entity public class Child implements Serializable { @Id @GeneratedValue private long id; @ManyToOne @JoinColumn private Parent parent; } If I have a parent P and children C 1 ...C n referencing back to P, is there a clean and pretty way in JPA to automatically remove the children C 1 ...C n when P is removed (i.e. entityManager.remove(P) )? What I'm looking for is a functionality similar to ON DELETE CASCADE in SQL. Vineet

Persist java.time.Instant (JDK8) with JPA2/Hibernate

孤街浪徒 提交于 2019-11-26 16:59:21
问题 Neither JPA nor Hibernate currently support the new date/time classes brought by JSR-310 in JDK8 (JPA ticket, Hibernate ticket). Nonetheless, I'd like to code with the JDK8 date/time classes as they are finally well designed. In particular, I'm interested in java.time.Instant , not in full support for all java.time.* types, as all my entities will use this particular class (or so I think now, at least :-) One option is to write a type converter, as defined by JPA 2.1. However, our app server

JPA 2.0 orphanRemoval=true VS on delete Cascade

不羁岁月 提交于 2019-11-26 16:58:14
I am a little confused about the JPA 2.0 orphanRemoval attribute. I think I can see its is needed when I use my JPA provider's DB generation tools to create the underlying database DDL to have an ON DELETE CASCADE on the particular relation. However, if the DB exists and it already has an ON DELETE CASCADE on the relation, is this not enough to cascade the deletion appropriately? What does the orphanRemoval do in addition? Cheers axtavt orphanRemoval has nothing to do with ON DELETE CASCADE . orphanRemoval is an entirely ORM-specific thing . It marks "child" entity to be removed when it's no

JPA Select latest instance for each item

你。 提交于 2019-11-26 16:24:15
问题 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

Compare Date entities in JPA Criteria API

孤者浪人 提交于 2019-11-26 16:13:03
问题 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

Hibernate - @ElementCollection - Strange delete/insert behavior

强颜欢笑 提交于 2019-11-26 16:11:02
@Entity public class Person { @ElementCollection @CollectionTable(name = "PERSON_LOCATIONS", joinColumns = @JoinColumn(name = "PERSON_ID")) private List<Location> locations; [...] } @Embeddable public class Location { [...] } Given the following class structure, when I try to add a new location to the list of Person's Locations, it always results in the following SQL queries: DELETE FROM PERSON_LOCATIONS WHERE PERSON_ID = :idOfPerson And A lotsa' inserts into the PERSON_LOCATIONS table Hibernate (3.5.x / JPA 2) deletes all associated records for the given Person and re-inserts all previous

Rollback on every checked exception, whenever I say @Transactional

好久不见. 提交于 2019-11-26 15:45:42
问题 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 to define unidirectional OneToMany relationship in JPA

守給你的承諾、 提交于 2019-11-26 15:23:25
问题 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

How to use JPA2&#39;s @Cacheable instead of Hibernate&#39;s @Cache

浪尽此生 提交于 2019-11-26 15:15:34
问题 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

JPA Criteria API - How to add JOIN clause (as general sentence as possible)

泄露秘密 提交于 2019-11-26 12:55:40
问题 I am trying to construct queries dynamically, and my next target is add JOIN clauses (I don\'t know how can I use the API). By now, for example, this code work for me : ... Class baseClass; ... CriteriaBuilder cb = JpaHandle.get().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(this.baseClass); Root entity_ = cq.from(this.baseClass); Predicate restrictions = null; ... restrictions = cb.conjunction(); restrictions = cb.and(restrictions, entity_.get(\"id\").in(this.listId)); ... cq