Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 326439 - Inheritance Insert Misses Second table on MySQL
Summary: Inheritance Insert Misses Second table on MySQL
Status: CLOSED INVALID
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-28 13:08 EDT by S. Kalynuk CLA
Modified: 2022-06-09 10:31 EDT (History)
2 users (show)

See Also:


Attachments
Test case (Maven project) (9.12 KB, application/x-download)
2010-09-28 13:09 EDT, S. Kalynuk CLA
no flags Details
Output log for EclipseLink 2.1.1 with error (16.05 KB, application/octet-stream)
2010-09-28 13:11 EDT, S. Kalynuk CLA
no flags Details
Output log using EclipseLink 2.0.2 (successful) (8.95 KB, application/octet-stream)
2010-09-28 13:13 EDT, S. Kalynuk CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description S. Kalynuk CLA 2010-09-28 13:08:13 EDT
Build Identifier: 2.1.1.v20100817-r8050

Environment:
* Fedora 13
* Java 1.6.0_21 32-bit
* EclipseLink 2.1.1.v20100817-r8050
* MySQL server 5.0.88
* Connector/J 5.1.12

Data model:

ClassA1 - Abstract, InheritanceType.JOINED
ClassA2 - Sub-classed from ClassA1, OneToMany mapping (CascadeType.ALL) to ClassB2 (List<ClassB2>)

ClassB1 - Abstract, InheritanceType.JOINED
ClassB2 - Sub-classed from ClassB1, ManyToOne mapping to ClassA2

In the database, a foreign key constraint exists in the ClassB2 table on the ClassA2 table.

Test case:
1. ClassA2 instance is constructed
2. ClassB2 instance added as child to ClassA2 instance
3. Persist ClassA2

Cascade persist of ClassB2 instance fails because the persist order is:
1. ClassA1
2. ClassB1
3. ClassB2 (Error: ClassA2 not persisted yet - foreign key constraint violation)

The correct persist order should be:
1. ClassA1
2. ClassA2
3. ClassB1
4. ClassB2

This bug exists in 2.1.0 and 2.1.1. The bug does not exist in any previous version.

Reproducible: Always

Steps to Reproduce:
1. Unpack the attached EclipseLinkBugTest3.zip test case
2. Create the tables in a local MySQL 5.x database using the following script:
   EclipseLinkBugTest3/src/main/resources/create-schema.sql
3. Edit EclipseLinkBugTest3/src/main/resources/META-INF/persistence.xml and change the following properties to connect to a local MySQL 5.x database:
   * javax.persistence.jdbc.url
   * javax.persistence.jdbc.user
   * javax.persistence.jdbc.password
4. cd EclipseLinkBugTest3
5. Compile and run with Maven 2.2 (and change java path accordinly). Example:
   mvn -Dexec.classpathScope=runtime -Dexec.args="-classpath %classpath ca.avant.test.App" -Dexec.executable=/usr/local/java/1.6.0_21/bin/java process-classes org.codehaus.mojo:exec-maven-plugin:1.1.1:exec
Comment 1 S. Kalynuk CLA 2010-09-28 13:09:57 EDT
Created attachment 179766 [details]
Test case (Maven project)

A complete test case as a Maven project.
Comment 2 S. Kalynuk CLA 2010-09-28 13:11:30 EDT
Created attachment 179767 [details]
Output log for EclipseLink 2.1.1 with error

A log file showing the error with EclipseLink 2.1.1.
Comment 3 S. Kalynuk CLA 2010-09-28 13:13:24 EDT
Created attachment 179768 [details]
Output log using EclipseLink 2.0.2 (successful)

The output log on the same test case using EclipseLink 2.0.2. The test case works OK.
Comment 4 Tom Ware CLA 2010-10-07 14:58:39 EDT
Targetting for 2.1.3 - the next open patch.  Assuming this is actually a regression, this should be fixed for the next patch.
Comment 5 Tom Ware CLA 2010-11-19 09:45:39 EST
This appears to be an issue with the constraints in the DDL you have provided.  When I run with EclipseLink-created DDL (that also includes constraints), everything works fine.  The EclipseLink DDL creates the constraint between B2 and A1 instead.  That is how your DDL should be constructed.

There are ways to manually alter the commit order if you want, but you should be very careful about using them to ensure the new order works for all use cases in your application.
Comment 6 S. Kalynuk CLA 2010-11-19 14:44:12 EST
(In reply to comment #5)
> This appears to be an issue with the constraints in the DDL you have provided. 
> When I run with EclipseLink-created DDL (that also includes constraints),
> everything works fine.  The EclipseLink DDL creates the constraint between B2
> and A1 instead.  That is how your DDL should be constructed.
> 
> There are ways to manually alter the commit order if you want, but you should
> be very careful about using them to ensure the new order works for all use
> cases in your application.

I agree that your suggestion makes sense in the context of the test case, but
the more complete data model our software uses is more like this:

     A1                       B1
    / \                      / \
   /   A2.2  <----FK---- B2.2   \
  /                              \
A2.1 <------------FK----------- B2.1

A2.1 and A2.2 both sub-class A1
B2.1 and B2.2 both sub-class B1

A2.1 can only have a collection of B2.1 entities
A2.2 can only have a collection of B2.2 entities

If both B2.1 and B2.2 had foreign key references to A1, then the DB constraints
can theoretically allow a one-to-many relationship from A2.2 to B2.1, or A2.1
to B2.2. This is actually disallowed in the way we defined the JPA entity 
relationships, but we attempted to have the DB constraints reflect the entity 
model exactly.

I would expect EclipseLink to persist A1, A2.1 (or A2.2), B1, and then B2.1
(or B2.2). It works with EclipseLink 1.1.x and 2.0.x, so why does it no longer 
work with EclipseLink 2.1.x?
Comment 7 Tom Ware CLA 2010-11-19 15:22:24 EST
This issue has nothing to do with Commit Order.  Our Commit order is correct in all versions.  We seem to be missing an Insert into the A2 table.  I wonder if this is related to some feature interaction with MySQL native sequencing.  I'll take another look.
Comment 8 Tom Ware CLA 2010-11-22 09:58:31 EST
This change in behavior is caused by the fix for bug 310015 and bug 310505.

The issue here is that the DB has constraints the EclipseLink does not know about.  EclipseLink cannot properly calculate which statements it can defer without knowing that information.

Your should be able to address this issue in a DescriptorCustomizer.

In the DescriptorCustomizer, you can do one of the following

1. change the mapping so that the foreign reference points to the subclass table - In JPA we default the mapping to point to the primary table.

2. call descriptor.setHasMultipleTableConstraintDependecy(true) on the child descriptor
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:31:54 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink