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.  [ 5 posts ] 
Author Message
 Post subject: Very new to Hibernate
PostPosted: Wed May 06, 2009 10:12 am 
Newbie

Joined: Wed May 06, 2009 10:00 am
Posts: 6
Hello

I am very new to this, trying to make an webapplication in netbeans 6.5 with hibernate to connect to the database. I want to do an insert to a table from some values that you get from the webpage in a form... but how do I do that, for the select do need a table? I want to use the variables I send in the function.... but this doesnt work... I really need help! :) And I have to be finished friday....

public boolean getName(String enavn,String fnavn,Integer mobil) {
TExternalDataHotel name= new TExternalDataHotel();
try {
org.hibernate.Transaction tx = session.beginTransaction();
int q = session.createQuery("insert into TExternalDataHotel (firstname,surname,mobilNumber)select fnavn,enavn,mobil").executeUpdate();


} catch (Exception e) {
e.printStackTrace();
}


return true;
}
}

thanks for helping! :)


Top
 Profile  
 
 Post subject: Re: Very new to Hibernate
PostPosted: Wed May 06, 2009 10:40 am 
Newbie

Joined: Thu Jun 22, 2006 12:11 am
Posts: 14
you want to populate the entity and the persist it

i.e.

name.setFirstname(firstname);
name.setSurname(surname);

etc etc

then

session.save(name);

though this is about as basic as it gets when using hibernate, perhaps you should brush up on it a little bit.


Top
 Profile  
 
 Post subject: Re: Very new to Hibernate
PostPosted: Thu May 07, 2009 5:30 am 
Newbie

Joined: Wed May 06, 2009 10:00 am
Posts: 6
Thanks, yes found that another place after writing this topic yesterday, but then I wonder, when do we use createQuery and a insert statement? Maybe a silly question, but as the topic is names, I am very new to this :)


Top
 Profile  
 
 Post subject: Re: Very new to Hibernate
PostPosted: Thu May 07, 2009 7:58 am 
Newbie

Joined: Wed May 06, 2009 10:00 am
Posts: 6
It works, and I do a sequence insert for the id, but wonder why still the log is like this:

Hibernate: select nextval ('t_external_data_hotel_id_seq')
Hibernate: insert into public.t_external_data_hotel (first_name, surname, mobile_number, id) values (?, ?, ?, ?)

and not with the real values, and without the id column?


:)


Top
 Profile  
 
 Post subject: Re: Very new to Hibernate
PostPosted: Fri May 08, 2009 8:46 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
bella_Hiber wrote:
It works, and I do a sequence insert for the id, but wonder why still the log is like this:

Hibernate: select nextval ('t_external_data_hotel_id_seq')
Hibernate: insert into public.t_external_data_hotel (first_name, surname, mobile_number, id) values (?, ?, ?, ?)

and not with the real values, and without the id column?


:)


There is an ID field in the statement. Which is a PreparedStatement, so you only see ? where Hibernate fills in the correct values.
First it selects the next value for your ID. Then it creates an insert statement with the parameters you have to set via yourObject.setXyz() and the ID retrieved just before. Hibernate doesn't do anything you could not do by using plain JDBC, but it makes things a lot easier. You don't have to care about creating SQL-Statements an spell all the fields right and use the correct SQL-Function for e.g. datefields.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

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.