-2

我正在开发一个摇摆应用程序,以处理我选择 Hibernate 的数据,因为它的性能。现在我完成了 80% 的工作。我发现应用程序太慢了,就像永远在等待。我google了一下,发现如果有很多数据,Hibernate并不是最好的选择。我不知道,问题是我的数据库很复杂,有很多关联、连接、集合……我没有时间重新开始!请帮忙

这是一段代码,这是我检索数据的方式,每个实体都有一个相似的类

public class CoursCrud {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();  
Session session ;
 public ArrayList<Object> findCoursByCoursId( int id){
    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findLastCours(){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findLastCours");
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findAllCours(){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findAllCours");
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findProfByCoursId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findProfByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}

  public ArrayList<Object> findGroupeByCoursId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findGroupeByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findGroupeByCreneauId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findGroupeByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findSalleByCreneauId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findSalleByCreneauId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findProfByCreneauId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findProfByCreneauId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findCreneauFromCoursByCreneauId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCreneauFromCoursByCreneauId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();

    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findCoursByCreneauId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursByCreneauId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}

  public ArrayList<Object> findSalleByCoursId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findSalleByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findCreneauByCoursId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCreneauByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}

public   ArrayList<Object> findCoursByGroupeId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursByGroupeId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;
}
public   ArrayList<Object> findCoursBySalleId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursBySalleId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;
}
public   ArrayList<Object> findProfBySalleId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findProfBySalleId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;
}


  public int findIndex(int id){
    CoursCrud C= new CoursCrud();
    ArrayList<Object> cours =C.findAllCours(); 
    int i=0;
    for(Object cu: cours){
        i++;
        if(((Cours) cu).getId() == id){
            break;
        }
    }
    return --i;
}
public void createData( Object p){
    session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(p);  
        transaction.commit();  
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }  
}
public void updateData( Object p){

    session = sessionFactory.openSession();  
    session.beginTransaction();  

    session.update(p);  
    session.getTransaction().commit();  

    session.close();
}

public void CoursEmpty(){  
    CoursCrud C= new CoursCrud();
    List<Object> cc = C.findAllCours();
    for(Object c: cc){
        C.deleteCoursByCoursId(((Cours)c).getId());
    }
}
public void deleteCoursByCoursId(int id){
    session = HibernateUtil.getSessionFactory().openSession();
    CoursCrud C= new CoursCrud();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();     
        Cours c = (Cours) C.findCoursByCoursId(id).get(0);
        session.delete(c);
        transaction.commit();  
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }   
}

