OPENJPA-1954: Fix ArrayIndexOutOfBoundsException when querying on a version field that is in a MappedSuperclass.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1076370 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Richard G. Curtis 2011-03-02 20:57:03 +00:00
parent e0a0050e7b
commit 887f8977ed
5 changed files with 64 additions and 36 deletions

View File

@ -1075,9 +1075,14 @@ public class FieldMapping
// version, it will have a NoneFieldMapping (since the version strategy
// for the class takes care of it's mapping), and NoneFieldStrategies
// do not have columns.
if (isVersion())
return getDeclaringMapping().getVersion().getColumns();
else
//
// rgc : 2 March 2011 : Still hacky. If the version field is in a mapped super class we need to look
// at the defining metadata to find the correct Version. Not sure why the version for the declaring metadata
// is different than the defining metadata.
if (isVersion()){
ClassMapping cm = (ClassMapping)((FieldMetaData)this).getDefiningMetaData();
return cm.getVersion().getColumns();
}else
return _val.getColumns();
}

View File

@ -2140,7 +2140,8 @@ public class DBDictionary
if (augmentUpdates) {
Path path = (Path) updateParams.keySet().iterator().next();
FieldMapping fm = (FieldMapping) path.last();
ClassMapping meta = fm.getDeclaringMapping();
ClassMapping meta = fm.getDefiningMapping();
Map<Column,?> updates = meta.getVersion().getBulkUpdateValues();
for (Map.Entry e : updates.entrySet()) {
Column col = (Column) e.getKey();

View File

@ -0,0 +1,37 @@
package org.apache.openjpa.persistence.jdbc.update;
import java.sql.Timestamp;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
@MappedSuperclass
public abstract class BaseTimestampedEntity {
@Id
@GeneratedValue
private long id;
private String name;
@Version
private Timestamp version;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Timestamp getVersion() {
return version;
}
}

View File

@ -19,9 +19,15 @@
package org.apache.openjpa.persistence.jdbc.update;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.openjpa.jdbc.meta.ClassMapping;
import org.apache.openjpa.jdbc.meta.Version;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
@ -34,7 +40,15 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
*/
public class TestTimestampVersion extends SingleEMFTestCase {
public void setUp() {
super.setUp(CLEAR_TABLES, TimestampedEntity.class, NumericVersionedEntity.class);
super.setUp(CLEAR_TABLES, TimestampedEntity.class, NumericVersionedEntity.class, BaseTimestampedEntity.class);
}
public void testQueryOnVersion() {
EntityManager em = emf.createEntityManager();
String pql = "SELECT s FROM TimestampedEntity s WHERE s.version < :endDate";
Query queryObj = em.createQuery(pql);
Timestamp t1 = new Timestamp((new Date()).getTime());
queryObj.setParameter("endDate", t1);
List<TimestampedEntity> scenarioList = queryObj.getResultList();
}
public void testBulkUpdateOnTimestampedVersion() {
@ -47,7 +61,7 @@ public class TestTimestampVersion extends SingleEMFTestCase {
try {
// delay to ensure the new timestamp exceeds the timer's resolution.
Thread.sleep(1000);
Thread.sleep(1500);
} catch (InterruptedException e) {
}

View File

@ -18,13 +18,8 @@
*/
package org.apache.openjpa.persistence.jdbc.update;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
/**
* An entity using a Timestamp as Version field.
@ -35,31 +30,7 @@ import javax.persistence.Version;
*/
@Entity
@Table(name="TSENTITY1")
public class TimestampedEntity {
@Id
@GeneratedValue
private long id;
private String name;
@Version
private Timestamp version;
public class TimestampedEntity extends BaseTimestampedEntity{
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Timestamp getVersion() {
return version;
}
}