users@glassfish.java.net

Problem with simple Entity cascade delete

From: <glassfish_at_javadesktop.org>
Date: Thu, 27 Sep 2007 17:43:14 PDT

I have 3 entity class: Device, Hardware, Software. Device is an abstract entity class, Hardware is a concrete entity class that extends Device. Software is a simple entity class. Hardware maintains a one-to-many collection of Software entities.

I just created a test application with the 3 entity classes that have nothing more than the Id attribute that is auto-generated. I left all of the annotations with default values except the Hardware has the annotation: @OneToMan(cascade={CascadeType.PERSIST, CascadeType.REMOVE}) on Collection<Software>software attribute.

I have the persistence context perform a "drop and create" to create the database schema. I'm using JavaDB (derby).

I create one Hardware entity, one Software entity, add the Software entity to the Hardware entity software collection and persist the Hardware entity. This works correctly in that both the Hardware and Software entities are persisted to the database.

I then find the Hardware entity by Id, and remove the Hardware entity using the entity manager. Here is the code:

[i] EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication1PU");
        EntityManager entityManager = emf.createEntityManager();
        
        EntityTransaction tr = entityManager.getTransaction();
        tr.begin();
        
        Hardware h = new Hardware();
        Software s = new Software();
        h.getSoftware().add(s);
        
        entityManager.persist(h);
        
        tr.commit();
        
        tr.begin();
        
        h = entityManager.find(Hardware.class, h.getId());
        entityManager.remove(h);
        
        tr.commit();
[/i]



Note that I did not remove the Software entity from the Hardware entity's software collection. This causes a problem. The error that I get is:

[i][TopLink Finer]: 2007.09.27 08:22:33.970--ClientSession(421988)--Connection(3932167)--Thread(Thread[main,5,main])--rollback transaction
[TopLink Finer]: 2007.09.27 08:22:33.971--UnitOfWork(4126736)--Thread(Thread[main,5,main])--release unit of work
[TopLink Finer]: 2007.09.27 08:22:33.971--UnitOfWork(4126736)--Thread(Thread[main,5,main])--initialize identitymaps
[TopLink Finer]: 2007.09.27 08:22:33.971--ClientSession(421988)--Thread(Thread[main,5,main])--client released
Caused by: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: DELETE on table 'SOFTWARE' caused a violation of foreign key constraint 'DVCSFTWARESFTWREID' for key (3). The statement has been rolled back.
Error Code: -1
Call: DELETE FROM SOFTWARE WHERE (ID = ?)
        bind => [3]
Query: DeleteObjectQuery(javaapplication1.Software_at_da3a1e)
[/i]


So I'm very confused on why this simple application is getting this violation. This is complete defaulted so Toplink is reponsible for the schema which generates a join table, but Toplink is not generating the SQL to remove the row from the join table before trying to delete the row from the SOFTWARE table.

This is with glassfish-v2-b58g
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=237488