Presentation Hibernate University (Part 1/2)


Java Persistence 2 is closing the gap and standardizes even more persistence related features. What does this mean in the context of the Hibernate project? Generally speaking, what's new in Hibernate? In this session, we will check out the new features of Hibernate and in particular the one coming from Java Persistence 2. They will be explored via live coding sessions.
Published on: 2011-03-01T07:53:27.000Z
Channel: BeJUG (all)
Tags: hibernate
Speakers:

Emmanuel Bernard


After graduating from Supelec (French "Grande Ecole"), Emmanuel has spent a few years in the retail industry where he started to be involved in the ORM space. He joined the Hibernate team 4 years ago and is now a core developer at JBoss, a division of Red Hat. Emmanuel is the lead developer of Hibernate Annotations and Hibernate EntityManager, two key projects on top of Hibernate core implementing the Java Persistence(tm) specification, as well as Hibernate Search and Validator. Emmanuel is a member of the EJB 3.0 expert group and the JSR 303: Bean Validation expert group. He is a regular speaker at various conferences and JUGs, including JavaOne, JBoss World and JAX.

PDF: slides.pdf

Slides:

Hibernate University


Hibernate University Emmanuel Bernard, JBoss by Red Hat Platform Architect but actually doing things Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011

What to expect


What to expect • Get an overview of what’s new in –Core (ORM) –satellite projects (Search, Envers, Validator, Spatial) • Make you discover new features • A bunch of demo driven discovery • Maybe a surprise • Give me feedback http://spkr8.com/t/5631 Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 2

Emmanuel Bernard


Emmanuel Bernard • Work for JBoss by Red Hat • Hibernate * (founder, lead or contributor) • JCP (Bean Validation, Java Persistence) • Founder of Les Cast Codeurs Podcast • Author of Hibernate Search in Action • @emmanuelbernard Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 3

Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc.


Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 4

ORM


ORM a JPA 2 perspective Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011

Mapping


Mapping • All of JPA 2 mapping –standardization of specific annotations • Some interesting features Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 8

Generator - @MapsId


Generator - @MapsId @Entity class Customer { @Id UserId userId;   @MapsId   @OneToOne User user; } @Entity  class User {   @EmbeddedId UserId id;   Integer age; } @Embeddable class UserId implements Serializable {   String firstName;   String lastName; } Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 9

Generator Partial generator


Generator Partial generator @Entity public class CustomerInventory implements Serializable {   @Id   @o.h.a.GenericGenerator(name = "inventory", strategy = "uuid2")   @GeneratedValue(generator = "inventory")   String uuid; @Id String location;   @Id @ManyToOne(cascade = CascadeType.MERGE)   Customer customer; } @Entity public class Customer implements Serializable {    @Id    private int id; } Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 10

Runtime - fetch profile


Runtime - fetch profile • Define more than one fetching profile – classic one @OneToMany(fetch=EAGER) –Some other with specific names • Definition centralized Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 11

@Entity @FetchProfile(name = "all",  fetchOverrides = { @FetchProfile.FetchOverride( entity = Customer.class,  association = "orders",  mode = FetchMode.JOIN) @FetchProfile.FetchOverride( entity = Order.class,  association = "country",  mode = FetchMode.


@Entity @FetchProfile(name = "all",  fetchOverrides = { @FetchProfile.FetchOverride( entity = Customer.class,  association = "orders",  mode = FetchMode.JOIN) @FetchProfile.FetchOverride( entity = Order.class,  association = "country",  mode = FetchMode.JOIN) }) public class Customer {    @Id @GeneratedValue private long id; private String name;    private long customerNumber;    @OneToMany private Set orders;    // standard getter/setter } Session session = ...; session.enableFetchProfile( "all" );  // name matches @FetchProfile name Customer customer = (Customer) session.get( Customer.class, customerId ); session.disableFetchProfile( "all" ); // or just close the session Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 12

ORM


ORM Mappings Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011

Criteria API


Criteria API • Object Oriented query building API • Type-safe • Strongly typed • Shines for complex input driven queries • optionally use a static metamodel –annotation processor Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 14

Demo


Demo Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011

Static metamodel


Static metamodel @Entity public class Item { @Id @GeneratedValue public Long getId() {} public Boolean isShipped() {} public String getName() {} public BigDecimal getPrice() {} @OneToMany public Map getPhotos() {} @ManyToOne public Order getOrder() {} @ManyToOne public Product getProduct() {} } @StaticMetamodel(Item.class) public class Item_ { public static SingularAttribute id; public static SingularAttribute shipped; public static SingularAttribute name; public static SingularAttribute price; public static MapAttribute photos; public static SingularAttribute order; public static SingularAttribute product; } Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 16

How to build a query


How to build a query • From EntityManager • CriteriaBuilder is a helper class • CriteriaQuery represents your query • Type safe EntityManager em; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery critQ = qb.createQuery(Order.class); [...] TypedQuery q = em.createQuery(critQ); List orders = q.getResultList(); Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 17

Joins and Where


Joins and Where • CriteriaBuilder is a function placeholder SELECT c.name FROM Customer c JOIN c.orders o JOIN o.items i WHERE i.product.price > 200 Root c = cq.from(Customer.class); Path i = c.join(Customer_.orders).join(Order_.items); cq.select( c.get(Customer_.name) ) .where( cb.greaterThan( i.get(Item_.product).get(Product_.price) ), 200 ) ); Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 18

Map and like


Map and like SELECT p FROM PictureCategory c JOIN c.photos p WHERE c.name = 'birds' AND KEY(p) LIKE '%egret%' Root picCat = cq.from(PictureCategory.class); MapJoin photos = picCat.join(PictureCategory_.photos); cq.select(photos) //.select(photos.value()) .where( cb.and( cb.equal( picCat.get(PictureCategory_.name),"birds"), cb.like( photos.key(), "%egret%" ) ); Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 20

Support all of JP-QL


Support all of JP-QL • Collections and maps • Aggregation, order by, group by • Having, count • Subselect Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 21

ORM


ORM Mappings Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011

Lock Mode


Lock Mode • Prevents –dirty reads –non-repeatable reads • Types –Optimistic –Pessimistic Read / Write • Force increment • Make use of Database locks in a standard way Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 23

2nd level cache and Infinispan


2nd level cache and Infinispan • New caching contact –finer grained (per entity type / collection type / query) –more fine tuning (eviction, consistency level) • Infinispan –lighter than JBoss Cache –Better perf for both local and distributed cache –config embedded in persistence.xml / hibernate.cfg.xml Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 24

Proxy lib


Proxy lib • Deprecating CGLIB • Use Javassist Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 25

Packaging


Packaging • Core is made of several modules including –annotations –entitymanager –envers –... core :) • Easier for your to chose what you want –org.hibernate:hibernate-core:3.6.1.Final –org.hibernate:hibernate-entitymanager:3.6.1.Final • Doc on the way –core and annotations docs are now merged Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 26

Future


Future • Hibernate 4 • clean API/SPI/Impl • i18n logs and exception messages • rethinking of configuration –ServiceRegistry –pass instances around • multi-tenancy • on-demand loading • spatial as a module Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 27