Return-Path: Delivered-To: apmail-openjpa-dev-archive@www.apache.org Received: (qmail 96413 invoked from network); 8 Sep 2009 21:05:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 8 Sep 2009 21:05:23 -0000 Received: (qmail 23624 invoked by uid 500); 8 Sep 2009 21:05:23 -0000 Delivered-To: apmail-openjpa-dev-archive@openjpa.apache.org Received: (qmail 23547 invoked by uid 500); 8 Sep 2009 21:05:23 -0000 Mailing-List: contact dev-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openjpa.apache.org Delivered-To: mailing list dev@openjpa.apache.org Received: (qmail 23534 invoked by uid 99); 8 Sep 2009 21:05:23 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Sep 2009 21:05:23 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 08 Sep 2009 21:05:19 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 7F386234C044 for ; Tue, 8 Sep 2009 14:04:57 -0700 (PDT) Message-ID: <1394798165.1252443897516.JavaMail.jira@brutus> Date: Tue, 8 Sep 2009 14:04:57 -0700 (PDT) From: "Fay Wang (JIRA)" To: dev@openjpa.apache.org Subject: [jira] Commented: (OPENJPA-1255) InvalidStateException when using @MappedById and @JoinColumn on a unidirectional @ManyToOne relationship In-Reply-To: <1764877085.1250708174844.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/OPENJPA-1255?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12752742#action_12752742 ] 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(e2); em.persist(new E1(e2,2)); em.persist(new E1(e2,3)); As a result, "new E1(e2, 2) " ==> e2.id = 1 ==> e1.id is implicitly set to 1 but e1.id 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 parameter. > InvalidStateException when using @MappedById and @JoinColumn on a unidirectional @ManyToOne relationship > -------------------------------------------------------------------------------------------------------- > > Key: OPENJPA-1255 > URL: https://issues.apache.org/jira/browse/OPENJPA-1255 > 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: > 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(BrokerImpl.java:2292) > at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2140) > ..... > at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:553) > at main.Driver.main(Driver.java:67) > Caused by: org.apache.openjpa.util.InvalidStateException: Attempt to set column "E1.id" 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(PrimaryRow.java:344) > at org.apache.openjpa.jdbc.sql.RowImpl.setInt(RowImpl.java:437) > at org.apache.openjpa.jdbc.meta.strats.PrimitiveFieldStrategy.update(PrimitiveFieldStrategy.java:159) > at org.apache.openjpa.jdbc.meta.strats.PrimitiveFieldStrategy.insert(PrimitiveFieldStrategy.java:119) > at org.apache.openjpa.jdbc.meta.FieldMapping.insert(FieldMapping.java:608) > at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.insert(AbstractUpdateManager.java:230) > at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.populateRowManager(AbstractUpdateManager.java:162) > at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:95) > at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:76) > at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:675) > at org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:130) > ... 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.