In Lacture CSS441 Advanced Programming using JAVA EE

  • Slides: 24
Download presentation
In Lacture CSS-441 Advanced Programming using JAVA EE Topic : Hibernate 1 Kaster Nurmukan

In Lacture CSS-441 Advanced Programming using JAVA EE Topic : Hibernate 1 Kaster Nurmukan

Agenda • • • An ORM tool The problem fixed by ORM Advantage Hibernate

Agenda • • • An ORM tool The problem fixed by ORM Advantage Hibernate Basic – – Hibernate session. Factory Hibernate Session CRUD operation with Hibernate Take care with Transaction • Other ORM JAVA framework ?

The problem before then Hibernate id Name Age birthday Id Name Age Brithday Object

The problem before then Hibernate id Name Age birthday Id Name Age Brithday Object DB table

Hibernate vs. JDBC (an example) • JDBC insertion – st. execute. Update(“INSERT INTO student

Hibernate vs. JDBC (an example) • JDBC insertion – st. execute. Update(“INSERT INTO student VALUES(“Hans N”, ” 1990 -12 -11”)); • Hibernate insertion – session. save(student);

The problem ü ü ü Mapping object variables to column Mapping relationship Inheritance :

The problem ü ü ü Mapping object variables to column Mapping relationship Inheritance : java have , RDBMS no. Associations : in java Reference ; in RBDMS foreign key Handling data Type Managing changes to object state object Relatinal Mapping

ORM What is ORM stands for Object-Relational Mapping (ORM) is a programming technique for

ORM What is ORM stands for Object-Relational Mapping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C# etc Advantages ØLets business code access objects rather than DB tables. ØHides details of SQL queries from OO logic ØNo need deal with database implementation ØEntities based on business concept s rather then database structure ØTransaction management and automatic key generation ØFast development of application

Hibernate • Hibernate is an Object-Relational Mapping(ORM) solution for JAVA and it raised as

Hibernate • Hibernate is an Object-Relational Mapping(ORM) solution for JAVA and it raised as an open source persistent framework created by Gavin King in 2001. It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application • NHibernate for. Net. opensource

Hibernate Features • O-R mapping using ordinary Java. Beans • Can set attributes using

Hibernate Features • O-R mapping using ordinary Java. Beans • Can set attributes using private fields or private setter methods • Lazy instantiation of collections (configurable) • Polymorphic queries, object-oriented query language • Cascading persist & retrieve for associations, including collections and many-to-many • Transaction management with rollback • Can integrate with other container-provided services

Learn Hibernate what you need Study path , If JDBC way : Ø SQL

Learn Hibernate what you need Study path , If JDBC way : Ø SQL Fundamentals Ø JDBC Fundamentals Ø Design and Code • IF use a framework Ø How to use Hibernate Ø Configure a Database • IF use a stardard Ø How to use JPA Ø Configure a Database

Application Architecture User Interface Data object UI event Application Logic data request business Objects

Application Architecture User Interface Data object UI event Application Logic data request business Objects Session. Factory hibernate. cfg. xml *. hbm. xml class mappings Foundation Classes business object DAO Hibernate API business object Hibernate JDBC API JDBC Result. Set, etc.

hibernate. cfg. xml for My. SQL <? xml version='1. 0' encoding='utf-8'? > <!DOCTYPE hibernate-configuration

hibernate. cfg. xml for My. SQL <? xml version='1. 0' encoding='utf-8'? > <!DOCTYPE hibernate-configuration PUBLIC. . . remainder omitted > <hibernate-configuration> <session-factory> <property name="dialect"> org. hibernate. dialect. My. SQLDialect </property> <property name="connection. driver_class"> com. mysql. jdbc. Driver </property> <property name="connection. username">student</property> <property name="connection. password">pw</property> <property name="connection. url"> jdbc: mysql: //localhost: 3306/dbtest</property> <!-- Object-Relational mappings for classes --> <mapping resource="eventmgr/domain/Location. hbm. xml"/>. . . other mappings omitted </session-factory> </hibernate-configuration>

Hibernate in Application Archinecture Source: Hibernate Reference Manual (online)

Hibernate in Application Archinecture Source: Hibernate Reference Manual (online)

Hibernate Basics Session A single-threaded, short-lived object representing a conversation between the application and

Hibernate Basics Session A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC connection. Factory for Transaction. Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.

Hibernate Basics Persistent Objects and Collections Short-lived, single threaded objects containing persistent state and

Hibernate Basics Persistent Objects and Collections Short-lived, single threaded objects containing persistent state and business function. These might be ordinary Java. Beans/POJOs, the only special thing about them is that they are currently associated with (exactly one) Session. As soon as the Session is closed, they will be detached and free to use in any application layer (e. g. directly as data transfer objects to and from presentation).

Hibernate Basics Transient Objects and Collections Instances of persistent classes that are not currently

Hibernate Basics Transient Objects and Collections Instances of persistent classes that are not currently associated with a Session. They may have been instantiated by the application and not (yet) persisted or they may have been instantiated by a closed Session.

Hibernate Basics Transaction (Optional) A single-threaded, shortlived object used by the application to specify

Hibernate Basics Transaction (Optional) A single-threaded, shortlived object used by the application to specify atomic units of work. Abstracts application from underlying JDBC, JTA or CORBA transaction. Multiple transactions per Session.

Hibernate Basics Connection. Provider (Optional) A factory for (and pool of) JDBC connections. Abstracts

Hibernate Basics Connection. Provider (Optional) A factory for (and pool of) JDBC connections. Abstracts application from underlying Datasource or Driver. Manager. Not exposed to application, but can be extended/implemented by the developer. Transaction. Factory (Optional) A factory for Transaction instances. Not exposed to the application, but can be extended/implemented by the developer.

Session. Factry • • try { configuration. configure(config. File); session. Factory = configuration. build.

Session. Factry • • try { configuration. configure(config. File); session. Factory = configuration. build. Session. Factory(); } catch (Exception e) { System. err. println("%%%% Error Creating Session. Factory %%%%"); e. print. Stack. Trace(); }

Session • public Session get. Session() { • return Hibernate. Session. Factory. get. Session();

Session • public Session get. Session() { • return Hibernate. Session. Factory. get. Session(); • }

Save to db with hibernate • • • public void save(Answer answer) { log.

Save to db with hibernate • • • public void save(Answer answer) { log. debug("saving Answer instance"); try { get. Session(). save(answer); log. debug("save successful"); } catch (Runtime. Exception re) { log. error("save failed", re); throw re; } }

Take care with Transaction • • • private User. DAO u. Dao = new

Take care with Transaction • • • private User. DAO u. Dao = new User. DAO(); ……. . try { Transaction trans= u. Dao. get. Session(). begin. Transaction(); trans. begin(); u. Dao. save(user); trans. commit(); } catch (Runtime. Exception e) { throw e; }

Java ORM Frameworks Ø Ø Ø Ø Enterprise Java. Beans Entity Beans Java Data

Java ORM Frameworks Ø Ø Ø Ø Enterprise Java. Beans Entity Beans Java Data Object Castor Top. Link Spring DAO Hibernate More

Reference • http: //www. hibernate. org/hib_docs/reference/en/html_single/ • http: //www. hibernate. org/78. html • http:

Reference • http: //www. hibernate. org/hib_docs/reference/en/html_single/ • http: //www. hibernate. org/78. html • http: //www. oracle. com

Q&A

Q&A