1

The new feature for type safe queries generates a metamodel for the classes, but why these fields aren't final instead of volatile ?

public static volatile SingularAttribute<Presentation, String> topic;
tshepang
  • 12,111
  • 21
  • 91
  • 136
Cosmin Cosmin
  • 1,526
  • 1
  • 16
  • 34

1 Answers1

1

Fields in metamodel class cannot be final, because static final field should be assigned:

  1. together with the declaration or
  2. in static initializer.

This cannot be done, because value that should be assigned is not known to the one that creates metamodel class. Values are assigned by provider when EntityManagerFactory is created.

Consequence is that attributes must be volatile to guarantee that other threads can see values assigned by provider.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • I don't know very much about volatile. But why does the fields need a specific value ? Why can't they assign it a random implementation of SingularAttribute. For example new SingularAttributeImpl(); at generation step. The criteria query needs to know just the type (IS A SingularAttribute, CollectionAttribute and so on) and the generics parameters. Is it because of how the internals of the provider works ? – Cosmin Cosmin Jun 01 '12 at 07:48
  • Now that I look better at the API for SingularAttribute I can see some additional data that will be set by the provider later. So the generation tool is a little dumber, it doesn't process the annotations / xml to provide all the information needed. It delegates to the provider to do it later (so basically isn't very static :P). Thanks. – Cosmin Cosmin Jun 01 '12 at 07:53