hibernate metamodel generator

In this section, you will learn about hibernate metamodel generator.

hibernate metamodel generator

hibernate metamodel generator

In this section, you will learn about hibernate metamodel generator.

A new type safe Criteria API is specified by JPA2 which permits structuring criteria queries in a strongly-typed manner by employing metamodel objects to provide type safety. Automated metamodel generation is crucial for developers. Hibernate Static Metamodel Generator is an annotation processor which create JPA 2 static metamodel classes.

Given below example contains two JPA 2 entities Order and Item with the metamodel class Order_ and a typesafe query :

Order Entity

@Entity

public class Order {

@Id 

@GeneratedValue

Integer id;



@ManyToOne 

Customer customer;



@OneToMany 

Set<Item> items;

BigDecimal totalCost;



// standard setter/getter methods

...

}

Item Entity

@Entity 

public class Item { 

@Id

@GeneratedValue

Integer id;



int quantity;



@ManyToOne

Order order;



// standard setter/getter methods

...

}

Metamodel class Order_

@StaticMetamodel(Order.class)

public class Order_ {

public static volatile SingularAttribute<Order, Integer> id;

public static volatile SingularAttribute<Order, Customer> customer;

public static volatile SetAttribute<Order, Item> items;

public static volatile SingularAttribute<Order, BigDecimal> totalCost;

}

Typesafe criteria query

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);