1 / 20

ORM with JPA/Hibernate overview

ORM with JPA/Hibernate overview. Petar Milev. Contents. ORM Concepts Java class to database table Java object to database table’s row Classes vs. tables (OR) Mapping with Hibernate Two ways to go Annotations vs. XML Create, Retrieve, Update, Delete and JPA QL. ORM Concepts.

Download Presentation

ORM with JPA/Hibernate overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ORM with JPA/Hibernate overview Petar Milev

  2. Contents • ORM Concepts • Java class to database table • Java object to database table’s row • Classes vs. tables • (OR) Mapping with Hibernate • Two ways to go • Annotations vs. XML • Create, Retrieve, Update, Delete and JPA QL

  3. ORM Concepts

  4. Class To Table • Associate Java POJO with database table Table: PERSON (SQL may be generated by Hibernate) • You can do it without even using SQL class Person{ Long id; String name; String description; //getter, setters}

  5. Instances Are Rows • Java instances are represented as rows in the table void mymethod(Person p){ System.out.println(p.getId()); //1 System.out.println(p.getName()); //’Pesho’ System.out.println(p.getDescription());//’Java man’ }

  6. Classes vs. Tables • Classes inherit from each other • Classes have associations between them • What about tables? • The differences lead to the need for meta information

  7. (OR) Mappings

  8. Ways To Go - XML • XML files for every mapped class <class name=“www.com.Person" table=“PERSON">   <id column=“ID" name="id" type="java.lang.Long“/> <property column=“NAME" name=“name" type="java.lang.String"/><property column=“DESCRIPTION" name=“description" type="java.lang.String"/> <set name=“hairs"> <key column=“PERSON_ID" not-null="true"/><one-to-many class=“www.com.Hair"/> </set></class>

  9. Ways To Go – Java Annotations • Java Annotations used at the mapped classes @Entity(“PERSON”) public class Person { private Long id; private String name; private String description; private Set<Hair> hairs; //..code continued

  10. Java Annotations 2 • Java Annotations used at the mapped classes //... @Id public Long getId() { return id; } @Column(name=“NAME”) public String getName() { return name; } @Column(name=“DESCRIPTION”) public String getDescription(){ return description; } @OneToMany(mappedBy=“person”) public Set<Hair> getHairs(){ return hairs; } //normal setters }

  11. Annotations vs. XML • Annotations advantages • Everything is in one place • Annotations are refactoring-friendly for renaming and moving of classes • Annotations are controlled by the compiler • Annotations shortcomings • Cluttered source code

  12. XML vs. Annotations • XML advantages • Java code is more readable • XML shortcomings • More things to write • Support simultaneously two files: .xml and .java

  13. More about mappings in JPA • Configuration by exception • You can tell JPA how to fetch the relations • Transient properties • Map relations • Map maps • Embedded objects • Compound primary keys

  14. Create, Read, Update, Delete

  15. (CR)UD • Create • Read void create(EntityManager entityManager){ Person person = new Person(“pesho”, “java programmer”); entityManager.persist(person); } void read(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); } • Query void query(EntityManager entityManager){ Query query = entityManager.createQuery(“from Person where name like ‘pesho’ ”); List persons = query.getResultList();}

  16. CR(UD) • Update void update(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); person.setDescription(“new description”); …. entityManager.getTransaction().commit(); } • Delete void delete(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); entityManager.remove(person); }

  17. Query Language Examples • Polymorphic queries from Shape; //where Shape is an abstract class from java.lang.Object; //get the whole database, why not? • Where, like, in, functions from Person p where p.name like ‘P%’ and p.description like ‘J%’ or p.name in (‘John’, ‘Ben’, ‘Petar’) or lower(p.name) = ‘pesho’

  18. More Query Examples • Sub selects with ‘any’, ‘some’, ‘all’ and ‘in’ from Box b where 10 <= any ( select b.amount from i.bets b ) from Box b where 10 > all ( select b.amount from i.bets b ) from Box b where 10 = some ( select b.amount from i.bets b ) from Box b where 10 in ( select b.amount from i.bets b ) • Projection select a, b from Animal a, Banica b; //returns List of Object[], the array is with 2 elements

  19. More Query Examples • Implicit joins from Bet bet where bet.item.category.name like ‘hundred%' and bet.item.firstBet.quantity > 10 • SQL alternative select . . . . from BET B inner join ITEM I on B.ITEM_ID = I.ITEM_ID inner join CATEGORY C on I.CATEGORY_ID = C.CATEGORY_ID inner join BET SB on I.FIRST_BET_ID = SB.BET_ID where C.NAME like ‘hundred%' and SB.QUANTITY > 100

  20. More Query Examples • Normal joins from Person p left join p.hairs h with h.length > 10 where p.description like '%Foo%' • Dynamic instantiation select new BetInfo( bet.item.id, count(bet), avg(bet.amount) ) from Bet bet where bet.current.ammount is null group by bet.current.id

More Related