Hello, I don't have much more knowledge about the Hibernate. I also have basic knowledge about the hibernate. I am giving you the basic code which will help you to improve your knowledge about the Hibernate. Just go through it and understand logic about it.
Code:
import java.util.*;
import java.sql.*;
import org.hibernate.*;
import org.hibernate.criterion.*;
public class Main
{
public static void main(String[] args)
{
HibernateUtil.setup("create table Employee ( id int, name VARCHAR);");
HibernateUtil.setup("create table Department ( id int, name VARCHAR, description VARCHAR, total double,branchid int);");
prepareData();
Session session = HibernateUtil.currentSession();
String sql = "select {branch.*} from Employee branch";
String hql = "update Employee set name = :newName where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Employee Name 1");
query.setString("newName","s1");
int rowCount = query.executeUpdate();
System.out.println("Rows affected: " + rowCount);
query = session.createQuery("from Employee");
List results = query.list();
displayEmployeeList(results);
HibernateUtil.checkData("select * from Employee");
HibernateUtil.checkData("select * from Department");
}
static public void displayEmployeeList(List list)
{
Iterator iter = list.iterator();
if (!iter.hasNext())
{
System.out.println("No branchs to display.");
return;
}
while (iter.hasNext())
{
Employee branch = (Employee) iter.next();
String msg = branch.getName();
System.out.println(msg);
}
}
private static void prepareData()
{
Session session = HibernateUtil.currentSession();
Employee branch1 = new Employee();
branch1.setName("Employee Name 1");
session.save(branch1);
Employee branch2 = new Employee();
branch2.setName("Employee Name 2");
session.save(branch2);
Department product1 = new Department("Department 1","Name for Department 1", 2.0);
product1.setEmployee(branch1);
branch1.getDepartments().add(product1);
session.save(product1);
Department product12 = new Department("Department 2","Name for Department 2", 22.0);
product12.setEmployee(branch1);
branch1.getDepartments().add(product12);
session.save(product12);
Department product2 = new Department("Department 3", "Name for Department 3", 30.0);
product2.setEmployee(branch2);
branch2.getDepartments().add(product2);
session.save(product2);
session.flush();
HibernateUtil.closeSession();
}
}
public class Department
{
private int id;
private Employee branch;
private String name;
private String description;
private double total;
public Department()
{
super();
}
public Department(String name, String description, double total)
{
super();
this.name = name;
this.description = description;
this.total = total;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Employee getEmployee()
{
return branch;
}
public void setEmployee(Employee branch)
{
this.branch = branch;
}
public double getPrice()
{
return total;
}
public void setPrice(double total)
{
this.total = total;
}
}
Bookmarks