public void deleteCoursCreneauByCoursId(int id){
    session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();     
        Query Q = session.getNamedQuery("deleteCoursCreneauByCoursId");
        Q.setLong("id", id).executeUpdate();
        transaction.commit();  
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
        System.out.println("Not succ !! \n");
    } finally {
        session.close();
    }   
}

  public ArrayList<Object> findCreneauIndisponibiliteByCoursId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCreneauIndisponibiliteByCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findCoursIndisponibleByCreneauId(int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursIndisponibleByCreneauId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findCoursByClassCoursId( int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursByClassCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public void deleteCoursByClassCours(int id){
    CoursCrud C= new CoursCrud();
    for(Object o: C.findCoursByClassCoursId(id)){
        C.deleteCoursSeanceByCoursId(((Cours)o).getId());
        C.deleteCoursCreneauByCoursId(((Cours)o).getId());
        C.deleteCoursByCoursId(((Cours)o).getId());
    }
}
  public ArrayList<Object> findGroupeByClassCoursId( int id){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findGroupeByClassCoursId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Integer> findIndexGroupebyClassCours(int id){
    CoursCrud C= new CoursCrud();
    GroupeCrud Gc= new GroupeCrud();
    ArrayList<Object> groupeSelectedList= C.findGroupeByClassCoursId(id);
    ArrayList<Object> groupeList= Gc.findAllGroupe();
    ArrayList<Integer> indexSelectedList= new ArrayList<Integer>();

    Alert a= new Alert();
    for(Object m: groupeList){
        for(Object ms: groupeSelectedList){
            if(((Groupe) m).getId() == ((Groupe) ms).getId() ){
                indexSelectedList.add(groupeList.indexOf(m));
            }
        }
    }
    return indexSelectedList;
}
  public ArrayList<Integer> findIndexSeancebyClassCours(int id){
    CoursCrud C= new CoursCrud();
    SeanceCrud sC=new SeanceCrud();
    ArrayList<Seance> SeanceSelectedList = new ArrayList<Seance>();
    for(Object o: C.findCoursByClassCoursId(id)){
        for(Object v: sC.findSeanceByCoursId(((Cours)o).getId())){
            SeanceSelectedList.add((Seance)v);
        }
    }

    ArrayList<Object> seanceList= sC.findAllSeance();
    ArrayList<Integer> indexSelectedList= new ArrayList<Integer>();

    Alert a= new Alert();
    for(Object m: seanceList){
        for(Seance ms: SeanceSelectedList){
            if(((Seance) m).getId() == ms.getId() ){
                indexSelectedList.add(seanceList.indexOf(m));
            }
        }
    }
    return indexSelectedList;
}
public void deleteCoursSeanceByCoursId(int id){
    CoursCrud C= new CoursCrud();
    session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();     
        Cours c = (Cours) C.findCoursByCoursId(id).get(0);
        c.getCoursSeance().clear();
        session.update(c);
        transaction.commit();  
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }   
}
  public ArrayList<Integer> findIndexIndisponibilitebyCoursId(int id){
    CoursCrud C= new CoursCrud();
    CreneauCrud cC= new CreneauCrud();
    ArrayList<Object> IndispoSelectedList= C.findCreneauIndisponibiliteByCoursId(id);
    ArrayList<Object> creneauList= cC.findAllCreneau();
    ArrayList<Integer> indexindispoSelectedList= new ArrayList<Integer>();

    Alert a= new Alert();
    for(Object m: creneauList){
        for(Object ms: IndispoSelectedList){
            if(((Creneau) m).getId() == ((Creneau) ms).getId() ){
                indexindispoSelectedList.add(creneauList.indexOf(m));
            }
        }
    }
    return indexindispoSelectedList;
}
  public ArrayList<Object> findCoursIndisponibleByCreneauIdAndCoursId(int idCreneau, int idCours){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursIndisponibleByCreneauIdAndCoursId");
    Q.setLong("idCours", idCours);
    Q.setLong("idCreneau", idCreneau);

    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
  public ArrayList<Object> findCoursByCreneauIdAndGroupeId(int idCreneau, int idGroupe){

    session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findCoursByCreneauIdAndGroupeId");
    Q.setLong("idGroupe", idGroupe);
    Q.setLong("idCreneau", idCreneau);

    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
4

4 回答 4

0

no this is definitely not the case. hibernate is one of the best ORM implementations out there. you should figure out your queries, db indexes and other optimizations. The biggest bottleneck in most of the apps if the DB. if you see, hibernate internally uses jdbc and fires SQL queries. it reduces your work a lot and provides very concrete implementations and interfaces. please provide more description to your problem to explain why you feel hibernate is the culprite and not anything else.

于 2013-08-22T01:51:35.470 回答
0

也许您尝试缩小问题的确切范围。您的问题可以通过适当的索引等解决。也许提供更多信息。

通常 DB 最慢的部分是 DB 连接创建。你在使用数据库池吗?

于 2013-08-22T01:49:42.933 回答
0

您提到数据库实体之间存在复杂的关系,如果没有,您可能想查看 Hibernate“延迟加载”。

延迟加载控制何时以及如何将相关实体加载到内存中,在许多情况下,它可以带来巨大的性能提升。

于 2013-08-22T01:58:37.533 回答
0

您是否返回一个大型数据集,然后使用 Java 将其过滤为少量记录?我以前见过。通过网络传输大量数据只是为了丢弃大部分数据是一个常见的错误。

于 2013-08-22T01:59:25.647 回答