Skip to content

Commit 7cf6a62

Browse files
pb00068brmeyer
authored andcommitted
HHH-7513 testcase
1 parent b20ac39 commit 7cf6a62

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.naturalid.mutable.cached;
25+
26+
import javax.persistence.Column;
27+
import javax.persistence.Entity;
28+
import javax.persistence.GeneratedValue;
29+
import javax.persistence.GenerationType;
30+
import javax.persistence.Id;
31+
import javax.persistence.ManyToOne;
32+
import javax.persistence.OneToMany;
33+
import java.util.HashSet;
34+
import java.util.Set;
35+
36+
import org.hibernate.annotations.NaturalId;
37+
38+
/**
39+
* @author Guenther Demetz
40+
*/
41+
@Entity
42+
public class A {
43+
@Id
44+
@GeneratedValue(strategy = GenerationType.TABLE)
45+
public long oid;
46+
47+
@Column
48+
@NaturalId(mutable=true)
49+
public String myname;
50+
51+
@OneToMany(mappedBy="assA")
52+
public Set<B> assB = new HashSet<B>();
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.naturalid.mutable.cached;
25+
26+
import javax.persistence.Entity;
27+
import javax.persistence.GeneratedValue;
28+
import javax.persistence.GenerationType;
29+
import javax.persistence.Id;
30+
import javax.persistence.ManyToOne;
31+
32+
import org.hibernate.annotations.NaturalId;
33+
import org.hibernate.annotations.NaturalIdCache;
34+
35+
/**
36+
* @author Guenther Demetz
37+
*/
38+
@Entity
39+
@NaturalIdCache
40+
public class B {
41+
@Id
42+
@GeneratedValue(strategy = GenerationType.TABLE)
43+
public long oid;
44+
45+
@ManyToOne
46+
@NaturalId(mutable = true)
47+
public A assA = null;
48+
49+
@NaturalId(mutable = true)
50+
public int naturalid;
51+
}

hibernate-core/src/test/java/org/hibernate/test/naturalid/mutable/cached/CachedMutableNaturalIdTest.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.junit.Test;
2929

30+
import org.hibernate.LockOptions;
3031
import org.hibernate.Session;
3132
import org.hibernate.cfg.Configuration;
3233
import org.hibernate.cfg.Environment;
@@ -46,7 +47,7 @@
4647
public abstract class CachedMutableNaturalIdTest extends BaseCoreFunctionalTestCase {
4748
@Override
4849
protected Class<?>[] getAnnotatedClasses() {
49-
return new Class[] {Another.class, AllCached.class};
50+
return new Class[] {A.class, Another.class, AllCached.class, B.class};
5051
}
5152

5253
@Override
@@ -181,5 +182,72 @@ public void testNaturalIdChangeAfterResolveEntityFrom2LCache() {
181182
session.getTransaction().commit();
182183
session.close();
183184
}
185+
186+
@Test
187+
public void testReattachementUnmodifiedInstance() {
188+
Session session = openSession();
189+
session.beginTransaction();
190+
A a = new A();
191+
B b = new B();
192+
b.naturalid = 100;
193+
session.persist( a );
194+
session.persist( b );
195+
b.assA = a;
196+
a.assB.add( b );
197+
session.getTransaction().commit();
198+
session.close();
199+
200+
session = openSession();
201+
session.beginTransaction();
202+
session.buildLockRequest(LockOptions.NONE).lock(b);
203+
// org.hibernate.PropertyAccessException: could not get a field value by reflection getter of org.hibernate.test.naturalid.mutable.cached.A.oid
204+
// at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:62)
205+
// at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:341)
206+
// at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4425)
207+
// at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4147)
208+
// at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:209)
209+
// at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:248)
210+
// at org.hibernate.type.ManyToOneType.disassemble(ManyToOneType.java:214)
211+
// at org.hibernate.cache.spi.NaturalIdCacheKey.<init>(NaturalIdCacheKey.java:84)
212+
// at org.hibernate.engine.internal.StatefulPersistenceContext$1.removeSharedNaturalIdCrossReference(StatefulPersistenceContext.java:1991)
213+
// at org.hibernate.persister.entity.AbstractEntityPersister.handleNaturalIdReattachment(AbstractEntityPersister.java:4134)
214+
// at org.hibernate.persister.entity.AbstractEntityPersister.afterReassociate(AbstractEntityPersister.java:4106)
215+
// at org.hibernate.event.internal.AbstractReassociateEventListener.reassociate(AbstractReassociateEventListener.java:100)
216+
// at org.hibernate.event.internal.DefaultLockEventListener.onLock(DefaultLockEventListener.java:81)
217+
// at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:811)
218+
// at org.hibernate.internal.SessionImpl.fireLock(SessionImpl.java:804)
219+
// at org.hibernate.internal.SessionImpl.access$11(SessionImpl.java:803)
220+
// at org.hibernate.internal.SessionImpl$LockRequestImpl.lock(SessionImpl.java:2365)
221+
// at org.hibernate.test.naturalid.mutable.cached.CachedMutableNaturalIdTest.testSimone(CachedMutableNaturalIdTest.java:308)
222+
// at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
223+
// at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
224+
// at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
225+
// at java.lang.reflect.Method.invoke(Unknown Source)
226+
// at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
227+
// at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
228+
// at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
229+
// at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:63)
230+
// at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
231+
// at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
232+
// at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
233+
// at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:62)
234+
// Caused by: java.lang.IllegalArgumentException: Can not set long field org.hibernate.test.naturalid.mutable.cached.A.oid to java.lang.Long
235+
// at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
236+
// at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
237+
// at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
238+
// at sun.reflect.UnsafeLongFieldAccessorImpl.getLong(Unknown Source)
239+
// at sun.reflect.UnsafeLongFieldAccessorImpl.get(Unknown Source)
240+
// at java.lang.reflect.Field.get(Unknown Source)
241+
// at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:59)
242+
// ... 29 more
243+
244+
245+
session.delete(b.assA);
246+
session.delete(b);
247+
248+
session.getTransaction().commit();
249+
session.close();
250+
}
251+
184252
}
185253

0 commit comments

Comments
 (0)