This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Database errors and Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Database errors and Hibernate" Watch "Database errors and Hibernate" New topic
Author

Database errors and Hibernate

Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
In a solution using Hibernate, will DB level errors get passed back to the Java class initiating the action? For example if a class attempts to write a duplicate record to a uniquely keyed table how will the resulting error be manifested?
[ September 02, 2004: Message edited by: Ray Marsh ]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
They are wrapped in fact in HibernateException. You can take a look to see the hierachy beyond HibernateException. JDBCException will wrap original SQLException. More you can extract from API.

./pope


blog - InfoQ.com
Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
Thank you.

I've done some Java coding that uses the java.sql.* classes. However, I have not used Hibernate. How detached is the coding process from the actual database itself? Having come from an environment that interacts directly with the DB, (iSeries, OS/400) using the actual table and column names, I'm trying to get an idea how this type of methodology looks and feels.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi

hibernate offers HQL (Hibernate Query Language) as a an object oriented replacement for SQL. Your main point of contact will be the hibernate Session. It allows to issue queries, load/store and remove Object.
Instead to query for columns in tables you query for Objects and their properties (e.g "from Cat" selects all instances of Cat class).
If you have an Object oriented model, then you can work with hibernate, have it store/load/query the database.

helpful ?

cheers

pascal
Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
Thank you for the insight.

Are the "properties" of an object the columns in a given row(s) in the table? How are multiple rows of a table represented as objects in a java class? Is it represented as an array?

I know databases, but I am trying to understand how data is viewed from an object oriented perspective.

Thanks again.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Ray Marsh:
Thank you.

I've done some Java coding that uses the java.sql.* classes. However, I have not used Hibernate. How detached is the coding process from the actual database itself? Having come from an environment that interacts directly with the DB, (iSeries, OS/400) using the actual table and column names, I'm trying to get an idea how this type of methodology looks and feels.


It is highly detached as you rest inside the objectual world (for most of the time). The link to relational world are the mapping, and so your code is freed.

./pope
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Ray Marsh:
Are the "properties" of an object the columns in a given row(s) in the table? How are multiple rows of a table represented as objects in a java class? Is it represented as an array?

Yes, for the basic cases, tables map to classes and columns map to properties/fields of classes. When you query for multiple rows, you get multiple instances of the class for that table.

Hibernate has support for multiple methods of handling inheritence, and it is quite flexible in its mapping of relations. For example, if you have a User with many addresses, one of each type (home, work, etc), you can set it up to create a Set of Addresses or a Map of [type -> Address].

The main thing an ORM tool provides is abstraction. You do things like and the tool figures out what SQL to execute in response. As well, it manages units of work in order to handle transactions and improve performance by delaying and aggregating SQL operations to the end.
Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
So it looks like the lion's share of the work is in creating the object or entity definition. Once it is set up, assuming it was set up properly, the coding is relatively easy.

Is that the idea?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Ray Marsh:
So it looks like the lion's share of the work is in creating the object or entity definition. Once it is set up, assuming it was set up properly, the coding is relatively easy.

Precisely. The first step is to create configuration files that map your business objects to the underlying data model. There are plenty of tools to ease this step as well (XDoclet, Middlegen, etc). In fact, there are tools to create your business objects and mapping files from your schema and vice versa.

Then you code your business logic in standard OO fashin. Again there are open source tools (e.g. Spring Framework) to help with transaction demarcation, caching, etc. The big benefit is that you can modify many business objects in a single transaction, and Hibernate will generate the necessary -- and optimized -- JDBC calls to do the work in the database.
Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
So Hibernate provides a "translation" layer between the database and the application, but requires additional utilities to create configuration files and object modeling. Is that an accurate picture?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Ray Marsh:
So Hibernate provides a "translation" layer between the database and the application, but requires additional utilities to create configuration files and object modeling. Is that an accurate picture?


So Hibernate is an ORM which tries to solve the so-called object/relational paradigm mismatch, and for this Hibernate have chosen to use an external mapping mechanism using XML descriptions.

./pope

PS: for object/relational paradigm mismatch try a little googling ;-).
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Ray Marsh:
So Hibernate ... requires additional utilities to create configuration files and object modeling.

Only if you consider a text editor to be an "additional utility." The mapping files are straight XML. The object model classes are just that -- Java classes. While both can be created by hand, there are many tools that will ease this process even further.
Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
Where does the database design and creation fit in? Is it outside the items we've been discussing? However, it appears necessary to have a DB with a proper design to allow for OO methodology to apply.

Or to re-phrase, does all the previous assume a properly designed DB?

Thanks,
Gavin King
author
Ranch Hand

Joined: Aug 31, 2004
Posts: 76
Data modelling is outside the scope of ORM. However, Hibernate can be used to export DDL given existing mapping files. The generated schemas are very useable, as long as you add sufficient information about required constraints and indexes.


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/193239415X/ref=jranch-20" target="_blank" rel="nofollow">Hibernate in Action</a>
Bacon
Ranch Hand

Joined: Jul 14, 2004
Posts: 305
Thanks for the feedback.

I'm trying to establish a mental picture of the development cycle for this methodology and understand how Hibernate and other tools fit in. I believe I have a pretty good idea now. Thanks again for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database errors and Hibernate
 
Similar Threads
Struts REquest Processor throws Null Pointer exception
why the annotaion way
hibernate validator question
Compilation Error while using Hibernate Annotations
HI I got the null pointer exception using session factory