--- 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
~-<br />Doing my best this time and everytime.<br />-- Me
Travis Hein
Ranch Hand
Joined: Jun 06, 2006
Posts: 161
posted
0
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
posted
0
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. -->