I’m very new to Hibernate and just wanted to query the initial basics.
I’ve created my Hibernate bean as such…
package com.behaviour.chapter1;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
private int userId;
private String firstName;
@Id
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
I’ve configured my hibernate.cfg.xml and db connection there. My question is simply, how do I actually call this from a main method and use it in Hibernate3.6.6? I was following a tutorial online but it was for Hibernate3.2 and it seems to be a bit different. If anyone could show me just a really simple main method calling this bean, creating a new user (that would create a user in this table) it would be greatly appreciated. Also – if anyone has any good Hibernate tutorial links that would be great 🙂
Thanks,
There are several ways of doing this, it is a matter of design choice, a basic way to achieve this will be to create the session factory from
hibernate.cfg.xmlfile. Make sure the file can be located at your classpath.And using the class below, create a Session Factory object which is then used to open up new Session’s
Now to create a new user, do:
Main
EDIT
Your
hibernate.cfg.xmlshould look something like this: