jpa 2.0 - JPA 2, understanding CascadeType.ALL and GenerationType.AUTO (EclipseLink 2.5) -


i having trouble understand how persist entities sub-entities when jvm has been restarted , database contains data previous sessions.

i have following entities:

@entity public class organization {     ...     @onetoone(cascade = cascadetype.all, fetch = fetchtype.eager, orphanremoval = true)     @joincolumn(name = "\"address_id\"", nullable = false)     private address address; }  @entity public class address {     ...     @id     @generatedvalue(strategy = generationtype.auto)     @column(name = "\"address_id\"")     private int addressid;     @manytoone(fetch = fetchtype.eager, cascade = cascadetype.merge, optional = false)     @joincolumn(name = "\"address_type_id\"", nullable = false)     private addresstype addresstype; }  @entity public class addresstype {     ...     // not bi-directional, nothing special here } 

it excpected address types present in database (cascadetype.merge) before creating address. new organization created new address , address has type set given selection. => works ok when there clean database (only address types present).

still developing, every , shutdown server (jvm) , restarted application. want add new organization database contains data persisted in previous sessions, following error:

exception [eclipselink-4002] (eclipse persistence services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.databaseexception internal exception: java.sql.sqlintegrityconstraintviolationexception: statement aborted because have caused duplicate key value in unique or primary key constraint or unique index identified 'sql151120084237691' defined on 'address'. error code: -20001 call: insert "address" ("address_id", "street_address", "country", "zip_code", "city", "address_type_id") values (?, ?, ?, ?, ?, ?) bind => [2, testroad 1, country, 99999, testcity, abcdef-123456]

it tries use same id exists in database. how make realize id used , should continue last?

notes:
- address persisted part of organization (cascadetype.all) not separately.
- in tests, loading existing organiztations same entitymanager persisting operation => organization has addresses accessed eagerly, should available in em-cache. duplicate address_id complains in unit tests seems orphan entity (maybe reason of error actually?).
- can error in unit tests using derby, test server using oracle db has these same errors in log.
- tried adding 'find all' query load address-entities cache of same entitymanager persisting operation of organization. 'find all' executed before persisting done => still failed.

// update
same thing happens use tablegenerator id values.

@entity public class address { ...     @id     @generatedvalue(strategy = generationtype.table, generator = "addr_gen")     @tablegenerator(name = "addr_gen", allocationsize = 1, initialvalue = 100, table = "\"address_gen\"")     @column(name = "\"address_id\"")     private int osoiteid;     ... } 

the generator table gets created, remains empty. id's start running initial value of '100'.

some more notes:
- when using self defined table , inserting value there sequence, id address-entities continues correctly value. when test finsihed, table gets emptied while there still remains data in tables => fail next time. - when using generationtype.auto, sequence table gets default sequence, after tests cleared (same thing self defined table)

^i guess has happened in test servers , can duplicated not emptying database after test. sequence table gets emptied. question be, how synchronize sequence table after jvm boot (or prevent not emptying itself)?

i not know if solution or right in general original topic, managed make kind of workaround defining sequences separately auto-generated id fields.

@id @generatedvalue(strategy = generationtype.sequence, generator = "addrseq") @sequencegenerator(name = "addrseq", sequencename = "addr_seq", allocationsize = 10) @column(name = "\"address_id\"") private int addressid; 

it seems work, though not know why behaves differently using 'auto'? normal default sequence nulled when server restarted?


Comments