Hi,
I have another issue with transient fields. In my case I want to initialize transient field by constructor when loading from repository but it is null regardless. I have similar situation on transient JPA entities, where it is ok.
This is my simplified test:
I have now workaround by lazy initialization when first PropertyChangeListener is added and null check before firePropertyChange. Is there another solution or some plans to support initialization on construct?Code:import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.Matchers.sameInstance; import static org.junit.Assert.assertThat; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import javax.persistence.Transient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.neo4j.annotation.GraphId; import org.springframework.data.neo4j.annotation.NodeEntity; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.node.Neo4jHelper; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TransientTest { @Autowired private Neo4jTemplate template; @BeforeTransaction public void setUp() { Neo4jHelper.cleanDb(template); } @Test @Transactional public void transientTest(){ TestEntity entity = new TestEntity(); entity.setValue("value"); TestEntity persistedEntity = template.save(entity); assertThat(persistedEntity.getId(), notNullValue()); assertThat(persistedEntity.pcs, notNullValue()); assertThat(persistedEntity.pcs, not(sameInstance(entity.pcs))); assertThat(persistedEntity.getValue(), equalTo(entity.getValue())); } @NodeEntity public static class TestEntity { @Transient private PropertyChangeSupport pcs = new PropertyChangeSupport(this); @GraphId Long id; String value; public Long getId() { return id; } public void setId(Long id) { Long oldValue = this.id; this.id = id; pcs.firePropertyChange("id", oldValue, id); } public String getValue() { return value; } public void setValue(String value) { String oldValue = this.value; this.value = value; pcs.firePropertyChange("value", oldValue, value); } public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } } }


Reply With Quote