From 4b04ef53cfa5769f9a32cd90808f17a5bb06a1f0 Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Tue, 17 Jul 2012 21:59:10 +0000 Subject: [PATCH] OPENJPA-2228: Fix SQLBuffer when using an Entity with an EmbeddedId that has multiple pks and QuerySQLCache is enabled. Patch contributed by Helen Xu. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1362679 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/sql/SQLBuffer.java | 23 ++++- .../sqlcache/CombinedPKEmbeddedEntity.java | 91 +++++++++++++++++++ .../jdbc/sqlcache/CombinedPKListEntity.java | 88 ++++++++++++++++++ .../jdbc/sqlcache/CombinedPKTestEntity.java | 80 ++++++++++++++++ 4 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java index 345877ffc..418214f47 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java @@ -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++; + } + } } } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java new file mode 100644 index 000000000..757625c2e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKEmbeddedEntity.java @@ -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; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java new file mode 100644 index 000000000..def133e2e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKListEntity.java @@ -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; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java new file mode 100644 index 000000000..430f9991d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/CombinedPKTestEntity.java @@ -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; + } + +}