Home Hibernate Hibernate4 hibernate metamodel generator
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

hibernate metamodel generator

Advertisement
In this section, you will learn about 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);
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 31, 2012

Ask Questions?    Discuss: hibernate metamodel generator  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com