OPENJPA-1944: Properly handle a null version field.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1084265 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Richard G. Curtis 2011-03-22 17:41:58 +00:00
parent f6e05d0852
commit 2cfb4f6232
5 changed files with 49 additions and 15 deletions

View File

@ -270,15 +270,6 @@ public abstract class ColumnVersionStrategy
Object version = populateFromResult(res); Object version = populateFromResult(res);
// we know the version column was part of the result - safe to initialize to 1.
if(version == null) {
if (sm.getMetaData().getVersionField().getDeclaredTypeCode() == JavaTypes.DATE) {
version = new Timestamp(1);
} else {
version = new Long(1);
}
}
// OPENJPA-662 Allow a null StateManager because this method may just be // OPENJPA-662 Allow a null StateManager because this method may just be
// invoked to get the result of projection query // invoked to get the result of projection query
if (sm != null) { if (sm != null) {

View File

@ -3069,7 +3069,10 @@ public class StateManagerImpl
// always be set after the first state load or set (which is why // always be set after the first state load or set (which is why
// we do this even if no fields were loaded -- could be that this // we do this even if no fields were loaded -- could be that this
// method is being called after a field is set) // method is being called after a field is set)
if (_loadVersion == null && (_meta == null || _meta.getVersionField() != null)) { // If the _loadVersion field is null AND the version field has been loaded, skip calling sync version.
// This indicates that the DB has a null value for the version column.
FieldMetaData versionMeta = _meta != null ? _meta.getVersionField() : null;
if (_loadVersion == null && (versionMeta != null && !_loaded.get(versionMeta.getIndex()))) {
syncVersion(sdata); syncVersion(sdata);
ret = ret || _loadVersion != null; ret = ret || _loadVersion != null;
} }

View File

@ -41,7 +41,7 @@ public class AnnoTest1 {
@Version @Version
@Column(name = "ANNOVER") @Column(name = "ANNOVER")
protected int version; protected Integer version;
@Basic @Basic
protected int basic; protected int basic;

View File

@ -34,7 +34,10 @@ public class TestVersion extends SingleEMFTestCase {
public void setUp() { public void setUp() {
setUp(AnnoTest1.class, AnnoTest2.class, AnnoTest3.class, Flat1.class, setUp(AnnoTest1.class, AnnoTest2.class, AnnoTest3.class, Flat1.class,
EmbedOwner.class, EmbedValue.class, CLEAR_TABLES); EmbedOwner.class, EmbedValue.class, CLEAR_TABLES
,"openjpa.Log","SQL=trace"
,"openjpa.ConnectionFactoryProperties","printParameters=true"
);
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
@ -192,4 +195,37 @@ public class TestVersion extends SingleEMFTestCase {
cls.getVersion().getStrategy()); cls.getVersion().getStrategy());
assertEquals(1, cls.getVersion().getColumns().length); assertEquals(1, cls.getVersion().getColumns().length);
} }
public void testNullInitialVersion() {
EntityManager em = emf.createEntityManager();
EntityManager em2 = emf.createEntityManager();
try {
AnnoTest1 e = new AnnoTest1(System.currentTimeMillis());
em.getTransaction().begin();
em.persist(e);
em.createQuery("UPDATE AnnoTest1 a SET a.version=null where a.pk=:pk").setParameter("pk", e.getPk())
.executeUpdate();
em.getTransaction().commit();
em.close();
em = emf.createEntityManager();
em.getTransaction().begin();
em2.getTransaction().begin();
AnnoTest1 e2 = em2.find(AnnoTest1.class, e.getPk());
e = em.find(AnnoTest1.class, e.getPk());
e.setBasic(1);
em.getTransaction().commit();
e2 = em2.find(AnnoTest1.class, e.getPk());
em2.refresh(e2);
System.out.println(e2.getBasic());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
} }

View File

@ -53,7 +53,8 @@ public class TestVersionColumn extends SQLListenerTestCase {
assertNotNull("No results found", results); assertNotNull("No results found", results);
assertFalse("No results found", results.isEmpty()); assertFalse("No results found", results.isEmpty());
for (IntVersion iv : results) { for (IntVersion iv : results) {
assertEquals("Version should be initialized to 1, was: " + iv.getVersion(), 1, iv.getVersion()); assertEquals("Version should be initialized to 0, was: " + iv.getVersion(), 0, iv.getVersion());
em.find(IntVersion.class, iv.getId());
} }
assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount()); assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount());
@ -80,7 +81,8 @@ public class TestVersionColumn extends SQLListenerTestCase {
assertNotNull("No results found", results); assertNotNull("No results found", results);
assertFalse("No results found", results.isEmpty()); assertFalse("No results found", results.isEmpty());
for (TimestampVersion iv : results) { for (TimestampVersion iv : results) {
assertEquals("Version should be initialized to 1" + iv.getVersion(), new Timestamp(1), iv.getVersion()); assertEquals("Version should be initialized to null, was: " + iv.getVersion(), null, iv.getVersion());
em.find(TimestampVersion.class, iv.getId());
} }
assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount()); assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount());
@ -107,9 +109,11 @@ public class TestVersionColumn extends SQLListenerTestCase {
assertNotNull("No results found", results); assertNotNull("No results found", results);
assertFalse("No results found", results.isEmpty()); assertFalse("No results found", results.isEmpty());
for (ShortVersion iv : results) { for (ShortVersion iv : results) {
assertEquals("Version should be initialized to 1" + iv.getVersion(), 1, iv.getVersion()); assertEquals("Version should be initialized to 0, was" + iv.getVersion(), 0, iv.getVersion());
em.find(ShortVersion.class, iv.getId());
} }
assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount()); assertEquals("Unexpected number of SQL statements: " + getSQLCount(), 1, getSQLCount());
em.close(); em.close();