diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java index 9c559cf98c..af496670ad 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/CollectionCacheEvictionTest.java @@ -283,4 +283,40 @@ public class CollectionCacheEvictionTest extends BaseCoreFunctionalTestCase { } s.close(); } + + /** + * @author Guenther Demetz + */ + @TestForIssue(jiraKey = "HHH-9764") + @Test + public void testLockModes() { + Session s1 = openSession(); + s1.beginTransaction(); + + Company company1 = (Company) s1.get( Company.class, 1 ); + + User user1 = (User) s1.get( User.class, 1 ); // into persistent context + + /****************************************** + * + */ + Session s2 = openSession(); + s2.beginTransaction(); + User user = (User) s2.get( User.class, 1 ); + user.setName("TestUser"); + s2.getTransaction().commit(); + s2.close(); + + + /****************************************** + * + */ + + // init cache of collection + assertEquals( 1, company1.getUsers().size() ); // raises org.hibernate.StaleObjectStateException if 2LCache is enabled + + + s1.getTransaction().commit(); + s1.close(); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/Company.java b/hibernate-core/src/test/java/org/hibernate/test/cache/Company.java index a5c6a9ed1d..71a8c4c367 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/Company.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/Company.java @@ -7,10 +7,13 @@ package org.hibernate.test.cache; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToMany; +import javax.persistence.Version; + import java.util.ArrayList; import java.util.List; @@ -21,7 +24,7 @@ import org.hibernate.annotations.CacheConcurrencyStrategy; public class Company { @Id int id; - + @OneToMany(fetch = FetchType.LAZY, mappedBy = "company") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) List users = new ArrayList(); @@ -48,4 +51,5 @@ public class Company { public void setUsers(List users) { this.users = users; } + } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/User.java b/hibernate-core/src/test/java/org/hibernate/test/cache/User.java index be1f8e3fa5..5a5e938502 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/User.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/User.java @@ -7,18 +7,39 @@ package org.hibernate.test.cache; +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; import javax.persistence.Table; +import javax.persistence.Version; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity +@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @Table(name="`User`") public class User { @Id int id; + + @Version + @Column(name="OPTLOCK") + private int version; + + + + @Column + private String name; + + @ManyToOne(fetch = FetchType.LAZY) Company company; @@ -49,4 +70,18 @@ public class User { public void setCompany(Company group) { this.company = group; } + + + + public int getVersion() { + return version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } }