OPENJPA-1583: Back-ported Pinaki's trunk changes to 1.0.x.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@1395642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2012-10-08 16:32:26 +00:00
parent 8f4934639c
commit 5e9d1d1cc4
6 changed files with 237 additions and 5 deletions

View File

@ -368,7 +368,7 @@ 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

View File

@ -27,6 +27,7 @@ import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.kernel.OpenJPAStateManager;
import org.apache.openjpa.kernel.StoreManager;
import org.apache.openjpa.jdbc.schema.Column;
/**
* Handles optimistic lock versioning for a class.
@ -78,7 +79,7 @@ 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

View File

@ -1972,9 +1972,15 @@ public class DBDictionary
iter.hasNext(); ) {
Map.Entry e = (Map.Entry) iter.next();
Column col = (Column) e.getKey();
String val = (String) e.getValue();
sql.append(", ").append(col.getName())
.append(" = ").append(val);
Object val = e.getValue();
sql.append(", ").append(col.getName()).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,93 @@
/*
* 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();
try {
// delay to ensure the new timestamp exceeds the timer's resolution.
Thread.sleep(100);
} catch (InterruptedException e) {
}
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,65 @@
/*
* 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.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.
*
*
* @author Pinaki Poddar
*
*/
@Entity
@Table(name="TSENTITY")
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;
}
}