Return-Path: X-Original-To: apmail-openjpa-commits-archive@www.apache.org Delivered-To: apmail-openjpa-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E77699DC3 for ; Mon, 4 Jun 2012 18:18:32 +0000 (UTC) Received: (qmail 51932 invoked by uid 500); 4 Jun 2012 18:18:32 -0000 Delivered-To: apmail-openjpa-commits-archive@openjpa.apache.org Received: (qmail 51877 invoked by uid 500); 4 Jun 2012 18:18:32 -0000 Mailing-List: contact commits-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 commits@openjpa.apache.org Received: (qmail 51861 invoked by uid 99); 4 Jun 2012 18:18:32 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jun 2012 18:18:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jun 2012 18:18:27 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B10F823888CD for ; Mon, 4 Jun 2012 18:18:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1346073 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/ openjpa-pe... Date: Mon, 04 Jun 2012 18:18:05 -0000 To: commits@openjpa.apache.org From: dianner@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120604181805.B10F823888CD@eris.apache.org> Author: dianner Date: Mon Jun 4 18:18:04 2012 New Revision: 1346073 URL: http://svn.apache.org/viewvc?rev=1346073&view=rev Log: OPENJPA-2175 Fix problem with multiple foreign keys of the same type Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ContainsMultSimpleEntity.java (with props) openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/SimpleEntity.java (with props) openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TestMultForeignKeys.java (with props) openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TopLevel.java (with props) Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMapping.java openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerCollectionTableFieldStrategy.java Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java?rev=1346073&r1=1346072&r2=1346073&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java Mon Jun 4 18:18:04 2012 @@ -1122,6 +1122,11 @@ public class FieldMapping throws SQLException { _val.setForeignKey(row, sm); } + + public void setForeignKey(Row row, OpenJPAStateManager sm, int targetNumber) + throws SQLException { + _val.setForeignKey(row, sm, targetNumber); + } public void whereForeignKey(Row row, OpenJPAStateManager sm) throws SQLException { Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMapping.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMapping.java?rev=1346073&r1=1346072&r2=1346073&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMapping.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMapping.java Mon Jun 4 18:18:04 2012 @@ -178,6 +178,14 @@ public interface ValueMapping */ public void setForeignKey(Row row, OpenJPAStateManager rel) throws SQLException; + + /** + * Sets this value's foreign key to the given related object. The object + * may be null. If the object is one of2or more foreign keys with the + * same target, the targetNumber specifies the one to set. + */ + public void setForeignKey(Row row, OpenJPAStateManager rel, int targetNumber) + throws SQLException; /** * Sets this value's foreign key to the given related object. The object Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java?rev=1346073&r1=1346072&r2=1346073&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java Mon Jun 4 18:18:04 2012 @@ -159,7 +159,7 @@ public class ValueMappingImpl _join = JOIN_FORWARD; } - public ForeignKey getForeignKey(ClassMapping target) { + public ForeignKey getForeignKey(ClassMapping target, int targetNumber) { if (_fk == null && getValueMappedBy() != null) return getValueMappedByMapping().getForeignKey(target); if (target == null) @@ -167,10 +167,14 @@ public class ValueMappingImpl ClassMapping embeddedMeta = (ClassMapping)getEmbeddedMetaData(); if (embeddedMeta != null) { FieldMapping[] fields = embeddedMeta.getFieldMappings(); + int j = 0; for (int i = 0; i < fields.length; i++) { ValueMapping val = fields[i].getValueMapping(); if (val.getDeclaredTypeMapping() == target) + if (targetNumber == j) return val.getForeignKey(); + else + j++; } } if (_fk == null && _cols.length == 0) @@ -198,7 +202,9 @@ public class ValueMappingImpl return newfk; } } - + public ForeignKey getForeignKey(ClassMapping target) { + return getForeignKey(target, 0); + } /** * Create a forward foreign key to the given target. */ @@ -323,11 +329,12 @@ public class ValueMappingImpl _join = direction; } - public void setForeignKey(Row row, OpenJPAStateManager rel) + public void setForeignKey(Row row, OpenJPAStateManager rel, int targetNumber) throws SQLException { - if (rel != null) - row.setForeignKey(getForeignKey((ClassMapping) rel.getMetaData()), + if (rel != null) { + row.setForeignKey(getForeignKey((ClassMapping) rel.getMetaData(), targetNumber), _io, rel); + } else if (_fk != null) row.setForeignKey(_fk, _io, null); else { @@ -340,6 +347,10 @@ public class ValueMappingImpl } } } + public void setForeignKey(Row row, OpenJPAStateManager rel) + throws SQLException { + setForeignKey(row, rel, 0); + } public void whereForeignKey(Row row, OpenJPAStateManager rel) throws SQLException { Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerCollectionTableFieldStrategy.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerCollectionTableFieldStrategy.java?rev=1346073&r1=1346072&r2=1346073&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerCollectionTableFieldStrategy.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/HandlerCollectionTableFieldStrategy.java Mon Jun 4 18:18:04 2012 @@ -161,8 +161,17 @@ public class HandlerCollectionTableField Collection rels = new ArrayList(); if (isEmbedded) { getRelations(esm, rels, ctx); + Map targets = new HashMap(); for (Object rel : rels) { - elem.setForeignKey(row, (StateManagerImpl)rel); + StateManagerImpl relSm = (StateManagerImpl)rel; + ClassMapping cm =(ClassMapping) relSm.getMetaData(); + if(!targets.containsKey(cm)){ + targets.put(cm, 0); + } + Integer n = targets.get(cm); + elem.setForeignKey(row, (StateManagerImpl)rel, n); + n++; + targets.put(cm, n); } } } Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ContainsMultSimpleEntity.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ContainsMultSimpleEntity.java?rev=1346073&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ContainsMultSimpleEntity.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ContainsMultSimpleEntity.java Mon Jun 4 18:18:04 2012 @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.foreign.keys; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Embeddable; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Embeddable +public class ContainsMultSimpleEntity implements Serializable { + + private static final long serialVersionUID = -8576236113079133657L; + + @ManyToOne(cascade=CascadeType.ALL) + @JoinColumn(nullable=false) + private SimpleEntity simpleEntity1; + + @ManyToOne(cascade=CascadeType.ALL) + @JoinColumn(nullable=false) + private SimpleEntity simpleEntity2; + + @ManyToOne(cascade=CascadeType.ALL) + @JoinColumn(nullable=false) + private SimpleEntity simpleEntity3; + + public SimpleEntity getSimpleEntity1() { + return simpleEntity1; + } + + public void setSimpleEntity1(SimpleEntity simpleEntity1) { + this.simpleEntity1 = simpleEntity1; + } + + public SimpleEntity getSimpleEntity2() { + return simpleEntity2; + } + + public void setSimpleEntity2(SimpleEntity simpleEntity2) { + this.simpleEntity2 = simpleEntity2; + } + + public SimpleEntity getSimpleEntity3() { + return simpleEntity3; + } + + public void setSimpleEntity3(SimpleEntity simpleEntity3) { + this.simpleEntity3 = simpleEntity3; + } + +} Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/ContainsMultSimpleEntity.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/SimpleEntity.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/SimpleEntity.java?rev=1346073&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/SimpleEntity.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/SimpleEntity.java Mon Jun 4 18:18:04 2012 @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.foreign.keys; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="FK_SIMPLE") +public class SimpleEntity implements Serializable { + private static final long serialVersionUID = -7535244143281520213L; + + @Id + private long id; + + private String name; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/SimpleEntity.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TestMultForeignKeys.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TestMultForeignKeys.java?rev=1346073&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TestMultForeignKeys.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TestMultForeignKeys.java Mon Jun 4 18:18:04 2012 @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.foreign.keys; + +import java.util.ArrayList; +import java.util.Collection; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +/** + * Test multiple keys that have the same Entity as + * the type. + */ +public class TestMultForeignKeys extends SQLListenerTestCase { + public void setUp() { + setUp(SimpleEntity.class, + ContainsMultSimpleEntity.class, + TopLevel.class, + DROP_TABLES); + assertNotNull(emf); + + create(); + } + + public void testCreated() { + + EntityManager em = emf.createEntityManager(); + + TopLevel tl = em.find(TopLevel.class, 1); + assertNotNull(tl); + + ArrayList contMultSEColl = + (ArrayList)tl.getContMultSEColl(); + assertNotNull(contMultSEColl); + assertEquals(contMultSEColl.size(), 1); + + ContainsMultSimpleEntity contMultSE = contMultSEColl.get(0); + assertNotNull(contMultSE); + + SimpleEntity se1 = contMultSE.getSimpleEntity1(); + assertNotNull(se1); + assertEquals(se1.getId(), 1); + assertEquals(se1.getName(), "name1"); + + SimpleEntity se2 = contMultSE.getSimpleEntity2(); + assertNotNull(se2); + assertEquals(se2.getId(), 2); + assertEquals(se2.getName(), "name2"); + + SimpleEntity se3 = contMultSE.getSimpleEntity3(); + assertNotNull(se3); + assertEquals(se3.getId(), 3); + assertEquals(se3.getName(), "name3"); + + em.close(); + } + + private void create() { + EntityManager em = emf.createEntityManager(); + assertNotNull(em); + em.getTransaction().begin(); + + ContainsMultSimpleEntity contMultSE = new ContainsMultSimpleEntity(); + + SimpleEntity se3 = new SimpleEntity(); + se3.setId(3); + se3.setName("name3"); + + SimpleEntity se1 = new SimpleEntity(); + se1.setId(1); + se1.setName("name1"); + + SimpleEntity se2 = new SimpleEntity(); + se2.setId(2); + se2.setName("name2"); + + contMultSE.setSimpleEntity3(se3); + contMultSE.setSimpleEntity1(se1); + contMultSE.setSimpleEntity2(se2); + + Collection contMultSEColl = new ArrayList(); + contMultSEColl.add(contMultSE); + + TopLevel tl = new TopLevel(); + tl.setId(1); + tl.setContMultSEColl(contMultSEColl); + + em.persist(tl); + + em.getTransaction().commit(); + em.close(); + } + +} Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TestMultForeignKeys.java ------------------------------------------------------------------------------ svn:eol-style = native Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TopLevel.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TopLevel.java?rev=1346073&view=auto ============================================================================== --- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TopLevel.java (added) +++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TopLevel.java Mon Jun 4 18:18:04 2012 @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.foreign.keys; + +import java.io.Serializable; +import java.util.Collection; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class TopLevel implements Serializable { + + private static final long serialVersionUID = -4677719724853263855L; + + @Id + private long id; + + @ElementCollection + private Collection contMultSEColl; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Collection getContMultSEColl() { + return contMultSEColl; + } + + public void setContMultSEColl(Collection contMultSEColl) { + this.contMultSEColl = contMultSEColl; + } + +} Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/foreign/keys/TopLevel.java ------------------------------------------------------------------------------ svn:eol-style = native