Reading Java Persistence API spec

Let's look at the following section of the spec:

3.2.3 Synchronization to the Database


The semantics of the flush operation, applied to an entity X are as follows:

  • If X is a managed entity, it is synchronized to the database.
    ...

    • For any entity Y referenced by a relationship from X, where the relationship to Y has
      not been annotated with the cascade element value cascade=PERSIST or cascade=ALL:

      • If Y is new or removed, an IllegalStateException will be thrown by
        the flush operation (and the transaction rolled back) or the transaction commit
        will fail.




The last bullet is an interesting requirement. Please read it carefully (and may be more than once...).


This requirement means that if you have in a transaction entities X and Y with relationship between them, and you remove an entity Y, you must nullify all references to Y, or your transaction will fail. This is true even if Y owns the relationship (i.e. if FK column belongs to the table that Y is mapped to).


It also means that if you have such relationship marked with cascade=ALL, the removal of Y will be reverted (!) - see section 3.2.1 Persisting an Entity Instance on semantics of the persist operation, applied to a removed instance.


Food for thought... Let me know if you need more detailed explanation :)

Comments:

Post a Comment:
Comments are closed for this entry.
About

Marina Vatkina

Search

Top Tags
Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News

No bookmarks in folder

Blogroll
Related Links