Parleys.com Search Index

Home | Only JUGs | BeJUG
Title: Hibernate University (Part 1/2)

Description:
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.

Speaker(s): Emmanuel Bernard

Keyword(s): hibernate

Slide Content:
0) 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
1) 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
2) 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
3) Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 4
4) ORM a JPA 2 perspective Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011
5) 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
6) 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
7) 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
8) 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
9) @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
10) ORM Mappings Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011
11) 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
12) Demo Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011
13) 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
14) 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
15) 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
16) 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
17) 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
18) ORM Mappings Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011
19) 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
20) 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
21) Proxy lib • Deprecating CGLIB • Use Javassist Copyright 2007-2011 Emmanuel Bernard and Red Hat Inc. vendredi 25 février 2011 25
22) 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
23) 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
(c) Parleys.com NV, 2006-2011 | Blog | Wiki | Follow Us