Jul 19, 2024
hb.cfg.xml, you need to pass this name as a parameter when calling the configure method.
config.configure("hb.cfg.xml")
Student table, we create a Student class annotated with @Entity, @Table, @Column, @Id.addAnnotatedClass method.
config.addAnnotatedClass(Student.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.commit();
Configuration config = new Configuration().configure("hb.cfg.xml")
.addAnnotatedClass(Student.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setId(3);
student.setName("Charlie");
student.setDepartment("Mech");
student.setPercentage(80);
session.save(student);
tx.commit();
Query<Student> query = session.createQuery("from Student", Student.class);
List<Student> students = query.getResultList();
for(Student s: students) {
System.out.println(s);
}
String hql = "from Student";
String hql = "from Student s where s.percentage > 70";
Query<Student> query = session.createQuery(hql, Student.class);
List<Student> students = query.getResultList();
for(Student s: students) {
System.out.println(s);
}
hibernate.format_sql property to hibernate.cfg.xml.
<property name="hibernate.format_sql">true</property>
``