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