Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hibernate 3.1  RSS feed

Shubh Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!