File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate 3.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate 3.1" Watch "Hibernate 3.1" New topic

Hibernate 3.1

Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
Hi all,
I am a Hibernate newbie. Had a simple task.

I am using the following mapping query and class:

<class name="com.TableA" table="TableA">
<id name="id" column="id" type="string"></id>
<property name="fun"/>
<property name="foo"/>

SQLQuery q = this.sessionFactory.getCurrentSession().createSQLQuery(
"select {r.*}, b.rate from TableA r, TableB b " +

q.addEntity("r", TableA.class);
q.addJoin("b", "r.rate");
TableA class
Has all the setter and getter for, id, foo, fun AND ALSO rate

My task is:

1. when I fetch data, I do a join on two tables to get all the fields, lets say TableA & TableB.

2. when I update and save I just want to TableA to be updated.

From the above I get and error that rate is not mapped.
How do I put rate in the mapping and when I do an update I don't really affect TableB.


~-<br />Doing my best this time and everytime.<br />-- Me
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
The hibernate mapping file:

your beans:

then your query:

then when you do a q.list() you will get instances of TableA beans, that contain a nested (joined) TableB, that you can access like

and later when you save

It will save just the tableA, but not the nested TableB

Note also that i used named queries as opposed to building the query string in the code. Unless there is a good reason, like a complete free form dynamic query, I have found it better to use named queries as they are validated when hibernate boots up, as opposed to when they are run, which saves a lot of sanity when you are investigating a mature app and someone changed the table name but missed the one screen that was doing the query in the code using the old column name.

The other thing i did differntly was use the hibernate query as opposed to the sql query. Again unless there is a good reason to use sql queries, like adhoc join of different tables, it is better to use hibernate mappings and hibernate queries.
[ July 03, 2006: Message edited by: Travis Hein ]

Error: Keyboard not attached. Press F1 to continue.
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74

Sorry for jumping in.

I could not understand your remarks.

<!-- read only bean mapped for TableB
Since we have the column id above as a column, we must specify the insert=false & update=false here.
Alternatively, we could move them to the testId property, which makes the testId property read only and then the TableB is always saved when TableA is saved, but you didn't want that.

Can you explain bit further.

I agree. Here's the link:
subject: Hibernate 3.1
jQuery in Action, 3rd edition