📄 hibernateoperation.java
字号:
// List result=query.list();
// Computer c=(Computer)result.iterator().next();
//
// Computer c1=(Computer)session.load(Computer.class,new Long(c.getId()));
// tx.commit();
// return c1;
// }catch (Exception e) {
// if (tx != null) {
// // Something went wrong; discard all partial changes
// tx.rollback();
// }
// throw e;
// } finally {
// // No matter what, close the session
// session.close();
// }
// }
//
//
/**
* 根据学号来加载学生对象
*/
// public Student findStudent1(String sno) throws Exception{
// // Ask for a session using the JDBC information we've configured
// Session session = sessionFactory.openSession();
// Transaction tx = null;
// try {
// tx = session.beginTransaction();
// List t=session.find("from Student s where s.sno="+sno);
// Student it=(Student)t.iterator().next();
// Student it1=(Student)session.load(Student.class,new Long(it.getId()));
// tx.commit();
// return it1;
// }catch (Exception e) {
// if (tx != null) {
// // Something went wrong; discard all partial changes
// tx.rollback();
// }
// throw e;
// } finally {
// // No matter what, close the session
// session.close();
// }
// }
public Computer loadComputer(int id) throws Exception{//根据id查computer
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List l=session.find("from Computer where id="+id);
Computer c=(Computer)l.iterator().next();
//session.save(l);
Hibernate.initialize(c.getSc());
tx.commit();
return c;
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public Student findStudent(int id) throws Exception{//根据id查computer
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List l=session.find("from Student where id="+id);
Student stu=(Student)l.iterator().next();
//session.save(l);
Hibernate.initialize(stu.getSc());
tx.commit();
return stu;
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public List findAllComputer() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
List l1=new ArrayList();
try {
tx = session.beginTransaction();
l1=session.find("from Computer");
tx.commit();
return l1;
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public List findAllSc() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
List l1=new ArrayList();
try {
tx = session.beginTransaction();
l1=session.find("from Sc");
tx.commit();
return l1;
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public void saveStudents(Student stu) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(stu);
tx.commit();
}catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public List loadStudent(int a) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Student c where c.id=:id");
query.setLong("id",a);
List result = query.list();
tx.commit();
return result;
}catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public List loadSc1(int a) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Sc c where c.student.id=:id");
query.setLong("id",a);
List result = query.list();
tx.commit();
return result;
}catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void deleteSc(Student student,Computer computer) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Sc sc=new HibernateOperation().findSc(student,computer);
session.delete(sc);
tx.commit();
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
//根据computer的id在sc表中查出相关信息,用与显示选了此“等级”的学生信息
public List loadSc2(int a) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Sc c where c.computer.id=:id");
query.setLong("id",a);
List result = query.list();
tx.commit();
return result;
}catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public List findComputer(int a) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Sc c where c.student.id=:id");
query.setLong("id",a);
List result = query.list();
tx.commit();
return result;
}catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public Sc loadSc(long id) throws Exception{//根据id查computer
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List l=session.find("from Sc where id="+id);
Sc c=(Sc)l.iterator().next();
//session.save(l);
tx.commit();
return c;
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public void test() throws Exception
{
HibernateOperation h=new HibernateOperation();
}
// catch(Exception e){e.printStackTrace();}
//
public static void main(String args[]) throws Exception {
new HibernateOperation().test();
try
{
HibernateOperation h=new HibernateOperation();
Student stu=(Student)h.loadStudent(1);
Computer computer=h.loadComputer(1);
h.deleteSc(stu,computer);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -