diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java index a4d8f5eb1..c30113654 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/FieldMapping.java @@ -92,6 +92,8 @@ public class FieldMapping private FieldMapping _bi_MTo1_JT_Field = null; private ForeignKey _bi_1ToM_Join_FK = null; private ForeignKey _bi_1ToM_Elem_FK = null; + + private boolean _hasMapsIdCols = false; /** * Constructor. @@ -1335,4 +1337,12 @@ public class FieldMapping public boolean isNonDefaultMappingUsingJoinTableStrategy() { return isBi1To1JT() || isUni1To1JT() || isUniMTo1JT() || isBiMTo1JT(); } + + public void setMapsIdCols(boolean hasMapsIdCols) { + _hasMapsIdCols = hasMapsIdCols; + } + + public boolean hasMapsIdCols() { + return _hasMapsIdCols; + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java index dd3b004bc..506bde429 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingInfo.java @@ -544,6 +544,10 @@ public abstract class MappingInfo // n columns because we don't know which of the n columns the info // applies to List given = getColumns(); + + if (context instanceof FieldMapping && ((FieldMapping)context).hasMapsIdCols()) + given = ((FieldMapping)context).getValueInfo().getMapsIdColumns(); + boolean fill = ((MappingRepository) context.getRepository()). getMappingDefaults().defaultMissingInfo(); if ((!given.isEmpty() || (!adapt && !fill)) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java index e3f505c91..900dd2f4f 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingImpl.java @@ -34,6 +34,7 @@ import org.apache.openjpa.jdbc.sql.Select; import org.apache.openjpa.kernel.OpenJPAStateManager; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.JavaTypes; +import org.apache.openjpa.meta.ValueMetaData; import org.apache.openjpa.meta.ValueMetaDataImpl; import org.apache.openjpa.util.InternalException; import org.apache.openjpa.util.MetaDataException; @@ -458,6 +459,11 @@ public class ValueMappingImpl embed.syncMappingInfo(); } } + + public void copy(ValueMetaData vmd) { + super.copy(vmd); + copyMappingInfo((ValueMapping)vmd); + } public void copyMappingInfo(ValueMapping vm) { setValueMappedBy(vm.getValueMappedBy()); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingInfo.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingInfo.java index 3687aefc5..190ca6f91 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingInfo.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ValueMappingInfo.java @@ -18,6 +18,7 @@ */ package org.apache.openjpa.jdbc.meta; +import java.util.Collections; import java.util.List; import org.apache.openjpa.jdbc.schema.Column; @@ -47,6 +48,7 @@ public class ValueMappingInfo private boolean _criteria = false; private boolean _canNull = true; + private List _mapsIdCols = null; /** * Whether to use class criteria when joining to related type. @@ -331,4 +333,18 @@ public class ValueMappingInfo if (_canNull) _canNull = vinfo.canIndicateNull(); } + + /** + * Raw column data. + */ + public List getMapsIdColumns() { + return (_mapsIdCols == null) ? Collections.EMPTY_LIST : _mapsIdCols; + } + + /** + * Raw column data. + */ + public void setMapsIdColumns(List cols) { + _mapsIdCols = cols; + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java index 154d55c90..ca8205ec6 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedValueHandler.java @@ -283,7 +283,7 @@ public abstract class EmbedValueHandler Column[] pkCols = ((ValueMappingImpl)fmd.getValue()).getColumns(); for (int j = 0; j < pkCols.length; j++) { Column newCol = new Column(); - newCol.setName(pkCols[j].getName()); + newCol.copy(pkCols[j]); cols.add(newCol); } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ObjectIdValueHandler.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ObjectIdValueHandler.java index 90174635c..56ba286cf 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ObjectIdValueHandler.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ObjectIdValueHandler.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; import org.apache.openjpa.jdbc.kernel.JDBCStore; +import org.apache.openjpa.jdbc.meta.ClassMapping; +import org.apache.openjpa.jdbc.meta.FieldMapping; import org.apache.openjpa.jdbc.meta.ValueMapping; import org.apache.openjpa.jdbc.schema.Column; import org.apache.openjpa.jdbc.schema.ColumnIO; @@ -46,12 +48,92 @@ public class ObjectIdValueHandler boolean adapt) { List cols = new ArrayList(); List args = new ArrayList(); + setMapsIdCol(vm); super.map(vm, name, io, adapt, cols, args); vm.setColumns((Column[]) cols.toArray(new Column[cols.size()])); _args = args.toArray(); return vm.getColumns(); } + + private void setMapsIdCol(ValueMapping vm) { + if (!(vm instanceof FieldMapping)) + return; + if (!((FieldMapping)vm).hasMapsIdCols()) + return; + ClassMapping embeddedMeta = (ClassMapping)((FieldMapping)vm).getValue().getEmbeddedMetaData(); + if (embeddedMeta == null) + return; + List mapsIdColList = ((FieldMapping)vm).getValueInfo().getMapsIdColumns(); + if (mapsIdColList.size() > 0 ) { + setMapsIdCols(mapsIdColList, embeddedMeta); + return; + } + + FieldMapping[] fmds = embeddedMeta.getFieldMappings(); + for (int i = 0; i < fmds.length; i++) { + mapsIdColList = fmds[i].getValueInfo().getMapsIdColumns(); + if (mapsIdColList.size() == 0) + continue; + ClassMapping embeddedMeta1 = (ClassMapping)fmds[i].getEmbeddedMetaData(); + if (embeddedMeta1 != null) + setMapsIdCols(mapsIdColList, embeddedMeta1); + else + setMapsIdCols(mapsIdColList, fmds[i]); + } + } + + private void setMapsIdCols(List cols, ClassMapping cm) { + for (int i = 0; i < cols.size(); i++) { + String refColName = ((Column)cols.get(i)).getTarget(); + FieldMapping fm = getReferenceField(cm, refColName); + if (fm != null) { + List colList1 = new ArrayList(); + colList1.add(cols.get(i)); + fm.setMapsIdCols(true); + fm.getValueInfo().setMapsIdColumns(colList1); + } + } + } + + private void setMapsIdCols(List cols, FieldMapping fm) { + if (cols.size() == 1) { + fm.setMapsIdCols(true); + fm.getValueInfo().setMapsIdColumns(cols); + return; + } + + for (int i = 0; i < cols.size(); i++) { + String refColName = ((Column)cols.get(i)).getTarget(); + if (isReferenceField(fm, refColName)) { + List colList1 = new ArrayList(); + colList1.add(cols.get(i)); + fm.setMapsIdCols(true); + fm.getValueInfo().setMapsIdColumns(colList1); + } + } + } + + private FieldMapping getReferenceField(ClassMapping cm, String refColName) { + FieldMapping[] fmds = cm.getFieldMappings(); + for (int i = 0; i < fmds.length; i++) { + if (isReferenceField(fmds[i], refColName)) + return fmds[i]; + } + return null; + } + + private boolean isReferenceField(FieldMapping fm, String refColName) { + List cols = fm.getValueInfo().getColumns(); + if (cols.size() == 0) { + if (fm.getName().equals(refColName)) + return true; + } else { + if (((Column)cols.get(0)).getName().equals(refColName)) + return true; + } + return false; + } public Object getResultArgument(ValueMapping vm) { return _args; diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java index 04de366d9..f9428e91b 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java @@ -200,7 +200,8 @@ public class RelationFieldStrategy FieldMapping fm = (FieldMapping) pks[i]; ValueMappingImpl val = (ValueMappingImpl) field.getValue(); ValueMappingInfo info = val.getValueInfo(); - info.setColumns(getMappedByIdColumns(fm)); + if (info.getColumns().size() == 0) + info.setColumns(getMappedByIdColumns(fm)); } } diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java index aa209e907..959b2894d 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java @@ -1689,6 +1689,21 @@ public class AnnotationPersistenceMappingParser setColumns(fm, info, cols, unique); if (secondary != null) fm.getMappingInfo().setTableName(secondary); + String mappedByIdValue = fm.getMappedByIdValue(); + if (mappedByIdValue != null) { + FieldMapping[] pks = fm.getDefiningMapping().getPrimaryKeyFieldMappings(); + pks[0].setMapsIdCols(true); + if (mappedByIdValue.length() == 0) { + pks[0].getValueInfo().setMapsIdColumns(cols); + } else { + ClassMapping embeddedMeta = (ClassMapping)pks[0].getValue().getEmbeddedMetaData(); + if (embeddedMeta != null) { + FieldMapping fmd = embeddedMeta.getFieldMapping(mappedByIdValue); + if (fmd != null) + fmd.getValueInfo().setMapsIdColumns(cols); + } + } + } } /** diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java index 6107adca6..7337a05ee 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Book1.java @@ -22,13 +22,17 @@ import java.io.Serializable; import java.util.HashSet; import java.util.Set; +import javax.persistence.AttributeOverride; +import javax.persistence.AttributeOverrides; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MapsId; import javax.persistence.OneToMany; +import javax.persistence.Table; import org.apache.openjpa.persistence.jdbc.VersionColumn; @@ -44,17 +48,22 @@ import org.apache.openjpa.persistence.jdbc.VersionColumn; * */ @Entity +@Table(name="DI_BOOK1") @VersionColumn public class Book1 implements Serializable { @EmbeddedId + @AttributeOverrides({ + @AttributeOverride(name="name", column=@Column(name="BOOK_NAME")), + @AttributeOverride(name="library", column=@Column(name="LIBRARY_NAME")) + }) private BookId1 bid; @OneToMany(cascade = CascadeType.ALL, mappedBy = "book") private Set pages = new HashSet(); @MapsId("library") - @Column(nullable = false) @ManyToOne + @JoinColumn(name="LIBRARY_NAME", referencedColumnName="LIBRARY_NAME") private Library1 library; private String author; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java index f24c663cf..3e9fbe1e0 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Library1.java @@ -26,6 +26,7 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; +import javax.persistence.Table; import org.apache.openjpa.persistence.jdbc.VersionColumn; @@ -41,6 +42,7 @@ import org.apache.openjpa.persistence.jdbc.VersionColumn; * */ @Entity +@Table(name="DI_LIBRARY1") @VersionColumn public class Library1 implements Serializable { @Id diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Line1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Line1.java new file mode 100644 index 000000000..64948307a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Line1.java @@ -0,0 +1,82 @@ +/* + * 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.enhance.identity; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.AttributeOverride; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.apache.openjpa.persistence.jdbc.VersionColumn; + +/** + * Entity used to test compound primary keys using entity as relationship to + * more than one level. + * + * Test case and domain classes were originally part of the reported issue + * OPENJPA-207 + * + * @author Jeffrey Blattman + * @author Pinaki Poddar + * + */ +@Entity +@Table(name="DI_LINE1") +@VersionColumn +public class Line1 implements Serializable { + @EmbeddedId + @AttributeOverride(name="lineNum", column=@Column(name="LINE_NUM")) + private LineId1 lid; + + @MapsId("page") + @ManyToOne + @JoinColumns({ + @JoinColumn(name="LIBRARY_NAME", referencedColumnName="LIBRARY_NAME"), + @JoinColumn(name="BOOK_NAME", referencedColumnName="BOOK_NAME"), + @JoinColumn(name="PAGE_NUM", referencedColumnName="PAGE_NUM") + }) + private Page1 page; + + public LineId1 getLid() { + return lid; + } + + public void setLid(LineId1 lid) { + this.lid = lid; + } + + public Page1 getPage() { + return page; + } + + public void setPage(Page1 page) { + this.page = page; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/LineId1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/LineId1.java new file mode 100644 index 000000000..b6a625305 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/LineId1.java @@ -0,0 +1,95 @@ +/* + * 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.enhance.identity; + +import java.io.Serializable; + +import javax.persistence.AttributeOverride; +import javax.persistence.AttributeOverrides; +import javax.persistence.Column; +import javax.persistence.Embeddable; +import javax.persistence.Embedded; + + +/** + * Entity identity used to test compound primary keys using entity as + * relationship to more than one level. + * + * Test case and domain classes were originally part of the reported issue + * OPENJPA-207 + * + * @author Jeffrey Blattman + * @author Pinaki Poddar + * + */ + +@Embeddable +public final class LineId1 implements Serializable { + @Column(name="LINE_NUM") + private int lineNum; + + @Embedded + @AttributeOverride(name="number", column=@Column(name="PAGE_NUM")) + private PageId1 page; + + public LineId1() {} + + public LineId1(int lineNum, PageId1 page) { + this.lineNum = lineNum; + this.page = page; + } + + public int getLineNum() { + return lineNum; + } + + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + + public boolean equals(Object o) { + if (!(o instanceof LineId1)) { + return false; + } + + LineId1 other = (LineId1)o; + + if (!(getLineNum() == other.getLineNum())) { + return false; + } + + if (!getPage().equals(other.getPage())) { + return false; + } + + return true; + } + + public int hashCode() { + return lineNum * (page != null ? getPage().hashCode() : 31); + } + + public PageId1 getPage() { + return page; + } + + public void setPage(PageId1 page) { + this.page = page; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java index b4a5e8609..01e6c957f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Page1.java @@ -19,7 +19,11 @@ package org.apache.openjpa.persistence.enhance.identity; import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; +import javax.persistence.AttributeOverride; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; @@ -27,6 +31,8 @@ import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.MapsId; +import javax.persistence.OneToMany; +import javax.persistence.Table; import org.apache.openjpa.persistence.jdbc.VersionColumn; @@ -42,22 +48,24 @@ import org.apache.openjpa.persistence.jdbc.VersionColumn; * */ @Entity - +@Table(name="DI_PAGE1") @VersionColumn public class Page1 implements Serializable { @EmbeddedId + @AttributeOverride(name="number", column=@Column(name="PAGE_NUM")) private PageId1 pid; @MapsId("book") - @Column(nullable = false) @ManyToOne @JoinColumns({ - @JoinColumn(name="BOOK_LIBRARY_LIBRARY_NAME", - referencedColumnName="LIBRARY_LIBRARY_NAME"), - @JoinColumn(name="BOOK_BOOK_NAME", referencedColumnName="BOOK_NAME") + @JoinColumn(name="LIBRARY_NAME", referencedColumnName="LIBRARY_NAME"), + @JoinColumn(name="BOOK_NAME", referencedColumnName="BOOK_NAME") }) private Book1 book; - + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "page") + private Set lines = new HashSet(); + public PageId1 getPid() { return pid; } @@ -73,4 +81,22 @@ public class Page1 implements Serializable { public void setBook(Book1 book) { this.book = book; } + + public Set getLines() { + return lines; + } + + public Line1 getLine(LineId1 lid) { + for (Line1 l: lines) { + if (l.getLid().equals(lid)) { + return l; + } + } + return null; + } + + public void addLine(Line1 l) { + l.setPage(this); + lines.add(l); + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java index 61d5b3602..c110e27aa 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/PageId1.java @@ -20,6 +20,9 @@ package org.apache.openjpa.persistence.enhance.identity; import java.io.Serializable; +import javax.persistence.AttributeOverride; +import javax.persistence.AttributeOverrides; +import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Embedded; @@ -39,7 +42,12 @@ import javax.persistence.Embedded; @Embeddable public final class PageId1 implements Serializable { private int number; + @Embedded + @AttributeOverrides({ + @AttributeOverride(name="name", column=@Column(name="BOOK_NAME")), + @AttributeOverride(name="library", column=@Column(name="LIBRARY_NAME")) + }) private BookId1 book; public PageId1() {} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java index d325579a4..ccb7e128e 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMultipleLevelDerivedIdentity1.java @@ -18,11 +18,13 @@ */ package org.apache.openjpa.persistence.enhance.identity; +import java.util.Arrays; import java.util.List; import javax.persistence.EntityManager; -import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.apache.openjpa.persistence.jdbc.SQLSniffer; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; /** @@ -32,22 +34,25 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; * */ @SuppressWarnings("unchecked") -public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { +public class TestMultipleLevelDerivedIdentity1 extends SQLListenerTestCase { private static String LIBRARY_NAME = "LIB"; private static String BOOK_NAME = "foo"; private static int NUM_PAGES = 3; + private static int NUM_LINES = 20; public void setUp() throws Exception { - super.setUp(CLEAR_TABLES, Library1.class, Book1.class, Page1.class, - BookId1.class, PageId1.class, + super.setUp(DROP_TABLES, Library1.class, Book1.class, Page1.class, + BookId1.class, PageId1.class, Line1.class, LineId1.class, "openjpa.RuntimeUnenhancedClasses", "unsupported"); create(); } public void testPersist() { + sql.clear(); create(); } public void testQueryRootLevel() { + sql.clear(); EntityManager em = emf.createEntityManager(); List list = em.createQuery("SELECT p FROM Library1 p") .getResultList(); @@ -62,6 +67,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testQueryIntermediateLevel() { + sql.clear(); EntityManager em = emf.createEntityManager(); List list = em.createQuery("SELECT p FROM Book1 p") .getResultList(); @@ -80,6 +86,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testQueryLeafLevel() { + sql.clear(); EntityManager em = emf.createEntityManager(); List list = em.createQuery("SELECT p FROM Page1 p") .getResultList(); @@ -95,6 +102,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testFindRootNode() { + sql.clear(); EntityManager em = emf.createEntityManager(); Library1 lib = em.find(Library1.class, LIBRARY_NAME); assertNotNull(lib); @@ -106,6 +114,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testFindIntermediateNode() { + sql.clear(); EntityManager em = emf.createEntityManager(); BookId1 bookId = new BookId1(); @@ -116,6 +125,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testFindLeafNode() { + sql.clear(); EntityManager em = emf.createEntityManager(); BookId1 bookId = new BookId1(); @@ -129,6 +139,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testUpdate() { + sql.clear(); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); BookId1 bookId = new BookId1(); @@ -141,6 +152,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testDeleteRoot() { + sql.clear(); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Library1 lib = em.find(Library1.class, LIBRARY_NAME); @@ -153,6 +165,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testDeleteLeafObtainedByQuery() { + sql.clear(); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Page1 page = (Page1)em.createQuery( @@ -168,6 +181,7 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { } public void testDeleteLeafObtainedByFind() { + sql.clear(); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); BookId1 bookId = new BookId1(); @@ -212,10 +226,36 @@ public class TestMultipleLevelDerivedIdentity1 extends SingleEMFTestCase { PageId1 pid = new PageId1(i, bid); page.setPid(pid); book.addPage(page); + for (int j = 1; j <= NUM_LINES; j++) { + Line1 line = new Line1(); + LineId1 lid = new LineId1(j, pid); + line.setLid(lid); + page.addLine(line); + + } } em.persist(lib); em.getTransaction().commit(); em.clear(); + assertSQLFragnments(sql, "CREATE TABLE DI_LIBRARY1", "LIBRARY_NAME"); + assertSQLFragnments(sql, "CREATE TABLE DI_BOOK1", "LIBRARY_NAME", "BOOK_NAME"); + assertSQLFragnments(sql, "CREATE TABLE DI_PAGE1", "LIBRARY_NAME", "BOOK_NAME", "PAGE_NUM"); + assertSQLFragnments(sql, "CREATE TABLE DI_LINE1", "LIBRARY_NAME", "BOOK_NAME", "PAGE_NUM", "LINE_NUM"); } + + void assertSQLFragnments(List list, String... keys) { + if (SQLSniffer.matches(list, keys)) + return; + fail("None of the following " + sql.size() + " SQL \r\n" + + toString(sql) + "\r\n contains all keys \r\n" + + toString(Arrays.asList(keys))); + } + + public String toString(List list) { + StringBuffer buf = new StringBuffer(); + for (String s : list) + buf.append(s).append("\r\n"); + return buf.toString(); + } }