You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2012/07/17 23:59:11 UTC

svn commit: r1362679 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/

Author: curtisr7
Date: Tue Jul 17 21:59:10 2012
New Revision: 1362679

URL: http://svn.apache.org/viewvc?rev=1362679&view=rev
Log:
OPENJPA-2228: Fix SQLBuffer when using an Entity with an EmbeddedId that has multiple pks and QuerySQLCache is enabled. Patch contributed by Helen Xu.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java?rev=1362679&r1=1362678&r2=1362679&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java Tue Jul 17 21:59:10 2012
@@ -189,9 +189,28 @@ public final class SQLBuffer
         }
 
         if (_userIndex != null) {
-            // fix up user parameter index
+            // fix up user parameter index(s)
             for (int i = 0; i < _userIndex.size(); i+=2) {
-                _userIndex.set(i, _userParams.indexOf(_userIndex.get(i+1)));
+            	Object param = _userIndex.get(i+1);
+                
+            	Object previousParam = (i > 0) ? _userIndex.get(i-1) : null;            	
+            	if ( param != previousParam) {
+            		_userIndex.set(i, _userParams.indexOf(param));
+            	}else{
+            		//if there are multiple parameters for the in clause or the combined PK field, 
+            		//we got duplicate param objects in _userParams list. 
+            		//In order to find the right index, we have to skip params that's checked already.
+            		int previousUserIndex = (Integer)_userIndex.get(i-2);
+            		int userParamindex = 0;
+
+                	for(Object next : _userParams){
+                        if (next == param && userParamindex > previousUserIndex) {
+                            _userIndex.set(i, userParamindex);
+                            break;
+                      }
+                      userParamindex++;
+                	}
+            	}            	
             }
         }
     }

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java?rev=1362679&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java Tue Jul 17 21:59:10 2012
@@ -0,0 +1,91 @@
+/*
+ * 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.jdbc.sqlcache;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class CombinedPKEmbeddedEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Column(name = "keyA", nullable = false)
+    private int keyA;
+    @Column(name = "keyB", nullable = false)
+    private int keyB;
+    @Column(name = "keyC", nullable = false)
+    private int keyC;
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + keyA;
+        result = prime * result + keyB;
+        result = prime * result + keyC;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        CombinedPKEmbeddedEntity other = (CombinedPKEmbeddedEntity) obj;
+        if (keyA != other.keyA)
+            return false;
+        if (keyB != other.keyB)
+            return false;
+        if (keyC != other.keyC)
+            return false;
+        return true;
+    }
+
+    public int getKeyA() {
+        return keyA;
+    }
+
+    public void setKeyA(int keyA) {
+        this.keyA = keyA;
+    }
+
+    public int getKeyB() {
+        return keyB;
+    }
+
+    public void setKeyB(int keyB) {
+        this.keyB = keyB;
+    }
+
+    public int getKeyC() {
+        return keyC;
+    }
+
+    public void setKeyC(int keyC) {
+        this.keyC = keyC;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java?rev=1362679&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java Tue Jul 17 21:59:10 2012
@@ -0,0 +1,88 @@
+/*
+ * 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.jdbc.sqlcache;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.ManyToOne;
+
+@Entity
+public class CombinedPKListEntity {
+
+    @Id
+    private int id;
+
+    @ManyToOne
+    @JoinColumns({ @JoinColumn(name = "keyA", referencedColumnName = "keyA"),
+        @JoinColumn(name = "keyB", referencedColumnName = "keyB"),
+        @JoinColumn(name = "keyC", referencedColumnName = "keyC") })
+    private CombinedPKTestEntity te;
+
+    @Column(insertable = false, updatable = false)
+    private int keyA;
+    @Column(insertable = false, updatable = false)
+    private int keyB;
+    @Column(insertable = false, updatable = false)
+    private int keyC;
+
+    public int getKeyA() {
+        return keyA;
+    }
+
+    public void setKeyA(int keyA) {
+        this.keyA = keyA;
+    }
+
+    public int getKeyB() {
+        return keyB;
+    }
+
+    public void setKeyB(int keyB) {
+        this.keyB = keyB;
+    }
+
+    public int getKeyC() {
+        return keyC;
+    }
+
+    public void setKeyC(int keyC) {
+        this.keyC = keyC;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public CombinedPKTestEntity getTe() {
+        return te;
+    }
+
+    public void setTe(CombinedPKTestEntity te) {
+        this.te = te;
+    }
+
+}

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

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java?rev=1362679&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java Tue Jul 17 21:59:10 2012
@@ -0,0 +1,80 @@
+/*
+ * 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.jdbc.sqlcache;
+
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+
+@Entity
+public class CombinedPKTestEntity {
+
+    @EmbeddedId
+    CombinedPKEmbeddedEntity pk = new CombinedPKEmbeddedEntity();
+
+    private String data1;
+    private String data2;
+
+    @Override
+    public int hashCode() {
+        return pk.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        CombinedPKTestEntity other = (CombinedPKTestEntity) obj;
+        if (pk == null) {
+            if (other.pk != null)
+                return false;
+        } else if (!pk.equals(other.pk))
+            return false;
+        return true;
+    }
+
+    public CombinedPKEmbeddedEntity getPk() {
+        return pk;
+    }
+
+    public void setPk(CombinedPKEmbeddedEntity pk) {
+        this.pk = pk;
+    }
+
+    public String getData1() {
+        return data1;
+    }
+
+    public void setData1(String data1) {
+        this.data1 = data1;
+    }
+
+    public String getData2() {
+        return data2;
+    }
+
+    public void setData2(String data2) {
+        this.data2 = data2;
+    }
+
+}