openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Heath Thomann (JIRA)" <>
Subject [jira] [Commented] (OPENJPA-1936) A database 'FOREIGN KEY' constraint is not detected by default
Date Tue, 05 Apr 2011 19:01:06 GMT


Heath Thomann commented on OPENJPA-1936:

I wanted to add a third option to the other two options mentioned in my description as I realize
that adding the SchemaFactory property to each persistent units/persistence.xml files might
be a pain.  As such, I wanted to offer another option which will allow a user to set the property
globally.  That is, OpenJPA offers an optional resource file named openjpa.xml to set configuration
properties, as described here:
 An example of the contents of the openjpa.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="" xmlns:xsi=""
version="1.0" xsi:schemaLocation="">

       <persistence-unit name="">
               <property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>

Notice that the persistence-unit name is blank.  This is expected, the name isn't actually
used.  As the OpenJPA document listed above points out, this file needs to be on the CLASSPATH.
 As an example (but not limited to), a user can place this file in the same location as the
persistence.xml file.  Or, a user can place the file in a .jar file and place the .jar file
in the lib directory of an ear.



> A database 'FOREIGN KEY' constraint is not detected by default
> --------------------------------------------------------------
>                 Key: OPENJPA-1936
>                 URL:
>             Project: OpenJPA
>          Issue Type: Improvement
>    Affects Versions: 2.0.1
>            Reporter: Heath Thomann
>            Assignee: Heath Thomann
>            Priority: Minor
> Take the following SQL to create two tables in a database:
>     "ID" INT NOT NULL,
>     PRIMARY KEY ("ID")
>   )
>     "ID" INT NOT NULL,
>     "PARENT_ID" INT,
>     PRIMARY KEY ("ID"),
>   )
> Take the following two entities:
> public class Parent implements Serializable {
>   @Id
>   private int id;
>   @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER,
orphanRemoval = true)
>   private Collection<Child> childs;
> .........
> public class Child implements Serializable {
>   @Id
>   private int id;
>   @ManyToOne
>   private Parent parent;
> .........
> If a scenario is executed in which an existing Parent is removed, an existing Child(s)
associated with the Parent will also be removed given the definition of the @OneToMany relationship.
 However, when OpenJPA executes the SQL to remove the Parent and Child, the SQL to remove
the Parent will be executed first.  Given the 'FOREIGN KEY' constraint on the Child table,
a database will throw some kind of 'constraint violation' exception when a Parent is removed
before its Child (if it were not for the 'FOREIGN KEY' constraint on the Child table, the
SQL order would be fine).  In this case, OpenJPA should execute the SQL to remove the Child
first, then the Parent.  However, by default, OpenJPA knows nothing about the 'FOREIGN KEY'
constraint on the Child table and OpenJPA never assumes that there are database constraints
for relationships.  As a result, OpenJPA does not  take them into account when executing SQL.
 To tell OpenJPA that there are database level constraints, and thus to effect the order of
the SQL in this case, a user can perform one of the following options:
> 1) Use the @ForeignKey annotation (org.apache.openjpa.persistence.jdbc.ForeignKey) in
entities (on the ToOne fields).
> 2) Have OpenJPA read in the table definitions from the database by adding the following
>   <property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>
> While either of these two options will properly handle the above scenario, it can be
argued that OpenJPA should detect the 'FOREIGN KEY' constraint, and not require a user to
add an annotation to their code or set a property.  This JIRA will be used to investigate
possible solutions to change the way the constraints are detected.
> Thanks,
> Heath

This message is automatically generated by JIRA.
For more information on JIRA, see:

View raw message