You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by st...@apache.org on 2012/04/30 09:49:45 UTC

svn commit: r1332089 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle: Course.java Lecturer.java SomeEmbeddable.java TestOracleDistinctJoin.java

Author: struberg
Date: Mon Apr 30 07:49:45 2012
New Revision: 1332089

URL: http://svn.apache.org/viewvc?rev=1332089&view=rev
Log:
OPENJPA-2179 create unit test showing the problem.

Please note that the unit test doesn't fail yet!
It just logs the superfluous SQL statements in the output.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Course.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Lecturer.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/SomeEmbeddable.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/TestOracleDistinctJoin.java   (with props)

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Course.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Course.java?rev=1332089&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Course.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Course.java Mon Apr 30 07:49:45 2012
@@ -0,0 +1,138 @@
+/*
+ * 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.jdbc.oracle;
+
+import javax.persistence.AttributeOverride;
+import javax.persistence.AttributeOverrides;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Embedded;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+import javax.persistence.OneToMany;
+import javax.persistence.OrderColumn;
+import javax.persistence.Version;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This is a test entity to demonstrate OPENJPA-2179
+ */
+@Entity
+public class Course implements Serializable
+{
+
+    @Id
+    @GeneratedValue
+    private Long id;
+
+    @Version
+    private Integer optlock;
+
+    @Column(name = "courseNumber", length = 10, nullable = false)
+    private String courseNumber;
+
+
+    private String normalAttribute;
+
+
+    @Embedded
+    @AttributeOverrides({@AttributeOverride(name = "valA", column = @Column(name = "objectiveDe")),
+                         @AttributeOverride(name = "valB", column = @Column(name = "objectiveEn"))})
+    private SomeEmbeddable anEmbeddable;
+
+
+    @Lob
+    private String lobColumn;
+
+    @OneToMany(mappedBy = "course",
+            cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
+            orphanRemoval = true, fetch = FetchType.EAGER)
+    @OrderColumn(name = "POSITION")
+    private List<Lecturer> lecturers;
+
+
+    public Long getId() {
+        return id;
+    }
+
+    public String getCourseNumber() {
+        return courseNumber;
+    }
+
+    public void setCourseNumber(String courseNumber) {
+        this.courseNumber = courseNumber;
+    }
+
+
+    public SomeEmbeddable getAnEmbeddable() {
+        return anEmbeddable;
+    }
+
+    public void setAnEmbeddable(SomeEmbeddable anEmbeddable) {
+        this.anEmbeddable = anEmbeddable;
+    }
+
+    public String getLobColumn() {
+        return lobColumn;
+    }
+
+    public void setLobColumn(String lobColumn) {
+        this.lobColumn = lobColumn;
+    }
+
+    public Integer getOptlock() {
+        return optlock;
+    }
+
+    public void setOptlock(Integer optlock) {
+        this.optlock = optlock;
+    }
+
+    public List<Lecturer> getLecturers() {
+        return lecturers;
+    }
+
+    public void setLecturers(List<Lecturer> lecturers) {
+        this.lecturers = lecturers;
+    }
+
+    /**
+     * @param lecturer the lecturer to add
+     */
+    void addLecturer(Lecturer lecturer) {
+        if (lecturers == null) {
+            lecturers = new ArrayList<Lecturer>();
+        }
+        lecturers.add(lecturer);
+    }
+
+
+    public String getNormalAttribute() {
+        return normalAttribute;
+    }
+
+    public void setNormalAttribute(String normalAttribute) {
+        this.normalAttribute = normalAttribute;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Course.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Lecturer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Lecturer.java?rev=1332089&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Lecturer.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Lecturer.java Mon Apr 30 07:49:45 2012
@@ -0,0 +1,70 @@
+/*
+ * 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.jdbc.oracle;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.Version;
+
+import java.io.Serializable;
+
+import org.apache.openjpa.persistence.jdbc.Index;
+
+/**
+ * This is a test entity to demonstrate OPENJPA-2179
+ */
+@Entity
+public class Lecturer implements Serializable
+{
+
+    @Id
+    @GeneratedValue
+    private Long id;
+
+    @ManyToOne(optional = false)
+    @Index
+    private Course course;
+
+    @Version
+    private Integer optlock;
+
+    private String name;
+
+    public Long getId() {
+        return id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Course getCourse() {
+        return course;
+    }
+
+    public void setCourse(Course course) {
+        this.course = course;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/Lecturer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/SomeEmbeddable.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/SomeEmbeddable.java?rev=1332089&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/SomeEmbeddable.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/SomeEmbeddable.java Mon Apr 30 07:49:45 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.jdbc.oracle;
+
+import javax.persistence.Embeddable;
+import javax.persistence.Lob;
+
+/**
+ * Embeddable for being used in  {@link Course}
+ * This entity contains Lob columns which cause OpenJPA to create
+ * single sub selects for each Embedded field in Oracle.
+ */
+@Embeddable
+public class SomeEmbeddable {
+    @Lob
+    String valA;
+
+    @Lob
+    String valB;
+
+    public String getValA() {
+        return valA;
+    }
+
+    public void setValA(String valA) {
+        this.valA = valA;
+    }
+
+    public String getValB() {
+        return valB;
+    }
+
+    public void setValB(String valB) {
+        this.valB = valB;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/SomeEmbeddable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/TestOracleDistinctJoin.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/TestOracleDistinctJoin.java?rev=1332089&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/TestOracleDistinctJoin.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/TestOracleDistinctJoin.java Mon Apr 30 07:49:45 2012
@@ -0,0 +1,213 @@
+/*
+ * 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.jdbc.oracle;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.OracleDictionary;
+import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
+
+
+/**
+ * This test case demonstrates that we currently do way too much sub selects
+ * on Oracle if &#064;Embedded fields with a &#064;Lob are involved.
+ *
+ * For running the test you can use the following commandline in
+ * openjpa-persistence-jdbc:
+ *
+ *
+ * <pre>
+ * mvn clean test -Dtest=TestOracleDistinctJoin -Doracle.artifactid=ojdbc14 -Doracle.version=10.2.0.4.0
+ *                -Dopenjpa.oracle.url="jdbc:oracle:thin:@192.168.1.6/XE"
+ *                -Dopenjpa.oracle.username=username -Dopenjpa.oracle.password=yourpwd
+ *                -Dopenjpa.Log=DefaultLevel=TRACE -Dtest-oracle
+ * </pre>
+ *
+ * Of course you need to set the correct IP address, username and password of your Oracle server.
+ * This also assumes that you have downloaded the oracle JDBC driver and locally installed it to maven.
+ */
+public class TestOracleDistinctJoin extends AbstractPersistenceTestCase {
+
+    private static String projectStr = "project";
+
+    private Log log;
+
+    private boolean skipTest(DBDictionary dict) {
+        return !(dict instanceof OracleDictionary);
+    }
+
+    public void setUp() throws SQLException {
+        OpenJPAEntityManagerFactorySPI emf = createEMF();
+
+        JDBCConfiguration conf = ((JDBCConfiguration) emf.getConfiguration());
+        DBDictionary dict = conf.getDBDictionaryInstance();
+
+        if (skipTest(dict)) {
+            emf.close();
+            return;
+        }
+        log = emf.getConfiguration().getLog("Tests");
+
+        emf.close();
+    }
+
+    public void testJoinOnly() throws SQLException {
+        OpenJPAEntityManagerFactorySPI emf =
+            createEMF(Course.class, Lecturer.class, SomeEmbeddable.class,
+                "openjpa.jdbc.SchemaFactory", "native",
+                "openjpa.jdbc.DBDictionary", "org.apache.openjpa.jdbc.sql.OracleDictionary",
+                "openjpa.jdbc.SynchronizeMappings",  "buildSchema(ForeignKeys=true)",
+                "openjpa.jdbc.QuerySQLCache", "false",
+                "openjpa.DataCache", "false",
+                "openjpa.PostLoadOnMerge", "true",
+                "openjpa.DetachState", "loaded(DetachedStateField=true)",
+                "openjpa.Compatibility", "IgnoreDetachedStateFieldForProxySerialization=true",
+                "openjpa.jdbc.MappingDefaults", "ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict");
+
+                JDBCConfiguration conf = ((JDBCConfiguration) emf.getConfiguration());
+        DBDictionary dict = conf.getDBDictionaryInstance();
+
+        if (skipTest(dict)) {
+            emf.close();
+            return;
+        }
+
+        Long id = null;
+
+        {
+            EntityManager em = emf.createEntityManager();
+            EntityTransaction tran = em.getTransaction();
+            tran.begin();
+            em.createQuery("DELETE from Lecturer as l").executeUpdate();
+            em.createQuery("DELETE from Course as c").executeUpdate();
+            tran.commit();
+            em.close();
+
+        }
+
+        {
+            EntityManager em = emf.createEntityManager();
+            EntityTransaction tran = em.getTransaction();
+            tran.begin();
+
+            Course course = new Course();
+            SomeEmbeddable emb = new SomeEmbeddable();
+            emb.setValA("a");
+            emb.setValB("b");
+            course.setAnEmbeddable(emb);
+            course.setLobColumn("oh this could be a very looooong text...");
+            course.setCourseNumber("4711");
+
+            em.persist(course);
+
+            Lecturer l1 = new Lecturer();
+            l1.setCourse(course);
+            course.addLecturer(l1);
+
+            id = course.getId();
+            tran.commit();
+            em.close();
+        }
+
+        {
+            EntityManager em = emf.createEntityManager();
+            Course course = em.find(Course.class, id);
+            assertNotNull(course);
+
+            em.close();
+        }
+
+        {
+            log.info("\n\nDistinct and Join"); // this one does sub-selects for LocalizedString and changeLog
+            EntityManager em = emf.createEntityManager();
+            EntityTransaction tran = em.getTransaction();
+            tran.begin();
+
+            Query q = em.createQuery("select distinct c from Course c join  c.lecturers l ");
+            List<Course> courses = q.getResultList();
+            assertFalse(courses.isEmpty());
+            assertNotNull(courses.get(0));
+
+            tran.commit();
+            em.close();
+        }
+        
+        {
+            log.info("\n\nDistinct"); // creates NO sub-query!
+            EntityManager em = emf.createEntityManager();
+
+            Query q = em.createQuery("select distinct c from Course c");
+            List<Course> courses = q.getResultList();
+            assertFalse(courses.isEmpty());
+            assertNotNull(courses.get(0));
+
+            em.close();
+        }
+        
+        {
+            log.info("\n\nJoin"); // creates NO sub-query!
+            EntityManager em = emf.createEntityManager();
+
+            Query q = em.createQuery("select c from Course c join c.lecturers l ");
+            List<Course> courses = q.getResultList();
+            assertFalse(courses.isEmpty());
+            assertNotNull(courses.get(0));
+
+            em.close();
+        }
+        
+        {
+            log.info("\n\nDistinct inverse join"); // this one does sub-selects for LocalizedString and changeLog
+            EntityManager em = emf.createEntityManager();
+
+            Query q = em.createQuery("select distinct c from Lecturer l join l.course c");
+            List<Course> courses = q.getResultList();
+            assertFalse(courses.isEmpty());
+            assertNotNull(courses.get(0));
+
+            em.close();
+        }
+        
+        {
+            log.info("\n\nInverse join"); // this one does sub-selects for LocalizedString and changeLog
+            EntityManager em = emf.createEntityManager();
+
+            Query q = em.createQuery("select c from Lecturer l join l.course c");
+            List<Course> courses = q.getResultList();
+            assertFalse(courses.isEmpty());
+            assertNotNull(courses.get(0));
+
+            em.close();
+        }
+
+
+        emf.close();
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/oracle/TestOracleDistinctJoin.java
------------------------------------------------------------------------------
    svn:eol-style = native