www.jboss.com. Hibernate is a high-performance object/relational persis- tence
and query service. The most flexible and powerful object/relational solution on ...
Hibernate's Main Classes. ▫ Mapping entities' relationships (i.e. entities to objects
). ▫ Mapping OO relationships (i.e. classes to entities). – Mapping associations.
2 Nov 2007 ... Object/Relational Mapping with Hibernate. 2 .... How to retrieve data from
associations in an ..... Hibernate doesn't manage associations for you;.
Développement Web - Object Relational Mapping and Hibernate. Plan. ORM et
Hibernate. Objectifs. • se familiariser avec l'ORM. • le mettre en oeuvre sans ...
can take advantage of a number of available tools. The remainder of this ... methodology sup- ports the generation of relational databases by a methodological and automated ..... OES/SEO 2001 Rome Workshop, Luiss Publica- tions, Missikoff ...
In Hibernate, a value type may define associations; it's possible to navigate from
a value type ... For example, a many-to-many association mapping hides the.
Aug 9, 2012 ... Psuedo-syntax for UPDATE and DELETE statements using HQL ...... book.
setAuthor( entityManager.getReference( Author.class, authorId ) );.
The Inter-Agency Mapping Pla orm (IAMP) collects basic informa on of all informal se le- ments on a bi-monthly basis. Th
Hibernate supports the three basic inheritance mapping strategies: table per ...
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/inheritance.html. 1 von
4.
Object Role Modeling (ORM). ORM is a conceptual modeling language used in ontology engineering [13]. It encompasses a group of constraints that can ...
Abstract. We map ORM into the SHOIN/OWL, which is the most common description logic in ontology engineering. As SHOIN/OWL is known to be.
The Inter-Agency Mapping Pla orm (IAMP) collects basic informa on of all informal se le- ments on a bi-monthly basis. Th
Faculty of Information Technology ... requirements for the Master's Degree in Computing From ... This Master Thesis was successfully defended in 11/02/2012.
DB. $ symfony propel-build-schema xml. MODEL CLASSES. /lib/model/om. /lib/
model. /lib/model/map. Object/Relational. Mapping (ORM) use u se. SYMFONY ...
Those accountable for the success or failure ... successful outcome or to produce
a result. • The 5M Model looks at ..... To complete the Intermediate ORM Test,.
pendent on business intelligence and rule-based software to perform predictive ... support such a model-driven engineering approach to business analytics.
26 Sep 2005 ... difference in relationship management between entities: - in OO: links ....
Hibernate maps domain model and database schema with XML map-.
2 Problem. In a company I work, for some reason, we have used AppFuse as a base for ... tect with new ideas of new web-development approach, he told me: âWell, ... lowing DB centric approach, but you know that this is not the best way out,.
10. Basic ORM. 4. Make control decisions: Who's the right person? Whoever has the best grasp of the risk and the opportu
Shopping and Car-Rental) selects appropriate modules (from a library) and composes them through a composition operator. The result of the composition is ...
t ). • Use a Object-Relational Mapping System (eg. Hibernate). • Provides a
simple API for storing and retrieving Java objects directly to and from the
database.
Object-Relational Mapping (ORM) and
Hibernate
Problem area • When working with object-oriented systems, there’s a mismatch between the object model and the relational database • How do we map one to the other? public class Student { private String name; private String address; private Set courses; private Set degrees; }
Java object with properties and associations
Relational database with tables and columns
Problem area • How to map associations between objects? – References are directional, foreign keys not – Foreign keys can’t can t represent many many-to-many to many associations
Student
STUDENT student_id name address degree id degree_id
N N
Degree
DEGREE degree_id type name
public class Student { private Collection degrees; ...
Relational database / SQL
Java
Technology • Why Wh relational l ti ld databases? t b ? – Flexible and robust approach to data g management – De-facto standard in software development
• Why object-oriented models?
((Domain model))
Student
Course
Degree
– Business logic can be implemented in Java (opposed to stored procedures) – Allows for use of design patterns and concepts like polymorphism – Improves I code d reuse and d maintainability i t i bilit
• Demand for mapping interaction! (Database)
Approaches to ORM • Write SQL conversion methods by hand using JDBC – – – – –
Tedious and requires lots of code Extremely error error-prone prone Non-standard SQL ties the application to specific databases Vulnerable to changes g in the object j model Difficult to represent associations between objects
public void addStudent( Student student ) { String sql = ”INSERT INTO student ( name, address ) VALUES ( ’” + student.getName() + ”’, ’” + student.getAddress() + ”’ )”; // Initiate a Connection, create a Statement, and execute the query }
Student
Degree
Course
Approaches to ORM • Use Java serialization – write application state to a file – Can only be accessed as a whole – Not possible to access single objects
• Object oriented database systems – No complete query language implementation exists – Lacks necessary features
The preferred solution • U Use a Object-Relational Obj t R l ti lM Mapping i S System t ( (eg. Hib Hibernate) t ) • Provides a simple API for storing and retrieving Java objects directly to and from the database • Non-intrusive: No need to follow specific rules or design p patterns • Transparent: Your object model is unaware
ORM and Architecture • Middleware that manages persistence • Provides P id an abstraction b t ti layer between the domain model and the database
Presentation Layer
Service/Business Layer
Domain Model
Persistence Layer ORM / Hibernate
(Database)
Example app: The EventManager
Java objects
Hibernate API
Hibernate mapping files
Hibernate configuration file
Java objects Identifier property
public class Event { private int id; private String title; private Date date; private Set persons = new HashSet();
No-argument constructor
public Event() { } public int getId() { return id; }
Follows the JavaBean naming conventions
private void setId( int id ) { this.id = id; } public String getTitle() { return title; } public void setTitle( String title ) { this.title = title; } // Getter and setter for date and persons }
Example app: The EventManager
Java objects
Hibernate API
Hibernate mapping files
Hibernate configuration file
Hibernate mapping files • Tells Hibernate which tables and columns to use to load and store objects DTD
Cl Class element l t
Identifier mapping & generation
Property mapping
Unidirectional many-to-many association mapping
Filename: Event.hbm.xml
events >
Example app: The EventManager
Java objects
Hibernate API
Hibernate mapping files
Hibernate configuration file
The Configuration class • Represents a set of mapping files • Mapping M i fil files can b be specified ifi d programmatically or through the Hibernate configuration file • Intended as a startup-time object objec
Configuration configuration = new Configuration(); configuration.configure();
Loads Hibernate.cfg.xml from the classpath
The SessionFactory interface • Obtained from a Configuration instance • Shared Sh d among application li ti threads • Main purpose is to provide Session instances • Allowed to instantiate more than one SessionFactory • Sophisticated implementation of the factory design pattern
The Session interface • Obtained from a SessionFactory instance Session session = sessionFactory.openSession(); • Main M i runtime ti interface i t f b between t a Java application and Hibernate • Responsible for storing and retrieving objects • Think of it as a collection of loaded objects related to a single unit of work
Instance states • An object instance state is related to the persistence context • The Th persistence i t context t t = a Hibernate Hib t S Session i instance i t • Three types of instance states: – Transient • The instance is not associated with any persistence context
– Persistent • The instance is associated with a persistence context
– Detached • Th The instance i t was associated i t d with ith a persistence i t context t t which hi h h has been closed – currently not associated
The Session interface Make a transient object persistent
Event event = new Event( ”title”, new Date() ); I t Integer id = (Integer) (I t ) session.save( i ( eventt ); )
Load an object – if matching row exists
Event event = (Event) session session.load( load( Event Event.class, class id );
Load an object – if unsure about matching row
Event event = (Event) session.get( Event.class, id );
Delete an object – make it transient again
session.delete( event );
The Session interface Update an object – if its detached
Update or save an object – if you’re unsure about the state
session.flush(); // Happens auto. at transaction.commit()
The Criteria interface • You need a query when you don’t know the identifiers of the objects you are looking for • Criteria C it i used d ffor programmatic ti query creation ti Criteria criteria = session session.createCriteria( createCriteria( Event.class Event class );
Retrieve all instances of Event List events = criteria.list();
Transactions • Transaction: A set of database operations which must be executed in entirety or not at all • Should Sh ld end d either ith with ith a commit it or a rollback llb k • All communication with a database has to occur inside a transaction! Transaction begins
Advantages of ORM • Productivity – Eliminates lots of repetitive code – focus on business logic – Database schema is generated automatically
• Maintainability – Fewer lines of code – easier to understand – Easier to manage change in the object model
Advantages of ORM • Performance – Lazy loading – associations are fetched when needed – Caching
• Database vendor independence – The underlying database is abstracted away – Can be configured outside the application
Resources • Books on Hibernate – Christian Bauer and Gavin King: Hibernate in Action – James Elliot: Hibernate – A Developer Developer’s s notebook – Justin Gehtland, Bruce A. Tate: Better, Faster, Lighter Java
• The Hibernate reference documentation – www.hibernate.org