我正在研究一个swing应用程序,因为它的性能来处理我选择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;
}
答案 0 :(得分:0)
也许你试着缩小究竟是什么问题。您的问题可以通过适当的索引等来修复。也许可以提供更多信息。
通常,DB中最慢的部分是数据库连接创建。你在使用DB Pooling吗?
答案 1 :(得分:0)
不,绝对不是这样。 hibernate是最好的ORM实现之一。你应该找出你的查询,数据库索引和其他优化。如果数据库,大多数应用程序的最大瓶颈。 如果你看到,hibernate内部使用jdbc并触发SQL查询。它大大减少了你的工作,并提供了非常具体的实现和接口。 请提供更多描述你的问题来解释为什么你觉得hibernate是罪魁祸首而不是别的什么。
答案 2 :(得分:0)
你提到数据库实体之间存在复杂的关系,如果你没有,你可能想查看Hibernate“延迟加载”。
延迟加载控制相关实体何时以及如何加载到内存,在许多情况下,它可以带来巨大的性能提升。
答案 3 :(得分:0)
您是否返回大型数据集然后使用Java将其过滤为少量记录?我以前见过它。通过网络传输大量数据只是丢弃大部分数据是一个常见的错误。