Search in sources :

Example 1 with GetterMethodImpl

use of org.hibernate.property.access.spi.GetterMethodImpl in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testProtectedMethodSetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodSetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
    final Setter setter = new SetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findSetterMethod(AnEntity.class, "pk", PK.class));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(setter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Setter setterClone = (Setter) ois.readObject();
    final PK pkNew = new PK(2L);
    setterClone.set(entity, pkNew, null);
    assertSame(pkNew, getter.get(entity));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) SetterMethodImpl(org.hibernate.property.access.spi.SetterMethodImpl) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with GetterMethodImpl

use of org.hibernate.property.access.spi.GetterMethodImpl in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testProtectedMethodGetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodGetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(getter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Getter getterClone = (Getter) ois.readObject();
    assertSame(getter.get(entity), getterClone.get(entity));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with GetterMethodImpl

use of org.hibernate.property.access.spi.GetterMethodImpl in project hibernate-orm by hibernate.

the class AccessMappingTest method testDefaultPropertyAccessIsInherited.

@Test
public void testDefaultPropertyAccessIsInherited() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Horse.class);
    cfg.addAnnotatedClass(Animal.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        EntityTuplizer tuplizer = factory.getEntityPersister(Animal.class.getName()).getEntityMetamodel().getTuplizer();
        assertTrue("Property access should be used since explicity configured via @Access", tuplizer.getIdentifierGetter() instanceof GetterMethodImpl);
        tuplizer = factory.getEntityPersister(Horse.class.getName()).getEntityMetamodel().getTuplizer();
        assertTrue("Field access should be used since the default access mode gets inherited", tuplizer.getGetter(0) instanceof GetterFieldImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityTuplizer(org.hibernate.tuple.entity.EntityTuplizer) GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) Test(org.junit.Test)

Example 4 with GetterMethodImpl

use of org.hibernate.property.access.spi.GetterMethodImpl in project hibernate-orm by hibernate.

the class AccessMappingTest method testExplicitPropertyAccessAnnotationsWithHibernateStyleOverride.

@Test
public void testExplicitPropertyAccessAnnotationsWithHibernateStyleOverride() throws Exception {
    Configuration cfg = new Configuration();
    Class<?> classUnderTest = Course3.class;
    cfg.addAnnotatedClass(classUnderTest);
    cfg.addAnnotatedClass(Student.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        EntityTuplizer tuplizer = factory.getEntityPersister(classUnderTest.getName()).getEntityMetamodel().getTuplizer();
        assertTrue("Field access should be used.", tuplizer.getIdentifierGetter() instanceof GetterFieldImpl);
        assertTrue("Property access should be used.", tuplizer.getGetter(0) instanceof GetterMethodImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityTuplizer(org.hibernate.tuple.entity.EntityTuplizer) GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) Test(org.junit.Test)

Example 5 with GetterMethodImpl

use of org.hibernate.property.access.spi.GetterMethodImpl in project hibernate-orm by hibernate.

the class AccessMappingTest method testExplicitPropertyAccessAnnotationsOnProperty.

@Test
public void testExplicitPropertyAccessAnnotationsOnProperty() throws Exception {
    Configuration cfg = new Configuration();
    Class<?> classUnderTest = Course2.class;
    cfg.addAnnotatedClass(classUnderTest);
    cfg.addAnnotatedClass(Student.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        EntityTuplizer tuplizer = factory.getEntityPersister(classUnderTest.getName()).getEntityMetamodel().getTuplizer();
        assertTrue("Property access should be used.", tuplizer.getIdentifierGetter() instanceof GetterMethodImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityTuplizer(org.hibernate.tuple.entity.EntityTuplizer) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) Test(org.junit.Test)

Aggregations

GetterMethodImpl (org.hibernate.property.access.spi.GetterMethodImpl)7 Test (org.junit.Test)7 Configuration (org.hibernate.cfg.Configuration)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 EntityTuplizer (org.hibernate.tuple.entity.EntityTuplizer)5 GetterFieldImpl (org.hibernate.property.access.spi.GetterFieldImpl)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Getter (org.hibernate.property.access.spi.Getter)2 AnEntity (org.hibernate.serialization.entity.AnEntity)2 PK (org.hibernate.serialization.entity.PK)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Setter (org.hibernate.property.access.spi.Setter)1 SetterMethodImpl (org.hibernate.property.access.spi.SetterMethodImpl)1