I have just used org.apache.openjpa.persistence.meta.AnnotationProcessor6
to generate the MetaModel for my JPA2 entities.
@javax.annotation.Generated
(value="org.apache.openjpa.persistence.meta.AnnotationProcessor6",
date="Tue Nov 22 09:49:03 CET 2011")
public class Entity_ {
public static volatile SingularAttribute<Entity,Entity> id;
public static volatile SingularAttribute<Entity,String> value;
public static volatile SingularAttribute<Entity,String> order;
}
Can someone please explain why the attributes are marked volatile in this case?
Thanks.
Generating JPA Metamodel Classes Note, we need to add the target/generated-classes folder to the classpath of our IDE, as by default, the classes will be generated in this folder only.
JPA (Java persistence API) metamodel classes are classes that describe the structure of JPA entity classes (classes used to store database state as Java objects). They enable you to write Java code for creating database queries in a typesafe way.
The static metamodel is useful for creating type-safe queries with the JPA's Criteria API. For example, let's have the following two entities, Order and Item : @Entity public class Order { @Id @GeneratedValue Integer id; @ManyToOne Customer customer; @OneToMany Set<Item> items; BigDecimal totalCost; // accessors }
Canonical MetamodelThe annotation processor produces for every managed entity in the persistence unit a metamodel class based on these rules: For each managed class X in package p, a metamodel class X_ in package p is created.
The thread that sets the static variables might not be the same as the thread that you use to access them, so the volatile
modifier is required to synchronize memory between all threads.
The scenario without volatile
is like this:
null
for the static fieldsnull
for all static fields.Despite the meaning of volatile
keyword and Ingo's answer, it's worth noticing that every JPA generator is required to generate volatile metadata fields (JPA 2.0 FR, 6.2.1.1 Canonical Metamodel).
On page 199 you can read:
For every persistent non-collection-valued attribute y declared by class X, where the type of y is Y, the metamodel class must contain a declaration as follows:
public static volatile SingularAttribute<X, Y>
y;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With