From df0479951c847f8a0c132fb45f556f6a7990725e Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 30 Nov 2021 13:35:07 +0100 Subject: [PATCH] Fix o.h.Interceptor#onSave call --- .../java/org/hibernate/EmptyInterceptor.java | 7 + .../main/java/org/hibernate/Interceptor.java | 31 +---- .../EmptyInitializedNestedCompositesTest.java | 87 ++++++------ .../interceptor/CollectionInterceptor.java | 4 +- .../{ => orm}/test/interceptor/Image.hbm.xml | 2 +- .../{ => orm}/test/interceptor/Image.java | 2 +- .../interceptor/InstantiateInterceptor.java | 4 +- .../InterceptorNonNullTransactionTest.java | 2 +- .../test/interceptor/InterceptorTest.java | 126 +++++++++++------- .../InterceptorTransactionEventTest.java | 4 +- .../{ => orm}/test/interceptor/Log.java | 2 +- .../test/interceptor/PropertyInterceptor.java | 6 +- .../test/interceptor/StatefulInterceptor.java | 9 +- .../{ => orm}/test/interceptor/User.hbm.xml | 2 +- .../{ => orm}/test/interceptor/User.java | 2 +- .../test/interfaceproxy/Document.java | 2 +- .../test/interfaceproxy/DocumentImpl.java | 2 +- .../interfaceproxy/DocumentInterceptor.java | 4 +- .../{ => orm}/test/interfaceproxy/Folder.java | 2 +- .../test/interfaceproxy/FolderImpl.java | 2 +- .../interfaceproxy/InterfaceProxyTest.java | 122 +++++++++++++++++ .../test/interfaceproxy/Item.hbm.xml | 2 +- .../{ => orm}/test/interfaceproxy/Item.java | 2 +- .../test/interfaceproxy/ItemImpl.java | 2 +- .../test/interfaceproxy/SecureDocument.java | 2 +- .../interfaceproxy/SecureDocumentImpl.java | 2 +- .../ExceptionInterceptor.java | 4 +- .../LocalExceptionInterceptor.java | 3 +- .../{ => orm}/test/proxy/Container.java | 2 +- .../{ => orm}/test/proxy/DataPoint.hbm.xml | 2 +- .../{ => orm}/test/proxy/DataPoint.java | 2 +- .../hibernate/{ => orm}/test/proxy/Info.java | 2 +- .../MissingSetterWithEnhancementTest.java | 2 +- .../hibernate/{ => orm}/test/proxy/Owner.java | 2 +- .../{ => orm}/test/proxy/ProxyTest.java | 7 +- .../interceptor/ProxyInterceptor.java | 1 + .../tuplizer/EntityNameInterceptor.java | 1 + .../interfaceproxy/InterfaceProxyTest.java | 99 -------------- .../test/mixed/DocumentInterceptor.java | 2 +- migration-guide.adoc | 14 ++ 40 files changed, 321 insertions(+), 256 deletions(-) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/component/empty/EmptyInitializedNestedCompositesTest.java (50%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/CollectionInterceptor.java (83%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/Image.hbm.xml (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/Image.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/InstantiateInterceptor.java (88%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/InterceptorNonNullTransactionTest.java (99%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/InterceptorTest.java (77%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/InterceptorTransactionEventTest.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/Log.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/PropertyInterceptor.java (81%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/StatefulInterceptor.java (88%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/User.hbm.xml (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interceptor/User.java (97%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/Document.java (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/DocumentImpl.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/DocumentInterceptor.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/Folder.java (91%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/FolderImpl.java (93%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/InterfaceProxyTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/Item.hbm.xml (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/Item.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/ItemImpl.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/SecureDocument.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/interfaceproxy/SecureDocumentImpl.java (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/Container.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/DataPoint.hbm.xml (97%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/DataPoint.java (97%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/Info.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/MissingSetterWithEnhancementTest.java (98%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/Owner.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/ProxyTest.java (99%) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/InterfaceProxyTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/EmptyInterceptor.java b/hibernate-core/src/main/java/org/hibernate/EmptyInterceptor.java index c00700e373..5f8a25ba91 100644 --- a/hibernate-core/src/main/java/org/hibernate/EmptyInterceptor.java +++ b/hibernate-core/src/main/java/org/hibernate/EmptyInterceptor.java @@ -9,6 +9,7 @@ package org.hibernate; import java.io.Serializable; import org.hibernate.metamodel.RepresentationMode; +import org.hibernate.type.Type; /** * An interceptor that does nothing. May be used as a base class for application-defined custom interceptors. @@ -26,6 +27,12 @@ public class EmptyInterceptor implements Interceptor, Serializable { protected EmptyInterceptor() {} + @Override + public boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) + throws CallbackException { + return false; + } + @Override public Object instantiate(String entityName, RepresentationMode representationMode, Object id) throws CallbackException { return null; diff --git a/hibernate-core/src/main/java/org/hibernate/Interceptor.java b/hibernate-core/src/main/java/org/hibernate/Interceptor.java index 684b984d32..24c02bbab6 100644 --- a/hibernate-core/src/main/java/org/hibernate/Interceptor.java +++ b/hibernate-core/src/main/java/org/hibernate/Interceptor.java @@ -168,36 +168,9 @@ public interface Interceptor { * @return true if the user modified the state in any way. * * @throws CallbackException Thrown if the interceptor encounters any problems handling the callback. - * - * @deprecated use {@link #onSave(Object, Object, Object[], String[], Type[])} */ - @Deprecated - default boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) - throws CallbackException { - return false; - } - - /** - * Called before an object is saved. The interceptor may modify the state, which will be used for - * the SQL INSERT and propagated to the persistent object. - * - * @param entity The entity instance whose state is being inserted - * @param id The identifier of the entity - * @param state The state of the entity which will be inserted - * @param propertyNames The names of the entity properties. - * @param types The types of the entity properties - * - * @return true if the user modified the state in any way. - * - * @throws CallbackException Thrown if the interceptor encounters any problems handling the callback. - */ - default boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) - throws CallbackException { - if (id instanceof Serializable) { - return onSave(entity, (Serializable) id, state, propertyNames, types); - } - return false; - } + boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) + throws CallbackException; /** * Called before an object is deleted. It is not recommended that the interceptor modify the state. diff --git a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedNestedCompositesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/component/empty/EmptyInitializedNestedCompositesTest.java similarity index 50% rename from hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedNestedCompositesTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/component/empty/EmptyInitializedNestedCompositesTest.java index 4980342f95..25449190e7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/component/empty/EmptyInitializedNestedCompositesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/component/empty/EmptyInitializedNestedCompositesTest.java @@ -4,24 +4,26 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.component.empty; +package org.hibernate.orm.test.component.empty; + +import org.hibernate.cfg.Environment; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; import jakarta.persistence.Embeddable; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; -import org.hibernate.Session; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.orm.test.component.empty.ComponentEmptyEmbedded; +import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.junit.Assert.assertNotNull; /** * Tests that an empty embeddable that is nested inside an embeddable is initialized. @@ -29,46 +31,45 @@ import static org.junit.Assert.assertNotNull; * @author Gail Badner */ @TestForIssue(jiraKey = "HHH-11926") -public class EmptyInitializedNestedCompositesTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { ComponentEmptyNestedEmbeddedOwner.class }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - configuration.getProperties().put( Environment.CREATE_EMPTY_COMPOSITES_ENABLED, Boolean.valueOf( true ) ); - } +@DomainModel( + annotatedClasses = EmptyInitializedNestedCompositesTest.ComponentEmptyNestedEmbeddedOwner.class +) +@SessionFactory +@ServiceRegistry( + settings = @Setting(name = Environment.CREATE_EMPTY_COMPOSITES_ENABLED, value = "true") +) +public class EmptyInitializedNestedCompositesTest { /** * Test empty nested composite initialization. */ @Test - @FailureExpected( jiraKey = "HHH-11926" ) - public void testCompositesEmpty() { - Session s = openSession(); - try { - s.getTransaction().begin(); + public void testCompositesEmpty(SessionFactoryScope scope) { - ComponentEmptyNestedEmbeddedOwner owner = new ComponentEmptyNestedEmbeddedOwner(); - s.persist( owner ); + scope.inSession( + session -> { + session.beginTransaction(); + try { + ComponentEmptyNestedEmbeddedOwner owner = new ComponentEmptyNestedEmbeddedOwner(); + session.persist( owner ); - s.flush(); - s.getTransaction().commit(); + session.flush(); + session.getTransaction().commit(); - s.clear(); - s.getTransaction().begin(); - owner = s.get( ComponentEmptyNestedEmbeddedOwner.class, owner.getId() ); - assertNotNull( owner.getEmbedded() ); - assertNotNull( owner.getEmbedded().getNestedEmbedded() ); - - s.getTransaction().rollback(); - } - finally { - s.close(); - } + session.clear(); + session.getTransaction().begin(); + owner = session.get( ComponentEmptyNestedEmbeddedOwner.class, owner.getId() ); + assertNotNull( owner.getEmbedded() ); + assertNotNull( owner.getEmbedded().getNestedEmbedded() ); + session.getTransaction().commit(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + ); } @Entity(name = "EmptyNestedOwner") diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/CollectionInterceptor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/CollectionInterceptor.java similarity index 83% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/CollectionInterceptor.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/CollectionInterceptor.java index bc3a23282f..9eae751db6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/CollectionInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/CollectionInterceptor.java @@ -6,7 +6,7 @@ */ //$Id: CollectionInterceptor.java 7700 2005-07-30 05:02:47Z oneovthafew $ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import java.io.Serializable; import org.hibernate.EmptyInterceptor; @@ -19,7 +19,7 @@ public class CollectionInterceptor extends EmptyInterceptor { return false; } - public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { + public boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) { ( (User) entity ).getActions().add("created"); return false; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/Image.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Image.hbm.xml similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/Image.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Image.hbm.xml index 0a94f2df1a..5baf57c6e1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/Image.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Image.hbm.xml @@ -9,7 +9,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/Image.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Image.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/Image.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Image.java index 24f515ce7e..08f25b87cb 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/Image.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Image.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; public class Image { diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InstantiateInterceptor.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InstantiateInterceptor.java index e4e72d4a6b..c0af9fab61 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InstantiateInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InstantiateInterceptor.java @@ -8,7 +8,7 @@ /** * @author Gail Badner */ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import org.hibernate.CallbackException; import org.hibernate.EmptyInterceptor; @@ -23,7 +23,7 @@ public class InstantiateInterceptor extends EmptyInterceptor { @Override public Object instantiate(String entityName, RepresentationMode entityMode, Object id) throws CallbackException { - if ( ! "org.hibernate.test.interceptor.User".equals( entityName ) ) { + if ( ! "org.hibernate.orm.test.interceptor.User".equals( entityName ) ) { return null; } // Simply inject a sample string into new instances diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorNonNullTransactionTest.java similarity index 99% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorNonNullTransactionTest.java index 1e72c3aaa1..16de98a16f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorNonNullTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorNonNullTransactionTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorTest.java similarity index 77% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorTest.java index d14e5f032f..75a303a7c2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorTest.java @@ -4,10 +4,12 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; + import jakarta.persistence.PersistenceException; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; + import java.io.Serializable; import java.util.LinkedList; import java.util.List; @@ -22,8 +24,10 @@ import org.hibernate.Interceptor; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.TransactionException; + import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + import org.hibernate.type.Type; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; @@ -44,21 +48,26 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { return new String[] { "interceptor/User.hbm.xml", "interceptor/Image.hbm.xml" }; } + @Override + protected String getBaseForMappings() { + return "org/hibernate/orm/test/"; + } + @Test public void testCollectionIntercept() { Session s = openSession( new CollectionInterceptor() ); Transaction t = s.beginTransaction(); - User u = new User("Gavin", "nivag"); - s.persist(u); - u.setPassword("vagni"); + User u = new User( "Gavin", "nivag" ); + s.persist( u ); + u.setPassword( "vagni" ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); - u = (User) s.get(User.class, "Gavin"); + u = s.get( User.class, "Gavin" ); assertEquals( 2, u.getActions().size() ); - s.delete(u); + s.delete( u ); t.commit(); s.close(); } @@ -67,18 +76,18 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { public void testPropertyIntercept() { Session s = openSession( new PropertyInterceptor() ); Transaction t = s.beginTransaction(); - User u = new User("Gavin", "nivag"); - s.persist(u); - u.setPassword("vagni"); + User u = new User( "Gavin", "nivag" ); + s.persist( u ); + u.setPassword( "vagni" ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); - u = (User) s.get(User.class, "Gavin"); + u = s.get( User.class, "Gavin" ); assertNotNull( u.getCreated() ); assertNotNull( u.getLastUpdated() ); - s.delete(u); + s.delete( u ); t.commit(); s.close(); } @@ -89,34 +98,46 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { * causes EntityPersister.findDirty() to return no dirty properties. */ @Test - @TestForIssue( jiraKey = "HHH-1921" ) + @TestForIssue(jiraKey = "HHH-1921") public void testPropertyIntercept2() { Session s = openSession(); Transaction t = s.beginTransaction(); - User u = new User("Josh", "test"); + User u = new User( "Josh", "test" ); s.persist( u ); t.commit(); s.close(); s = openSession( new EmptyInterceptor() { - public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { - currentState[0] = "test"; + @Override + public boolean onFlushDirty( + Object entity, + Serializable id, + Object[] currentState, + Object[] previousState, + String[] propertyNames, + Type[] types) { + for ( int i = 0; i < propertyNames.length; i++ ) { + if ( propertyNames[i].equals( "password" ) ) { + currentState[i] = "test"; + } + } + return true; } } ); t = s.beginTransaction(); - u = ( User ) s.get( User.class, u.getName() ); + u = s.get( User.class, u.getName() ); u.setPassword( "nottest" ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); - u = (User) s.get(User.class, "Josh"); - assertEquals("test", u.getPassword()); - s.delete(u); + u = s.get( User.class, "Josh" ); + assertEquals( "test", u.getPassword() ); + s.delete( u ); t.commit(); s.close(); @@ -137,17 +158,17 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { t.begin(); // Sleep for an amount of time that exceeds the transaction timeout Thread.sleep( WAIT * 1000 ); - try { - // Do something with the transaction and try to commit it - s.persist( new User( "john", "test" ) ); - t.commit(); - fail( "Transaction should have timed out" ); - } - catch (PersistenceException e){ - assertTyping(TransactionException.class, e.getCause()); + try { + // Do something with the transaction and try to commit it + s.persist( new User( "john", "test" ) ); + t.commit(); + fail( "Transaction should have timed out" ); + } + catch (PersistenceException e) { + assertTyping( TransactionException.class, e.getCause() ); assertTrue( "Transaction failed for the wrong reason. Expecting transaction timeout, but found [" + - e.getCause().getMessage() + "]" , + e.getCause().getMessage() + "]", e.getCause().getMessage().contains( "transaction timeout expired" ) ); } @@ -160,7 +181,13 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { Session s = openSession( new EmptyInterceptor() { - public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { + @Override + public boolean onSave( + Object entity, + Object id, + Object[] state, + String[] propertyNames, + Type[] types) { if ( state[0] == null ) { Image.Details detail = new Image.Details(); detail.setPerm1( checkPerm ); @@ -174,7 +201,7 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { s.beginTransaction(); Image i = new Image(); i.setName( "compincomp" ); - i = ( Image ) s.merge( i ); + i = (Image) s.merge( i ); assertNotNull( i.getDetails() ); assertEquals( checkPerm, i.getDetails().getPerm1() ); assertEquals( checkComment, i.getDetails().getComment() ); @@ -183,7 +210,7 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { s = openSession(); s.beginTransaction(); - i = ( Image ) s.get( Image.class, i.getId() ); + i = s.get( Image.class, i.getId() ); assertNotNull( i.getDetails() ); assertEquals( checkPerm, i.getDetails().getPerm1() ); assertEquals( checkComment, i.getDetails().getComment() ); @@ -197,7 +224,7 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { User u = new User( "Gavin", "nivag" ); final StatefulInterceptor statefulInterceptor = new StatefulInterceptor(); - try(Session s = openSession( statefulInterceptor )) { + try (Session s = openSession( statefulInterceptor )) { statefulInterceptor.setSession( s ); Transaction t = s.beginTransaction(); @@ -222,7 +249,7 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { List logs = s.createQuery( criteria ).list(); // List logs = s.createCriteria(Log.class).list(); assertEquals( 2, logs.size() ); - s.delete(u); + s.delete( u ); s.createQuery( "delete from Log" ).executeUpdate(); } @@ -247,14 +274,14 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { s = openSession( initiateInterceptor ); t = s.beginTransaction(); - User merged = ( User ) s.merge( u ); + User merged = (User) s.merge( u ); assertEquals( injectedString, merged.getInjectedString() ); assertEquals( u.getName(), merged.getName() ); assertEquals( u.getPassword(), merged.getPassword() ); merged.setInjectedString( null ); - User loaded = ( User ) s.load(User.class, merged.getName()); + User loaded = s.load( User.class, merged.getName() ); // the session-bound instance was not instantiated by the interceptor, load simply returns it assertSame( merged, loaded ); assertNull( merged.getInjectedString() ); @@ -263,7 +290,7 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { s.flush(); s.evict( merged ); - User reloaded = ( User ) s.load( User.class, merged.getName() ); + User reloaded = s.load( User.class, merged.getName() ); // Interceptor IS called for instantiating the persistent instance associated to the session when using load assertEquals( injectedString, reloaded.getInjectedString() ); assertEquals( u.getName(), reloaded.getName() ); @@ -275,9 +302,9 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { } @Test - @TestForIssue( jiraKey = "HHH-6594" ) + @TestForIssue(jiraKey = "HHH-6594") public void testPrepareStatementIntercept() { - final Queue expectedSQLs = new LinkedList(); + final Queue expectedSQLs = new LinkedList<>(); // Transaction 1 expectedSQLs.add( "insert" ); // Transaction 2 @@ -285,7 +312,7 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { expectedSQLs.add( "select" ); // Transaction 3 expectedSQLs.add( "select" ); - expectedSQLs.add( "select" ); +// expectedSQLs.add( "select" ); expectedSQLs.add( "update" ); // Transaction 4 expectedSQLs.add( "select" ); @@ -295,20 +322,23 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { @Override public String onPrepareStatement(String sql) { assertNotNull( sql ); - String expectedSql = expectedSQLs.poll().toLowerCase(Locale.ROOT); - assertTrue("sql:\n " + sql.toLowerCase(Locale.ROOT) +"\n doesn't start with \n"+expectedSql+"\n", sql.toLowerCase(Locale.ROOT).startsWith( expectedSql ) ); + String expectedSql = expectedSQLs.poll().toLowerCase( Locale.ROOT ); + assertTrue( + "sql:\n " + sql.toLowerCase( Locale.ROOT ) + "\n doesn't start with \n" + expectedSql + "\n", + sql.toLowerCase( Locale.ROOT ).startsWith( expectedSql ) + ); return sql; } }; - Session s = openSession(interceptor); + Session s = openSession( interceptor ); Transaction t = s.beginTransaction(); User u = new User( "Lukasz", "Antoniak" ); s.persist( u ); t.commit(); s.close(); - s = openSession(interceptor); + s = openSession( interceptor ); t = s.beginTransaction(); s.get( User.class, "Lukasz" ); s.createQuery( "from User u" ).list(); @@ -316,13 +346,13 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { s.close(); u.setPassword( "Kinga" ); - s = openSession(interceptor); + s = openSession( interceptor ); t = s.beginTransaction(); s.merge( u ); t.commit(); s.close(); - s = openSession(interceptor); + s = openSession( interceptor ); t = s.beginTransaction(); s.delete( u ); t.commit(); @@ -346,9 +376,11 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase { User u = new User( "Kinga", "Mroz" ); s.persist( u ); t.commit(); - }catch (TransactionException e){ + } + catch (TransactionException e) { assertTrue( e.getCause() instanceof AssertionFailure ); - }finally { + } + finally { s.close(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorTransactionEventTest.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorTransactionEventTest.java index ee648088fb..42385e9f9f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/InterceptorTransactionEventTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/InterceptorTransactionEventTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import org.hibernate.EmptyInterceptor; import org.hibernate.Interceptor; @@ -30,7 +30,7 @@ import static org.junit.Assert.assertTrue; public class InterceptorTransactionEventTest extends BaseCoreFunctionalTestCase { @Test - public void testTransactionEvents() throws Exception { + public void testTransactionEvents() { LoggingInterceptor interceptor = new LoggingInterceptor(); Session s = openSession(interceptor); diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/Log.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Log.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/Log.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Log.java index 1fc12be647..6af84f33a6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/Log.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/Log.java @@ -6,7 +6,7 @@ */ //$Id: Log.java 7700 2005-07-30 05:02:47Z oneovthafew $ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import java.util.Calendar; public class Log { diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/PropertyInterceptor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/PropertyInterceptor.java similarity index 81% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/PropertyInterceptor.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/PropertyInterceptor.java index 18711fcf56..e4c155bbf3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/PropertyInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/PropertyInterceptor.java @@ -6,7 +6,7 @@ */ //$Id: PropertyInterceptor.java 7700 2005-07-30 05:02:47Z oneovthafew $ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import java.io.Serializable; import java.util.Calendar; @@ -15,12 +15,14 @@ import org.hibernate.type.Type; public class PropertyInterceptor extends EmptyInterceptor { + @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { currentState[1] = Calendar.getInstance(); return true; } - public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { + @Override + public boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) { state[2] = Calendar.getInstance(); return true; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/StatefulInterceptor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/StatefulInterceptor.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/StatefulInterceptor.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/StatefulInterceptor.java index f3dc209a68..60fac9116d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/StatefulInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/StatefulInterceptor.java @@ -6,7 +6,7 @@ */ //$Id: StatefulInterceptor.java 7701 2005-07-30 05:07:01Z oneovthafew $ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; @@ -21,14 +21,16 @@ public class StatefulInterceptor extends EmptyInterceptor { private Session session; private List list = new ArrayList(); - - public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { + + @Override + public boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) { if ( !(entity instanceof Log) ) { list.add( new Log( "insert", (String) id, entity.getClass().getName() ) ); } return false; } + @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { if ( !(entity instanceof Log) ) { list.add( new Log( "update", (String) id, entity.getClass().getName() ) ); @@ -36,6 +38,7 @@ public class StatefulInterceptor extends EmptyInterceptor { return false; } + @Override public void postFlush(Iterator entities) { if ( list.size()>0 ) { for ( Iterator iter = list.iterator(); iter.hasNext(); ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/User.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/User.hbm.xml similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/User.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/User.hbm.xml index 8eda4de3b0..6e8113cd23 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/User.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/User.hbm.xml @@ -15,7 +15,7 @@ --> + package="org.hibernate.orm.test.interceptor"> diff --git a/hibernate-core/src/test/java/org/hibernate/test/interceptor/User.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/User.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/interceptor/User.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/User.java index 0a07903e8f..dc3d06178c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interceptor/User.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interceptor/User.java @@ -6,7 +6,7 @@ */ //$Id: User.java 7700 2005-07-30 05:02:47Z oneovthafew $ -package org.hibernate.test.interceptor; +package org.hibernate.orm.test.interceptor; import java.util.Calendar; import java.util.HashSet; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Document.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Document.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Document.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Document.java index a2737afd08..7642efd2a6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Document.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Document.java @@ -6,7 +6,7 @@ */ //$Id: Document.java 4407 2004-08-22 01:20:08Z oneovthafew $ -package org.hibernate.test.interfaceproxy; +package org.hibernate.orm.test.interfaceproxy; import java.sql.Blob; import java.util.Calendar; diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/DocumentImpl.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/DocumentImpl.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/DocumentImpl.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/DocumentImpl.java index 758e9278eb..990413da2d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/DocumentImpl.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/DocumentImpl.java @@ -6,7 +6,7 @@ */ //$Id: DocumentImpl.java 4407 2004-08-22 01:20:08Z oneovthafew $ -package org.hibernate.test.interfaceproxy; +package org.hibernate.orm.test.interfaceproxy; import java.sql.Blob; import java.util.Calendar; diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/DocumentInterceptor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/DocumentInterceptor.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/DocumentInterceptor.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/DocumentInterceptor.java index a356785be9..d1c3f1cca6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/DocumentInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/DocumentInterceptor.java @@ -6,7 +6,7 @@ */ //$Id: DocumentInterceptor.java 7860 2005-08-11 21:58:23Z oneovthafew $ -package org.hibernate.test.interfaceproxy; +package org.hibernate.orm.test.interfaceproxy; import java.io.Serializable; import java.util.Calendar; @@ -40,7 +40,7 @@ public class DocumentInterceptor implements Interceptor { } } - public boolean onSave(Object entity, Serializable id, Object[] state, + public boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException { if ( entity instanceof Document ) { state[3] = state[2] = Calendar.getInstance(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Folder.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Folder.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Folder.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Folder.java index 6180b813f7..7e5cf00b02 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Folder.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Folder.java @@ -6,7 +6,7 @@ */ //$Id: Folder.java 4407 2004-08-22 01:20:08Z oneovthafew $ -package org.hibernate.test.interfaceproxy; +package org.hibernate.orm.test.interfaceproxy; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/FolderImpl.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/FolderImpl.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/FolderImpl.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/FolderImpl.java index 8e2c0d6923..1d9d25ab5a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/FolderImpl.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/FolderImpl.java @@ -6,7 +6,7 @@ */ //$Id: FolderImpl.java 4407 2004-08-22 01:20:08Z oneovthafew $ -package org.hibernate.test.interfaceproxy; +package org.hibernate.orm.test.interfaceproxy; /** diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/InterfaceProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/InterfaceProxyTest.java new file mode 100644 index 0000000000..456422afc1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/InterfaceProxyTest.java @@ -0,0 +1,122 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.interfaceproxy; + +import org.hibernate.Session; + +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + + +/** + * @author Gavin King + */ +@DomainModel( + xmlMappings = { "org/hibernate/orm/test/interfaceproxy/Item.hbm.xml" } +) +@SessionFactory +public class InterfaceProxyTest { + + @Test + @RequiresDialectFeature( + feature = DialectFeatureChecks.SupportsExpectedLobUsagePattern.class, + comment = "database/driver does not support expected LOB usage pattern" + ) + public void testInterfaceProxies(SessionFactoryScope scope) { + Document doc = new DocumentImpl(); + SecureDocument doc2 = new SecureDocumentImpl(); + try (Session session = openSession( scope )) { + session.beginTransaction(); + try { + doc.setName( "Hibernate in Action" ); + doc.setContent( session.getLobHelper().createBlob( "blah blah blah".getBytes() ) ); + session.save( doc ); + doc2.setName( "Secret" ); + doc2.setContent( session.getLobHelper().createBlob( "wxyz wxyz".getBytes() ) ); + // SybaseASE15Dialect only allows 7-bits in a byte to be inserted into a tinyint + // column (0 <= val < 128) + doc2.setPermissionBits( (byte) 127 ); + doc2.setOwner( "gavin" ); + session.save( doc2 ); + session.getTransaction().commit(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + Long did = doc.getId(); + Long d2id = doc2.getId(); + + try (Session session = openSession( scope )) { + session.beginTransaction(); + try { + Document d = (Document) session.load( ItemImpl.class, did ); + assertEquals( did, d.getId() ); + assertEquals( "Hibernate in Action", d.getName() ); + assertNotNull( d.getContent() ); + + SecureDocument d2 = (SecureDocument) session.load( ItemImpl.class, d2id ); + assertEquals( d2id, d2.getId() ); + assertEquals( "Secret", d2.getName() ); + assertNotNull( d2.getContent() ); + + session.clear(); + + d = session.load( DocumentImpl.class, did ); + assertEquals( did, d.getId() ); + assertEquals( "Hibernate in Action", d.getName() ); + assertNotNull( d.getContent() ); + + d2 = session.load( SecureDocumentImpl.class, d2id ); + assertEquals( d2id, d2.getId() ); + assertEquals( "Secret", d2.getName() ); + assertNotNull( d2.getContent() ); + assertEquals( "gavin", d2.getOwner() ); + + //s.clear(); + + d2 = session.load( SecureDocumentImpl.class, did ); + assertEquals( did, d2.getId() ); + assertEquals( "Hibernate in Action", d2.getName() ); + assertNotNull( d2.getContent() ); + + try { + d2.getOwner(); //CCE + fail( "ClassCastException expected" ); + } + catch (ClassCastException cce) { + //correct + } + + session.createQuery( "delete ItemImpl" ).executeUpdate(); + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + } + + private Session openSession(SessionFactoryScope scope) { + return scope.getSessionFactory() + .withOptions() + .interceptor( new DocumentInterceptor() ) + .openSession(); + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Item.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Item.hbm.xml similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Item.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Item.hbm.xml index 165f6942d9..5c0c7b4a8e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/Item.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/interfaceproxy/Item.hbm.xml @@ -17,7 +17,7 @@ --> + package="org.hibernate.orm.test.interfaceproxy"> . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import java.io.Serializable; import java.util.HashSet; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/DataPoint.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/DataPoint.hbm.xml similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/DataPoint.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/DataPoint.hbm.xml index 98ac07db21..d32238f525 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/DataPoint.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/DataPoint.hbm.xml @@ -10,7 +10,7 @@ "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> + package="org.hibernate.orm.test.proxy"> diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/DataPoint.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/DataPoint.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/DataPoint.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/DataPoint.java index d39c45e2d5..7803ecc1a5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/DataPoint.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/DataPoint.java @@ -6,7 +6,7 @@ */ //$Id: DataPoint.java 10223 2006-08-04 20:29:21Z steve.ebersole@jboss.com $ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import java.io.Serializable; import java.math.BigDecimal; diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/Info.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/Info.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/Info.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/Info.java index c3fd5e4812..3f31be062e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/Info.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/Info.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java index 030ccf1763..fdefafe181 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/MissingSetterWithEnhancementTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import jakarta.persistence.Column; import jakarta.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/Owner.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/Owner.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/Owner.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/Owner.java index 630ef1fd4a..78260cea63 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/Owner.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/Owner.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import java.io.Serializable; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/ProxyTest.java similarity index 99% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/ProxyTest.java index 171015cca0..ba65ca6197 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/ProxyTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import java.math.BigDecimal; import java.util.List; @@ -42,6 +42,11 @@ public class ProxyTest extends BaseCoreFunctionalTestCase { return new String[] { "proxy/DataPoint.hbm.xml" }; } + @Override + protected String getBaseForMappings() { + return "org/hibernate/orm/test/"; + } + @Override public void configure(Configuration cfg) { super.configure( cfg ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/interceptor/ProxyInterceptor.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/interceptor/ProxyInterceptor.java index 975a16130f..cb74601393 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/interceptor/ProxyInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/interceptor/ProxyInterceptor.java @@ -31,6 +31,7 @@ public class ProxyInterceptor extends EmptyInterceptor { * @param object The presumed entity instance. * @return The entity name (pointing to the proper entity mapping). */ + @Override public String getEntityName(Object object) { String entityName = ProxyHelper.extractEntityName( object ); if ( entityName == null ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java index 15a9a7051f..55e1cc6bbe 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java @@ -19,6 +19,7 @@ public class EntityNameInterceptor extends EmptyInterceptor { * @param object The presumed entity instance. * @return The entity name (pointing to the proper entity mapping). */ + @Override public String getEntityName(Object object) { String entityName = ProxyHelper.extractEntityName( object ); if ( entityName == null ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/InterfaceProxyTest.java b/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/InterfaceProxyTest.java deleted file mode 100644 index db0ec6ea72..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/interfaceproxy/InterfaceProxyTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.interfaceproxy; - -import org.hibernate.Session; -import org.hibernate.Transaction; - -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - - -/** - * @author Gavin King - */ -public class InterfaceProxyTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return new String[] { "interfaceproxy/Item.hbm.xml" }; - } - - @Test - @RequiresDialectFeature( - value = DialectChecks.SupportsExpectedLobUsagePattern.class, - comment = "database/driver does not support expected LOB usage pattern" - ) - public void testInterfaceProxies() { - Session s = openSession( new DocumentInterceptor() ); - Transaction t = s.beginTransaction(); - Document d = new DocumentImpl(); - d.setName("Hibernate in Action"); - d.setContent( s.getLobHelper().createBlob( "blah blah blah".getBytes() ) ); - Long did = (Long) s.save(d); - SecureDocument d2 = new SecureDocumentImpl(); - d2.setName("Secret"); - d2.setContent( s.getLobHelper().createBlob( "wxyz wxyz".getBytes() ) ); - // SybaseASE15Dialect only allows 7-bits in a byte to be inserted into a tinyint - // column (0 <= val < 128) - d2.setPermissionBits( (byte) 127 ); - d2.setOwner("gavin"); - Long d2id = (Long) s.save(d2); - t.commit(); - s.close(); - - s = openSession( new DocumentInterceptor() ); - t = s.beginTransaction(); - d = (Document) s.load(ItemImpl.class, did); - assertEquals( did, d.getId() ); - assertEquals( "Hibernate in Action", d.getName() ); - assertNotNull( d.getContent() ); - - d2 = (SecureDocument) s.load(ItemImpl.class, d2id); - assertEquals( d2id, d2.getId() ); - assertEquals( "Secret", d2.getName() ); - assertNotNull( d2.getContent() ); - - s.clear(); - - d = (Document) s.load(DocumentImpl.class, did); - assertEquals( did, d.getId() ); - assertEquals( "Hibernate in Action", d.getName() ); - assertNotNull( d.getContent() ); - - d2 = (SecureDocument) s.load(SecureDocumentImpl.class, d2id); - assertEquals( d2id, d2.getId() ); - assertEquals( "Secret", d2.getName() ); - assertNotNull( d2.getContent() ); - assertEquals( "gavin", d2.getOwner() ); - - //s.clear(); - - d2 = (SecureDocument) s.load(SecureDocumentImpl.class, did); - assertEquals( did, d2.getId() ); - assertEquals( "Hibernate in Action", d2.getName() ); - assertNotNull( d2.getContent() ); - - try { - d2.getOwner(); //CCE - assertFalse(true); - } - catch (ClassCastException cce) { - //correct - } - - s.createQuery( "delete ItemImpl" ).executeUpdate(); - t.commit(); - s.close(); - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/test/mixed/DocumentInterceptor.java b/hibernate-core/src/test/java/org/hibernate/test/mixed/DocumentInterceptor.java index bcea7fafde..62b99bba95 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/mixed/DocumentInterceptor.java +++ b/hibernate-core/src/test/java/org/hibernate/test/mixed/DocumentInterceptor.java @@ -46,7 +46,7 @@ public class DocumentInterceptor implements Interceptor { } public boolean onSave( - Object entity, Serializable id, Object[] state, + Object entity, Object id, Object[] state, String[] propertyNames, Type[] types ) throws CallbackException { if ( entity instanceof Document ) { diff --git a/migration-guide.adoc b/migration-guide.adoc index fd0458815a..bb621e3bed 100644 --- a/migration-guide.adoc +++ b/migration-guide.adoc @@ -180,6 +180,20 @@ where `param` is bound to `entityManager.getReference(EntityClass.class, 1)`. ** 'hibernate.classLoader.application', 'hibernate.classLoader.resources', 'hibernate.classLoader.hibernate' and 'hibernate.classLoader.environment': use 'hibernate.classLoaders' instead. ** 'hibernate.hbm2dll.create_namespaces': use 'jakarta.persistence.create-database-schemas' or 'hibernate.hbm2ddl.create_namespaces' +=== org.hibernate.Interceptors + +The method +``` +boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) +``` + +has been removed in favour of +``` +boolean onSave(Object entity, Object id, Object[] state, String[] propertyNames, Type[] types) +``` + + + === Fetch behaviour change We changed the way we detect circularity, we do not follow anymore a deep first detection, so what happens is that in a model like