3

I have a class to map a table for using hibernate. There are some variable I want to ignore for mapping to use as constant. And I want to load constant value from properties so I code it like this:

@Transient
@Value("${something.value}")
private int MY_VALUE;

But, the value of MY_VALUE is always set to 0. Can't I use @Transient annotation with @Value annotation? Or I missed something else?

matt b
  • 138,234
  • 66
  • 282
  • 345
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
  • if you remove the `@Transient` annotation, it continues to be set to `0`? – bluefoot Jul 26 '11 at 01:32
  • @bluefoot If I remove that the project throws error, because of this issue http://stackoverflow.com/questions/4662582/how-to-make-hibernate-ignore-class-variables-that-are-not-mapped – Sanghyun Lee Jul 26 '11 at 01:39

3 Answers3

6

Those two annotations belong in different domains.

@Transient belongs to an entity, while @Value belongs to Spring Beans. Entities are managed by JPA / Hibernate, Spring Beans are managed by Spring. It is not a good idea to mix the two.

You could achieve this by using the @Configurable annotation and AspectJ compilation or Load Time Weaving, but I would strongly advise against such a hack. Use a Spring Bean to hold a @Value, not an entity!

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
4

You use @Value to specify a property value to load when Spring creates the bean.

However, if you are using Hibernate to load data from a database, Spring is not instantiating these classes for you. So your @Value annotation has no effect.

I would suggest injecting the @Value into the DAO that loads these entities from Hibernate, something like

public class FooDao {
    @Value("...")
    private int yourConfiguredValue;

    public getFoo() {
        Foo foo = sessionFactory.getCurrentSession().get(...);
        foo.setYourValue(yourConfiguredValue);
        return foo;
    }
}
matt b
  • 138,234
  • 66
  • 282
  • 345
  • I get `foo` as a list like `List foos = ...`. Is it good idea to inject the `@Value` into every objects with loop? – Sanghyun Lee Jul 26 '11 at 01:52
  • 2
    Well if you need to add the value to each object, what other way is there? However I would consider not having this value attached to the entity object at all if it's constant and/or from a properties file - doesn't sound like it's related to *each* instance of Foo, especially if they all have the same value. I'd attach that @Value to some other class. – matt b Jul 26 '11 at 02:00
1

In my scenario I have a class Employee which has relation with class Organization.

I don't want to serialize a whole dependent object(Organization), rather serialize a single parameter of organization(e.g. orgID).

I tried following:

@Transient
@value("#{target.orgId.id}")
private UUID org_Id;

but it didnt work. So i used a simple getter mehtod instead of a field variable as follows:

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "orgID")
private Organization orgId;

@JsonProperty("orgId")
public UUID getOrg_ID() {
    return orgId.getId();
}

it worked and i got simple orgId field in response serialized by Jackson. It seems Jackson work with getters without considering a field variable is declared or not corresponding to that getter method.