OPENJPA-1583: Timestamp version field in bulk update

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@924938 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2010-03-18 18:32:19 +00:00
parent bd9b59c90f
commit 8c1175054f
7 changed files with 239 additions and 13 deletions

View File

@ -368,12 +368,12 @@ public class Version
}
/**
* @return a Map<Column,String> specifying how to update each version
* @return a Map<Column,Object> specifying how to update each version
* column in this instance during a bulk update.
*
* @since 1.0.0
*/
public Map getBulkUpdateValues() {
public Map<Column,?> getBulkUpdateValues() {
return _strategy.getBulkUpdateValues();
}
}

View File

@ -22,6 +22,7 @@ import java.sql.SQLException;
import java.util.Map;
import org.apache.openjpa.jdbc.kernel.JDBCStore;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.sql.Result;
import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.kernel.OpenJPAStateManager;
@ -77,10 +78,10 @@ public interface VersionStrategy
public int compareVersion(Object v1, Object v2);
/**
* @return a Map<Column,String> specifying how to update each version
* @return a Map<Column,Object> specifying how to update each version
* column during a bulk update.
*
* @since 1.0.0
*/
public Map getBulkUpdateValues();
public Map<Column,? extends Object> getBulkUpdateValues();
}

View File

@ -65,9 +65,9 @@ public class NumberVersionStrategy
return ((Number) version).intValue() + 1;
}
public Map getBulkUpdateValues() {
public Map<Column,String> getBulkUpdateValues() {
Column[] cols = vers.getColumns();
Map map = new HashMap(cols.length);
Map<Column,String> map = new HashMap<Column,String>(cols.length);
for (int i = 0; i < cols.length; i++)
map.put(cols[i], cols[i].getName() + " + 1");
return map;

View File

@ -2134,14 +2134,18 @@ public class DBDictionary
Path path = (Path) updateParams.keySet().iterator().next();
FieldMapping fm = (FieldMapping) path.last();
ClassMapping meta = fm.getDeclaringMapping();
Map updates = meta.getVersion().getBulkUpdateValues();
for (Iterator iter = updates.entrySet().iterator();
iter.hasNext(); ) {
Map.Entry e = (Map.Entry) iter.next();
Map<Column,?> updates = meta.getVersion().getBulkUpdateValues();
for (Map.Entry e : updates.entrySet()) {
Column col = (Column) e.getKey();
String val = (String) e.getValue();
sql.append(", ").append(toDBName(col.getIdentifier()))
.append(" = ").append(val);
Object val = e.getValue();
sql.append(", ").append(toDBName(col.getIdentifier())).append(" = ");
// Version update value for Numeric version is encoded in a String
// to make SQL such as version = version+1 while Time stamp version is parameterized
if (val instanceof String) {
sql.append((String)val);
} else {
sql.appendValue(val);
}
}
}
}

View File

@ -0,0 +1,67 @@
/*
* 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.update;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;
/**
* An entity using a Timestamp as Version field.
*
*
* @author Pinaki Poddar
*
*/
@Entity
public class NumericVersionedEntity {
@Id
@GeneratedValue
private long id;
private String name;
@Version
private int version;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getVersion() {
return version;
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.update;
import java.sql.Timestamp;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests for update on entity that uses a Timestamp as version.
*
* @see <A HREF="https://issues.apache.org/jira/browse/OPENJPA-1583">OPENJPA-1583</A>
*
* @author Pinaki Poddar
*
*/
public class TestTimestampVersion extends SingleEMFTestCase {
public void setUp() {
super.setUp(CLEAR_TABLES, TimestampedEntity.class, NumericVersionedEntity.class);
}
public void testBulkUpdateOnTimestampedVersion() {
TimestampedEntity pc = new TimestampedEntity();
pc.setName("Original");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(pc);
em.getTransaction().commit();
em.getTransaction().begin();
Timestamp oldVersion = pc.getVersion();
String jpql = "UPDATE TimestampedEntity t SET t.name=:newname WHERE t.name=:oldname";
em.createQuery(jpql)
.setParameter("newname", "Updated")
.setParameter("oldname", "Original")
.executeUpdate();
em.getTransaction().commit();
em.getTransaction().begin();
em.refresh(pc);
Timestamp newVersion = pc.getVersion();
assertTrue(newVersion.after(oldVersion));
}
public void testBulkUpdateOnNumericVersion() {
NumericVersionedEntity pc = new NumericVersionedEntity();
pc.setName("Original");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(pc);
em.getTransaction().commit();
em.getTransaction().begin();
int oldVersion = pc.getVersion();
String jpql = "UPDATE NumericVersionedEntity t SET t.name=:newname WHERE t.name=:oldname";
em.createQuery(jpql)
.setParameter("newname", "Updated")
.setParameter("oldname", "Original")
.executeUpdate();
em.getTransaction().commit();
em.getTransaction().begin();
em.refresh(pc);
int newVersion = pc.getVersion();
assertEquals(newVersion, oldVersion+1);
}
}

View File

@ -0,0 +1,67 @@
/*
* 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.update;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;
/**
* An entity using a Timestamp as Version field.
*
*
* @author Pinaki Poddar
*
*/
@Entity
public class TimestampedEntity {
@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;
}
}