Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @StaticMetamodel and SingularAttribute<Obj,Obj>?

I am trying to figure out this code for about two hours now, for example in below class what are these fields representing?

import java.util.Date; import javax.persistence.metamodel.ListAttribute; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.StaticMetamodel; import java.util.UUID;  @StaticMetamodel(Address.class) public class Address_ extends  {     public static volatile SingularAttribute<Address, Long> id;     public static volatile SingularAttribute<Address, UUID> personId;     public static volatile SingularAttribute<Address, Person> person; } 

The Address.class is a java class which has the following definition:

@Entity @Table(name = "address", schema = "public") public class Address{     private Long id;     private Person person;     private UUID personId;     //.... } 

Can you, please, explain what are the @StaticMetamodel and @SingularAttribute annotations used for? It might be simple but I can't understand.

like image 545
MoienGK Avatar asked Oct 31 '13 10:10

MoienGK


People also ask

What is SingularAttribute?

Instances of the type SingularAttribute represents persistent single-valued properties or fields. Since: Java Persistence 2.0. Nested Class Summary. Nested classes/interfaces inherited from interface javax.persistence.metamodel.Attribute.

What is static metamodel?

A static metamodel is a series of classes that "mirror" the entities and embeddables in the domain model and provide static access to the metadata about the mirrored class's attributes.

What is jpa static metamodel?

What is it about? JPA 2 defines a typesafe Criteria API which allows Criteria queries to be constructed in a strongly-typed manner, utilizing so called static metamodel classes. For developers it is important that the task of the metamodel generation can be automated.


1 Answers

As per documentation:

A static metamodel is a series of classes that "mirror" the entities and embeddables in the domain model and provide static access to the metadata about the mirrored class's attributes.

The static metamodel has the following properties:

  • For each managed class X in package p, a metamodel class X_ in package p is created.
  • 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:

SingularAttribute example:

public static volatile SingularAttribute<X, Y> y; 

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 } 

and the Item entity:

@Entity   public class Item {      @Id     @GeneratedValue     Integer id;      int quantity;      @ManyToOne     Order order;      // accessors } 

Here's a typesafe criteria query, build with the Criteria API:

CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Order> cq = cb.createQuery(Order.class); SetJoin<Order, Item> itemNode = cq.from(Order.class).join(Order_.items); cq.where(cb.equal(itemNode.get(Item_.id), 5)).distinct(true); 

Note the usage of Item_.id and Order_.item. Those access statically the static meta-model properties (which mirror the entity properties) and this way it's ensured that the query is build properly.

like image 135
Konstantin Yovkov Avatar answered Oct 05 '22 20:10

Konstantin Yovkov