import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
public class ManyToManyDemo {
@Test
public void save() {
Teacher teacher1 = new Teacher();
teacher1.setName("teacher1");
Teacher teacher2 = new Teacher();
teacher2.setName("teacher2");
Student student1 = new Student();
student1.setName("student1");
Student student2 = new Student();
student2.setName("student2");
teacher1.getStudents().add(student2);
teacher1.getStudents().add(student1);
teacher2.getStudents().add(student2);
teacher2.getStudents().add(student1);
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(student1);
session.save(student2);
session.save(teacher2);
session.save(teacher1);
session.getTransaction().commit();
session.close();
}
@Test
public void select() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
// String
// hql="select new map(t.name as tname,s.name as sname) from Teacher t inner join t.students s";
// List<Map> list = session.createQuery(hql).list();
//
// for(Map o:list){
// System.out.println(o);
// }
// List<Teacher> list=session.getNamedQuery("one").list();
// for(Teacher t:list){
// System.out.println(t);
// }
List list = session.getNamedQuery("sql").setString("name", "teacher1")
.list();
for (Object o : list) {
System.out.println(o);
}
session.getTransaction().commit();
session.close();
}
@Test
public void select1() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
String hql = "from Teacher t left join fetch t.students s where t.name=?";
Query query = session.createQuery(hql);
query.setString(0, "teacher1");
List<Teacher> result = query.list();
for (Teacher obj : result) {
System.out.println(obj);
}
session.getTransaction().commit();
session.close();
}
@Test
public void select2() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
String hql = "from Teacher t left join fetch t.students s where s.name in (:names)";
Query query = session.createQuery(hql);
String[] arr = { "student1", "student2" };
query.setParameterList("names", arr);
List<Teacher> result = query.list();
for (Teacher obj : result) {
System.out.println(obj);
}
session.getTransaction().commit();
session.close();
}
@Test
public void select3() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//String hql = "select count(t.id) from Teacher t left join t.students s where s.name in (:names) group by t.id";
String hql = "select count(distinct t.id) from Teacher t left join t.students s where s.name in (:names)";
Query query = session.createQuery(hql);
String[] arr = { "student1", "student2" };
query.setParameterList("names", arr);
Long result = (Long) query.uniqueResult();
System.out.println(result);
session.getTransaction().commit();
session.close();
}
}
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
public class ManyToManyDemo {
@Test
public void save() {
Teacher teacher1 = new Teacher();
teacher1.setName("teacher1");
Teacher teacher2 = new Teacher();
teacher2.setName("teacher2");
Student student1 = new Student();
student1.setName("student1");
Student student2 = new Student();
student2.setName("student2");
teacher1.getStudents().add(student2);
teacher1.getStudents().add(student1);
teacher2.getStudents().add(student2);
teacher2.getStudents().add(student1);
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(student1);
session.save(student2);
session.save(teacher2);
session.save(teacher1);
session.getTransaction().commit();
session.close();
}
@Test
public void select() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
// String
// hql="select new map(t.name as tname,s.name as sname) from Teacher t inner join t.students s";
// List<Map> list = session.createQuery(hql).list();
//
// for(Map o:list){
// System.out.println(o);
// }
// List<Teacher> list=session.getNamedQuery("one").list();
// for(Teacher t:list){
// System.out.println(t);
// }
List list = session.getNamedQuery("sql").setString("name", "teacher1")
.list();
for (Object o : list) {
System.out.println(o);
}
session.getTransaction().commit();
session.close();
}
@Test
public void select1() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
String hql = "from Teacher t left join fetch t.students s where t.name=?";
Query query = session.createQuery(hql);
query.setString(0, "teacher1");
List<Teacher> result = query.list();
for (Teacher obj : result) {
System.out.println(obj);
}
session.getTransaction().commit();
session.close();
}
@Test
public void select2() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
String hql = "from Teacher t left join fetch t.students s where s.name in (:names)";
Query query = session.createQuery(hql);
String[] arr = { "student1", "student2" };
query.setParameterList("names", arr);
List<Teacher> result = query.list();
for (Teacher obj : result) {
System.out.println(obj);
}
session.getTransaction().commit();
session.close();
}
@Test
public void select3() {
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//String hql = "select count(t.id) from Teacher t left join t.students s where s.name in (:names) group by t.id";
String hql = "select count(distinct t.id) from Teacher t left join t.students s where s.name in (:names)";
Query query = session.createQuery(hql);
String[] arr = { "student1", "student2" };
query.setParameterList("names", arr);
Long result = (Long) query.uniqueResult();
System.out.println(result);
session.getTransaction().commit();
session.close();
}
}