In the below exercises, i use Mysql for database. There are four classes for four different actions.
I used MyEclipse 6.6 for these examples.
- Open MyEclipse / New / Project. Choose Java Project.
- After creating your new project by right clicking the project name in the package explorer, choose MyEclipse / Add Hibernate Capabilities. You will see:
- Without changing default settings, click the Next button.
- Write your connection string, username and password here,:
- And in the last part of the configuration, write new Java Package for your classes by clicking the New button.
then click Next.
I have a table called Users and four columns in it. These columns’ names are Firstname, Lastname, Email and Password. Before you do something with Hibernate, let’s change our perspective by clicking Window/Open Perspective/MyEclipse Hibernate. When the DB Browser is shown in the scene, right click your table name, select Hibernate Reverse Engineering.
- Listing:
Create a new Java class by right clicking your package, which you created few steps before, in the Package Explorer. Choose create method static void main and give your class a name. - Inserting:
Create a new class for insert new data to your database - Updating:
- Deleting:
Copy and paste this code to your new class:
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class UsersList {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
Session session = sessionFact.openSession();
Query query = session.createQuery(”from Users”);
List users = query.list();
for (Users users2 : users) {
System.out.println(users2.getFirstname());
}
}
}
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class UserAdd {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
Session ses = sessionFact.openSession();
Transaction trans = ses.beginTransaction();
Users user = new Users();
user.setFirstname(”John”);
user.setLastname(”Locke”);
user.setEmail(”jj@c.com”);
user.setPassword(”john”);
ses.save(user);
trans.commit();
ses.flush();
ses.close();
}
}
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class UserUpdate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
Session session = sessionFact.openSession();
Transaction trans = session.beginTransaction();
Users user = (Users) session.get(Users.class, new Integer(5));
user.setFirstname(”x”);
session.update(user);
trans.commit();
session.close();
System.out.println(”İslem basarıyla tamamlandı.”);
}
}
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class UserDelete {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session ses = new Configuration().configure().buildSessionFactory().openSession();
Transaction trans = ses.beginTransaction();
String hql = “delete Users where id = 5″;
Query query = ses.createQuery(hql);
int rowCnt = query.executeUpdate();
trans.commit();
System.out.println(rowCnt);
}
}
P.S.: Right click your classes and choose Run As / Java Application to run.
