Search in sources :

Example 1 with SetterFieldImpl

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

the class PropertyAccessEnhancedImpl method resolveEnhancedSetterForField.

private static Setter resolveEnhancedSetterForField(Class<?> containerClass, String propertyName, Field field) {
    try {
        String enhancedSetterName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + propertyName;
        Method enhancedSetter = containerClass.getDeclaredMethod(enhancedSetterName, field.getType());
        enhancedSetter.setAccessible(true);
        return new EnhancedSetterImpl(containerClass, propertyName, enhancedSetter);
    } catch (NoSuchMethodException e) {
        // enhancedSetter = null --- field not enhanced: fallback to reflection using the field
        return new SetterFieldImpl(containerClass, propertyName, field);
    }
}
Also used : EnhancedSetterImpl(org.hibernate.property.access.spi.EnhancedSetterImpl) Method(java.lang.reflect.Method) SetterFieldImpl(org.hibernate.property.access.spi.SetterFieldImpl)

Example 2 with SetterFieldImpl

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

the class GetterSetterSerializationTest method testPrivateFieldSetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testPrivateFieldSetter() throws Exception {
    AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final Setter setter = new SetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    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 : GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) SetterFieldImpl(org.hibernate.property.access.spi.SetterFieldImpl) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SetterFieldImpl (org.hibernate.property.access.spi.SetterFieldImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Method (java.lang.reflect.Method)1 EnhancedSetterImpl (org.hibernate.property.access.spi.EnhancedSetterImpl)1 Getter (org.hibernate.property.access.spi.Getter)1 GetterFieldImpl (org.hibernate.property.access.spi.GetterFieldImpl)1 Setter (org.hibernate.property.access.spi.Setter)1 AnEntity (org.hibernate.serialization.entity.AnEntity)1 PK (org.hibernate.serialization.entity.PK)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1