Jpa + Spring - automatically setting transient field value after read from DB
Asked Answered
M

2

6

what's the best solution to set a value for a field marked @Transient after the entity has been read from the data source?

I'm using EclipseLink and I'm trying the DescriptorEventAdapter with his postBuild event solution because I need also to get the default value using a Spring bean (obviuosly using DI), but I would know if there is any simpler solution that I'm missing.

Thanks in advance

Morphophonemics answered 30/8, 2013 at 15:49 Comment(0)
C
6

Here's the simple approach if you're using a repository or DAO:

@Repository
class YourRepository {

    @Autowired
    private Bean bean;

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional(readOnly = true)
    public YourEntity find(..) {
        YourEntity entity = lookupUsingEntityManager();
        entity.transientField = bean.getDefaultValue();
        return entity;
    }
}

Here's another approach if you are using active record -style entities:

@Entity
class YourEntity {

    @Transient
    public Object field;

    @PostLoad
    public void populateField() {
        field = new BeanHolder().bean.getDefaultValueForField();
    }

    @Configurable
    private static class BeanHolder {
        @Autowired private Bean bean;
    }
}

Mind the semi-pseudo-code. Note that the latter approach works only if you use compile- or load-time AspectJ weaving with <context:spring-configured />.

Chalco answered 31/8, 2013 at 21:38 Comment(1)
Hi, thanks for your interest. The first approach is the one I'm using just now but using Spring Data JPA (and its pre written repositories facilities) and I don't want to override all crud methods (findOne, findAll, save), so I thought to act only at entity level. The second approach could be ok, but there is no solution without AspectJ weaving (e.g. using EclipseLink specific API)?Morphophonemics
W
0

You got entity which has transient field and the value is always taken from service using DI?

  1. What is the purpose of the field? It's used for some calculation within any entity method?
  2. Such calculation should probably use service's method to obtain the value.
  3. As value from any service is used, I'm not sure whether such calculation (method) belong into entity.

Note that entity and service has completely different lifecycle. The value is changing in the time so it does not make the sense to supply the value in entity's factory at the beginning of it's life?

Weathering answered 30/8, 2013 at 19:53 Comment(3)
Hi, thanks for your interest. I'm developing a multitenant (multischema) app so I want to have in the entity loaded a field containing the DB schema from where I loaded the entity data (that I use later, e.g. in the GUI).Morphophonemics
Have you looked at EclipseLink's built-in support for multitenancy (e.g. wiki.eclipse.org/EclipseLink/Examples/JPA/Multitenant)?Chalco
Yes, but there is a bug for the multi-tenant using multi-schema, I filed a bug, so i decided to proceed in another way.Morphophonemics

© 2022 - 2024 — McMap. All rights reserved.