• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate 3.1

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

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

---
Query
----
SQLQuery q = this.sessionFactory.getCurrentSession().createSQLQuery(
"select {r.*}, b.rate from TableA r, TableB b " +
"where b.id=r.testId"
);


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.

Shubhra
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Travis,

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.

Regards,
Vijay
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic