You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Benjamin S Vera-Tudela <bv...@us.ibm.com> on 2009/10/09 18:41:16 UTC

OpenJPA Relationships Question


Hi ....

I have a servlet that handles incoming HTTP requests and uses information
in the request to create a persistent relationship between 2 classes using
JPA. The database schema has 3 tables: Class1, Class2, and Relationship
(see MyDataAppRDB), where Relationship's primary key consists of the
primary keys of both Class1 and Class2 which are also referenced as foreign
keys. The servlet invokes a method called createRelationship (see
MyDataAppWeb) which starts a transaction through the EntityManager to
create a Class2 object if one isn't found in the database and to create a
relationship between Class1 (assumed to exist already) and Class2.

I am having a problem with the createRelationship method at the time of
committing the transaction where the database (DB2) throws error 23503 (see
below for error details).

I am not sure if the problem is related to 1) the order of my persist calls
within the transaction (see TransactionManager class in MyDataAppRDB); 2)
the contents of my persistence.xml file (see MyDataAppJPA -- I am not yet
very familiar with the values in the persistence.xml  schema); or 3) the
way I implement my JPA classes (see MyDataJPA -- in particular the
Relationship class which contains a composite key that references foreign
keys).)

Can someone please help me understand what I might be doing wrong? I am
attaching my simplified files below.

DB2 Error 23503:

An UPDATE or INSERT operation attempted to place a value in a foreign key
of the object table; however, this value was not equal to some value of the
parent key of the parent table. When a row is inserted into a dependent
table, the insert value of a foreign key must be equal to the value of the
parent key of some row of the parent table in the associated relationship.
When the value of the foreign key is updated, the update value of a foreign
key must be equal to the value of the parent key of some row of the parent
table of the associated relationship. System action: The UPDATE or INSERT
statement cannot be executed. The object table is unchanged. Programmer
response: Examine the insert or update value of the foreign key first, and
then compare it with each of the parent key values of the parent table to
determine the cause of the problem.


(See attached file: jpa_rel.zip)

Benjamin S. Vera-Tudela
SWG Middleware Strategy
Ph: (512)-286-9073 T/L 363-9073
E-Mail: bveratudela@us.ibm.com

Re: OpenJPA Relationships Question

Posted by Daryl Stultz <da...@6degrees.com>.
On Fri, Oct 9, 2009 at 12:53 PM, Benjamin S Vera-Tudela <
bveratudela@us.ibm.com> wrote:

> @Daryl,
>
> Yes, it works sometimes (very seldom). In my code I create Class2 first and
> then create the Relationship. I suppose you suggest I do it the other way
> around? I will try that.
>
Within the transaction it doesn't matter what order. All the queries are
generated and issued when the transaction is committed.

>
> Also, the @ForeignKey annotation should go in the Relationship class right?
> Let me know. I can make changes now.
>
> I put @ForeignKey after every @JoinColumn (which most frequently appears
after @ManyToOne). (It should go in every class on every field that uses a
foreign key to join.)

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: OpenJPA Relationships Question

Posted by Benjamin S Vera-Tudela <bv...@us.ibm.com>.
@Daryl,

Yes, it works sometimes (very seldom). In my code I create Class2 first and
then create the Relationship. I suppose you suggest I do it the other way
around? I will try that.

Also, the @ForeignKey annotation should go in the Relationship class right?
Let me know. I can make changes now.

Thanks,.



Benjamin S. Vera-Tudela
SWG Middleware Strategy
Ph: (512)-286-9073 T/L 363-9073
E-Mail: bveratudela@us.ibm.com


|------------>
| From:      |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Daryl Stultz <da...@6degrees.com>                                                                                                                 |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| To:        |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |users@openjpa.apache.org                                                                                                                          |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Date:      |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |10/09/2009 11:49 AM                                                                                                                               |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Subject:   |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Re: OpenJPA Relationships Question                                                                                                                |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|





On Fri, Oct 9, 2009 at 12:41 PM, Benjamin S Vera-Tudela <
bveratudela@us.ibm.com> wrote:

> Hi ....
>
> I am having a problem with the createRelationship method at the time of
> committing the transaction where the database (DB2) throws error 23503
(see
> below for error details).
>
> I am not sure if the problem is related to 1) the order of my persist
calls
> within the transaction (see TransactionManager class in MyDataAppRDB);
>
I don't believe this matters, queries are issued at commit time. I didn't
fully analyze your case but it looks like the child row is being inserted
before the parent. Does it happen every time? If it succeeds once in a
while, it's a query ordering issue. You might try annotating the FK
columns/fields with @ForeignKey.

--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com:


Re: OpenJPA Relationships Question

Posted by Daryl Stultz <da...@6degrees.com>.
On Fri, Oct 9, 2009 at 12:41 PM, Benjamin S Vera-Tudela <
bveratudela@us.ibm.com> wrote:

> Hi ....
>
> I am having a problem with the createRelationship method at the time of
> committing the transaction where the database (DB2) throws error 23503 (see
> below for error details).
>
> I am not sure if the problem is related to 1) the order of my persist calls
> within the transaction (see TransactionManager class in MyDataAppRDB);
>
I don't believe this matters, queries are issued at commit time. I didn't
fully analyze your case but it looks like the child row is being inserted
before the parent. Does it happen every time? If it succeeds once in a
while, it's a query ordering issue. You might try annotating the FK
columns/fields with @ForeignKey.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com