0

I recently explore an article on hibernate best practice over here https://thoughts-on-java.org/hibernate-best-practices/

I came across a topic

Use JPA Metamodel when working with Criteria API

@Generated(value = “org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor”)
@StaticMetamodel(Author.class)
public abstract class Author_ {

  public static volatile SingularAttribute<Author, String> firstName;
  public static volatile SingularAttribute<Author, String> lastName;
  public static volatile SetAttribute<Author, Book> books;
  public static volatile SingularAttribute<Author, Long> id;
  public static volatile SingularAttribute<Author, Integer> version;

}

I am still not able to understand what is the role of volatile over there as we know that values of volatile variable will never be cached and all writes and reads will be done to and from the main memory.

please can anyone clarify me, what exactly we want to achieve by using the volatile keyword? Don't you think the above code causes performance issues.

user10753505
  • 99
  • 11

1 Answers1

0

I think that volatile keyword here is generated by some kind of automatic generation, like an java eclipse project configured with jpa facet.

In fact, in my project eclipse metamodel (Dali in my case) generator do exactly same output:

@Generated(value="Dali", date="2018-10-22T14:37:09.798+0200")
@StaticMetamodel(AnaSchema.class)
public class AnaSchema_ {
    public static volatile SingularAttribute<AnaSchema, Long> id;
    public static volatile SingularAttribute<AnaSchema, String> code;
    public static volatile SingularAttribute<AnaSchema, String> description;
}

Interesting also hibernate metamodel generation do the same, I think in this context because JVM does not guarantee where values are stored: in main memory or on cpu cache, causing troubles in edge cases and multi thread environment.

A complete explanation is here

Vokail
  • 630
  • 8
  • 27