openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Fay Wang (JIRA)" <>
Subject [jira] Commented: (OPENJPA-1255) InvalidStateException when using @MappedById and @JoinColumn on a unidirectional @ManyToOne relationship
Date Tue, 08 Sep 2009 21:04:57 GMT


Fay Wang commented on OPENJPA-1255:

When MappedById is used, it can represent a single value primary key in the parent. According
to JPA spec 2.0 (section 2.4):

"If the dependent entity class has a single primary key attribute (i.e, the relationship attribute)
and the primary key of the parent entity is a simple primary key, the primary key of the dependent
entity is a simple primary key of the same type as that of the parent entity (and neither
EmbeddedId nor IdClass is specified). In this case, either (1) the relationship attribute
is annotated Id, or (2) a separate
Id attribute is specified and the relationship attribute is annotated MappedById."

Your entity defintions are ok. The problem is with E1's constructor:

   public E1(E2 e, int i) {
        e2 = e;
        id = i;

and in your test case, you have:

        E2 e2 = new E2(1);
        em.persist(new E1(e2,2));
        em.persist(new E1(e2,3));

As a result,
   "new E1(e2, 2) " ==> = 1 ==> is implicitly set to 1 but is explicitly
set to 2. Similarly for "new E1(e2, 3) "

Note that E1's id is a derived id (i.e., this id is 'derived' from E2). Since the test deliberately
provides two different values for the derived id, it is proper for Openjpa to throw exception
to indicate this conflict.

The fix for the test case is to provide a constructor for E1 which will take only E2 as the

> InvalidStateException when using @MappedById and @JoinColumn on a unidirectional @ManyToOne
> --------------------------------------------------------------------------------------------------------
>                 Key: OPENJPA-1255
>                 URL:
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jdbc
>            Reporter: Rick Curtis
> I found that if I have a ManyToOne relationship using the JoinColumn and MappedById annotations
I receive the following exception:
> <openjpa-2.0.0-SNAPSHOT-r422266:804085M fatal store error> org.apache.openjpa.util.StoreException:
The transaction has been rolled back.  See the nested exceptions for details on the errors
that occurred.
> 	at org.apache.openjpa.kernel.BrokerImpl.newFlushException(
> 	at org.apache.openjpa.kernel.BrokerImpl.flush(
> .....
> 	at org.apache.openjpa.persistence.EntityManagerImpl.commit(
> 	at main.Driver.main(
> Caused by: <openjpa-2.0.0-SNAPSHOT-r422266:804085M fatal user error> org.apache.openjpa.util.InvalidStateException:
Attempt to set column "" to two different values: (class java.lang.Integer)"1", (class
java.lang.Integer)"2" This can occur when you fail to set both sides of a two-sided relation
between objects, or when you map different fields to the same column, but you do not keep
the values of these fields in synch.
> 	at org.apache.openjpa.jdbc.sql.PrimaryRow.setObject(
> 	at org.apache.openjpa.jdbc.sql.RowImpl.setInt(
> 	at org.apache.openjpa.jdbc.meta.strats.PrimitiveFieldStrategy.update(
> 	at org.apache.openjpa.jdbc.meta.strats.PrimitiveFieldStrategy.insert(
> 	at org.apache.openjpa.jdbc.meta.FieldMapping.insert(
> 	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.insert(
> 	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.populateRowManager(
> 	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(
> 	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(
> 	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(
> 	at org.apache.openjpa.kernel.DelegatingStoreManager.flush(
> 	... 8 more
> I created this problem by running the following code in a JSE environment:
>       em.getTransaction().begin();
>       E2 e2 = new E2(1);
>       em.persist(e2);
>       em.persist(new E1(e2,2));
>       em.persist(new E1(e2,3));
>       em.getTransaction().commit();
> The domain model is as follows:
> @Entity
> public class E1 {
>     @Id
>     private int id;
>     @ManyToOne(cascade = CascadeType.PERSIST)
>     @MappedById
>     @JoinColumn(name = "something_fk")
>     private E2 e2;
>     public E1(E2 e, int i) {
>         e2 = e;
>         id = i;
>     }
>     public E1() {
>     }
> }//end E1
> @Entity
> public class E2 {
>     @Id
>     private int id;
>     public E2(int i) {
>         id = i;
>     }
>     public E2() {
>     }
> }// end E2

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message