-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Can i use createSQLQuery to execute native sql updates as we
PostPosted: Sun Dec 10, 2006 7:30 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
Can i use createSQLQuery to execute native sql updates as well... and if i have two or more table i just need to map them and create there Java Bean and then call them in my dao methods with joins in native sql...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 1:56 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Yes, no problem, just think about flushing before you do it, and clearing your session after you do it. (session.flush(), then session.clear()).

This will ensure Hibernate has to retrieve data from db, maybe that data you updated with native SQL.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 6:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
batmat is correct but instead of doing that flushing manually you should use <synchronize> in named queries to specify which tables needs flushing. In H3.2 svn you also have an .addSynchronize(string) method to do the same in the API.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 6:22 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
max wrote:
batmat is correct but instead of doing that flushing manually you should use <synchronize> in named queries to specify which tables needs flushing. In H3.2 svn you also have an .addSynchronize(string) method to do the same in the API.


Very interesting. I'm looking for this method in the svn http://anonsvn.jboss.org/repos/hibernat ... hibernate/, but can't find it. Where will be located this addSynchronize() method in the API? I looked at Criteria and Session, but does not seem to be there. Thanks a lot.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 7:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Better to look at the related SQLQuery class ;)

http://fisheye.labs.jboss.com/browse/Hi ... =10846#l63

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 11:54 am 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
so I can have something like

sess.createSQLQuery("UPDATE PERSON SET NAME=UPPER(?) WHERE ID=?")


"is the above correct"

I want to be able to update a list of person name from a jsp page how does hibernate handle updating a list of in names in the above case...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes you can...just use appropriate .addSynchronizeXXX(,,,).executeUpdate()

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: actually I am using using hibernate 3.0
PostPosted: Mon Dec 11, 2006 11:29 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
actually I am using using hibernate 3.0 does is that method available in hibernate 3.0


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 3:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no. it's recently added in Hibernate 3.2 svn

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 8:36 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
max wrote:
you should use <synchronize> in named queries to specify which tables needs flushing.

No. It's recently added in Hibernate 3.2 svn


Is the <synchronize> block present since the 3.0 ? If yes, just try and use it.

BTW, max, is there somewhere to find a doc for this feature ? I'm trying to test it with 3.2.1.ga, but can't make it work. I mean : I call an sql-query which has a <synchronize> tag. This sql-query is an update one.

<sql-query name="updateNombreHabitantsVille">
<synchronize table="VILLE" />
UPDATE VILLE SET NBHABITANT=NBHABITANT+:ajoutHabitants
</sql-query>

After calling executeUpdate, what do I have to do with the associated session, just "re-"get the persistent object ? Refresh it ? (though in this case I wouln't see the advantage of this tag anymore).

Obviously, if I manage to make it work, I will explain it here, so as suzie can use it :-).

suzie, can't you update to 3.2.1.ga ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 8:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
its documented in the reference docs.

there might be a casing issue; meaning if your entities is mapped to table="ville" then you should use that instead of table="VILLE"

You don't have do anything; the synchronize is to tell hibernate to flush any pending changes related to the table ville before doing the actual update.

That is what it does.

You are talking about getting the changed objects back again and here you need refresh or a fresh session (as documented for all our update/batch queries since hibernate cannot know which objects in memory you actually changed in the db)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 9:31 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, in the reference doc, I only found <synchronize> used in <class> not in queries. I guess it would be useful to remind that it's possible to use it inside named queries too.

Quote:
there might be a casing issue; meaning if your entities is mapped to table="ville" then you should use that instead of table="VILLE"

You were right. I didn't specify the table name. So the class was Ville, I put Ville to in sychronize => the flush is ok.

Quote:
You are talking about getting the changed objects back again and here you need refresh or a fresh session (as documented for all our update/batch queries since hibernate cannot know which objects in memory you actually changed in the db)

Well, yes, I think I knew about. But as I didn't know the <synchronize> tag before today, I thought it would act something like this :
* Flush the session on the objects corresponding to the specified table
* Call the query
* refresh() every objects corresponding to the specified table

I haven't thought about all the implications of such a behaviour, though I'm for example aware that the complexity of refreshing all objects would become bigger and bigger as the cache grows.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 12:42 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
I have the following sql and want to make sure that the mapping required for this query are correct:

select trans.date, con.type, ai.item
from Transaction tran, Condition con, AssignedItem ai
where tran.id = ai.transaction.id
and con.id = ai.condition.id
and tran.department=:deptNum

plus do i need the many-to-one mapping for the above query also in the Transaction and Condition table as well
or just plain mappings for those

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="AssignedItem" table="AssignedItem">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="item" column="item"/>

<property name="ai_trans_id" column="trans_id"/>
<property name="ai_con_id" column="con_id"/>

<property name="title"/>

<many-to-one column="ai_con_id" class="transactions"/>

<many-to-one column="ai_con_id" class="condition"/>

</class>


</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 5:46 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
suzie wrote:
I have the following sql and want to make sure that the mapping required for this query are correct:

select trans.date, con.type, ai.item
from Transaction tran, Condition con, AssignedItem ai
where tran.id = ai.transaction.id
and con.id = ai.condition.id
and tran.department=:deptNum

plus do i need the many-to-one mapping for the above query also in the Transaction and Condition table as well
or just plain mappings for those


Well, sql does not use the mappings, so it could even be bad, your sql query would work anyway.

I'd advice you to use a named sql-query with the <synchronize> tag that max spoke about.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 7:30 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
i think if I make a name sql then I won't be able to use the getter and setters of the three tables I am trying to join... is it possible to create the three joins by just putting in hibernate mappings so that I can use the hibernate getters and setters rather then hard coding the sql in my code... since the presentation layer will get pretty messy wont it...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.