This is my model class.Here we are using setters and getters methods to initialize variables:
package org.koushik.javabrains.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
This is my model object and assigning values to the variables created in model class:
package org.koushik.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
//import org.hibernate.Transaction;
import org.koushik.javabrains.dto.UserDetails;
public class HibernateTest {
public static void main(String arg[])
{
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName("First User");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.beginTransaction().commit();
}
}