Search in sources :

Example 1 with MutabilityPlan

use of org.hibernate.type.descriptor.java.MutabilityPlan in project hibernate-orm by hibernate.

the class ExplicitJavaTypeDescriptorTest method testIt.

@Test
@TestForIssue(jiraKey = "HHH-11098")
public void testIt() {
    // set up test data
    Session session = openSession();
    ((MetamodelImplementor) session.getMetamodel()).getTypeConfiguration().getJavaTypeDescriptorRegistry().addDescriptor(new JavaTypeDescriptorRegistry.FallbackJavaTypeDescriptor(MutableState2.class) {

        @Override
        public MutabilityPlan getMutabilityPlan() {
            return ImmutableMutabilityPlan.INSTANCE;
        }
    });
    session.beginTransaction();
    session.persist(new TheEntity(1));
    session.getTransaction().commit();
    session.close();
    // assertions based on the persist call
    // 1 instead of 0 because of the deep copy call
    assertThat(mutableToDomainCallCount, is(1));
    // 2 instead of 1 because of the deep copy call
    assertThat(mutableToDatabaseCallCount, is(2));
    // logical
    assertThat(immutableToDomainCallCount, is(0));
    // logical
    assertThat(immutableToDatabaseCallCount, is(1));
    // was 1 (like mutable) before the JavaTypeDescriptor registration
    assertThat(immutableMutableToDomainCallCount, is(0));
    // was 2 (like mutable) before the JavaTypeDescriptor registration
    assertThat(immutableMutableToDatabaseCallCount, is(1));
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.delete(session.byId(TheEntity.class).getReference(1));
    session.getTransaction().commit();
    session.close();
}
Also used : MetamodelImplementor(org.hibernate.metamodel.spi.MetamodelImplementor) JavaTypeDescriptorRegistry(org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Session (org.hibernate.Session)1 MetamodelImplementor (org.hibernate.metamodel.spi.MetamodelImplementor)1 TestForIssue (org.hibernate.testing.TestForIssue)1 ImmutableMutabilityPlan (org.hibernate.type.descriptor.java.ImmutableMutabilityPlan)1 JavaTypeDescriptorRegistry (org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry)1 MutabilityPlan (org.hibernate.type.descriptor.java.MutabilityPlan)1 Test (org.junit.Test)1