HHH-11147 - Allow enhanced entities to be returned in a completely uninitialized state
- Add test using IdClass
(cherry picked from commit 2e1d602f68
)
This commit is contained in:
parent
0a17f5ba6d
commit
f96eb35f97
|
@ -150,9 +150,9 @@ public class EntityMetamodel implements Serializable {
|
||||||
nonAggregatedCidMapper = (CompositeType) identifierMapperComponent.getType();
|
nonAggregatedCidMapper = (CompositeType) identifierMapperComponent.getType();
|
||||||
idAttributeNames = new HashSet<>( );
|
idAttributeNames = new HashSet<>( );
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
final Iterator<String> propertyItr = identifierMapperComponent.getPropertyIterator();
|
final Iterator<Property> propertyItr = identifierMapperComponent.getPropertyIterator();
|
||||||
while ( propertyItr.hasNext() ) {
|
while ( propertyItr.hasNext() ) {
|
||||||
idAttributeNames.add( propertyItr.next() );
|
idAttributeNames.add( propertyItr.next().getName() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.bytecode.enhancement.lazy.proxy;
|
package org.hibernate.test.bytecode.enhancement.lazy.proxy;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
|
@ -21,6 +22,7 @@ import javax.persistence.FetchType;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.IdClass;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
import javax.persistence.OneToMany;
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.PersistenceException;
|
import javax.persistence.PersistenceException;
|
||||||
|
@ -97,7 +99,6 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
|
|
||||||
assertEquals( 0, stats.getPrepareStatementCount() );
|
assertEquals( 0, stats.getPrepareStatementCount() );
|
||||||
|
|
||||||
// check that the `#setName` "persisted"
|
|
||||||
assertThat( loadedChild.getId(), is( lastChildID ) );
|
assertThat( loadedChild.getId(), is( lastChildID ) );
|
||||||
assertEquals( 0, stats.getPrepareStatementCount() );
|
assertEquals( 0, stats.getPrepareStatementCount() );
|
||||||
}
|
}
|
||||||
|
@ -112,12 +113,81 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setIdClassTest(){
|
||||||
|
final Statistics stats = sessionFactory().getStatistics();
|
||||||
|
stats.clear();
|
||||||
|
|
||||||
|
inTransaction(
|
||||||
|
session -> {
|
||||||
|
stats.clear();
|
||||||
|
ModelId id = new ModelId();
|
||||||
|
id.setId1( 1L );
|
||||||
|
id.setId2( 2L );
|
||||||
|
Parent parent = session.load( Parent.class, id );
|
||||||
|
|
||||||
|
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) parent;
|
||||||
|
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
|
||||||
|
MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getPrepareStatementCount() );
|
||||||
|
|
||||||
|
parent.getId1();
|
||||||
|
parent.setId1( 1L );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getPrepareStatementCount() );
|
||||||
|
|
||||||
|
assertThat( parent.getId1(), is( 1L ) );
|
||||||
|
assertThat( parent.getId2(), is( 2L ) );
|
||||||
|
|
||||||
|
assertEquals( 0, stats.getPrepareStatementCount() );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
inTransaction(
|
||||||
|
session -> {
|
||||||
|
Child loadedChild = session.load( Child.class, lastChildID );
|
||||||
|
assertThat( loadedChild, is( notNullValue() ) );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = PersistenceException.class)
|
||||||
|
public void updateIdClassTest(){
|
||||||
|
final Statistics stats = sessionFactory().getStatistics();
|
||||||
|
stats.clear();
|
||||||
|
|
||||||
|
inTransaction(
|
||||||
|
session -> {
|
||||||
|
stats.clear();
|
||||||
|
ModelId id = new ModelId();
|
||||||
|
id.setId1( 1L );
|
||||||
|
id.setId2( 2L );
|
||||||
|
Parent parent = session.load( Parent.class, id );
|
||||||
|
|
||||||
|
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) parent;
|
||||||
|
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
|
||||||
|
MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) );
|
||||||
|
|
||||||
|
// should trigger an exception
|
||||||
|
parent.setId1( 3L );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
inTransaction(
|
||||||
|
session -> {
|
||||||
|
Child loadedChild = session.load( Child.class, lastChildID );
|
||||||
|
assertThat( loadedChild, is( notNullValue() ) );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = PersistenceException.class)
|
@Test(expected = PersistenceException.class)
|
||||||
public void updateIdTest() {
|
public void updateIdTest() {
|
||||||
final Statistics stats = sessionFactory().getStatistics();
|
final Statistics stats = sessionFactory().getStatistics();
|
||||||
stats.clear();
|
stats.clear();
|
||||||
|
|
||||||
Long updatedId = new Long( lastChildID + 1 );
|
Long updatedId = lastChildID + 1;
|
||||||
|
|
||||||
inTransaction(
|
inTransaction(
|
||||||
session -> {
|
session -> {
|
||||||
|
@ -128,13 +198,8 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
|
final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
|
||||||
MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) );
|
MatcherAssert.assertThat( interceptor, instanceOf( EnhancementAsProxyLazinessInterceptor.class ) );
|
||||||
|
|
||||||
|
// should trigger an exception
|
||||||
loadedChild.setId( updatedId );
|
loadedChild.setId( updatedId );
|
||||||
|
|
||||||
assertEquals( 0, stats.getPrepareStatementCount() );
|
|
||||||
|
|
||||||
// check that the `#setName` "persisted"
|
|
||||||
assertThat( loadedChild.getId(), is( updatedId ) );
|
|
||||||
assertEquals( 0, stats.getPrepareStatementCount() );
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -144,6 +209,9 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
public void prepare() {
|
public void prepare() {
|
||||||
doInHibernate( this::sessionFactory, s -> {
|
doInHibernate( this::sessionFactory, s -> {
|
||||||
Parent parent = new Parent();
|
Parent parent = new Parent();
|
||||||
|
parent.setId1( 1L );
|
||||||
|
parent.setId2( 2L );
|
||||||
|
|
||||||
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
for ( int i = 0; i < CHILDREN_SIZE; i++ ) {
|
||||||
Child child = new Child();
|
Child child = new Child();
|
||||||
child.setId( new Long( i ) );
|
child.setId( new Long( i ) );
|
||||||
|
@ -171,8 +239,31 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class ModelId implements Serializable {
|
||||||
|
Long id1;
|
||||||
|
|
||||||
|
Long id2;
|
||||||
|
|
||||||
|
public Long getId1() {
|
||||||
|
return id1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId1(Long id1) {
|
||||||
|
this.id1 = id1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId2() {
|
||||||
|
return id2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId2(Long id2) {
|
||||||
|
this.id2 = id2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Entity(name = "Parent")
|
@Entity(name = "Parent")
|
||||||
@Table(name = "PARENT")
|
@Table(name = "PARENT")
|
||||||
|
@IdClass( ModelId.class )
|
||||||
private static class Parent {
|
private static class Parent {
|
||||||
|
|
||||||
String name;
|
String name;
|
||||||
|
@ -181,8 +272,10 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
Long id1;
|
||||||
Long id;
|
|
||||||
|
@Id
|
||||||
|
Long id2;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||||
List<Child> children;
|
List<Child> children;
|
||||||
|
@ -198,6 +291,22 @@ public class SetIdentifierOnAEnhancedProxyTest extends BaseNonConfigCoreFunction
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getId1() {
|
||||||
|
return id1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId1(Long id1) {
|
||||||
|
this.id1 = id1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId2() {
|
||||||
|
return id2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId2(Long id2) {
|
||||||
|
this.id2 = id2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "Person")
|
@Entity(name = "Person")
|
||||||
|
|
Loading…
Reference in New Issue