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
This commit is contained in:
Richard G. Curtis 2012-07-17 21:59:10 +00:00
parent a42c9c280a
commit 4b04ef53cf
4 changed files with 280 additions and 2 deletions

View File

@ -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++;
}
}
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}