Java Code Examples for org.hibernate.Session.clear()

The following are Jave code examples for showing how to use clear() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 6 votes
@Override
public Integer save(App entity) {
    Session session = null;
    Integer pkId = null;
    try {
        session = sessionFactory.openSession();
        pkId = (Integer) session.save(entity);
    } catch (Exception e) {
        logger.error("error:", e);
    } finally {
        session.flush();
        session.clear();
        session.close();
    }

    return pkId;
}
 
Example 2
Project: sjk   File: MergeServiceImpl.java   View Source Code Vote up 6 votes
private Transaction batchIn1TransactionToApp(Session session, List<MarketApp> marketApps) {
    Transaction tx = null;
    try {
        session.setDefaultReadOnly(false);
        tx = session.beginTransaction();
        long start = System.currentTimeMillis();
        mergePaginationToApp(session, marketApps);
        session.flush();
        session.clear();
        logger.info("mergePaginationToApp耗时:{}", (System.currentTimeMillis() - start) + " ms!");
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        logger.error("Exception", e);
    }
    return tx;
}
 
Example 3
Project: sjk   File: BigGamePackDaoImpl.java   View Source Code Vote up 6 votes
@Override
public Integer save(BigGamePack entity) {
    Session session = null;
    Integer pkId = null;
    try {
        session = sessionFactory.openSession();
        pkId = (Integer) session.save(entity);
    } catch (Exception e) {
        logger.error("error:", e);
    } finally {
        session.flush();
        session.clear();
        session.close();
    }

    return pkId;
}
 
Example 4
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public void update(App entity) {
    Session session = null;
    try {
        session = sessionFactory.openSession();
        session.update(entity);
    } finally {
        session.flush();
        session.clear();
        session.close();
    }
}
 
Example 5
Project: gitplex-mit   File: DefaultPersistManager.java   View Source Code Vote up 5 votes
@Sessional
@Override
public void exportData(File exportDir, int batchSize) {
	Session session = sessionFactory.openSession();
	for (Class<?> entityType: getEntityTypes(sessionFactory)) {
		logger.info("Exporting table '" + entityType.getSimpleName() + "'...");
		
		logger.info("Querying table ids...");
		
		Criteria criteria = session.createCriteria(entityType, "entity")
				.setProjection(Projections.property("entity.id")).addOrder(Order.asc("id"));
		@SuppressWarnings("unchecked")
		List<Long> ids = criteria.list();
		int count = ids.size();
		
		for (int i=0; i<count/batchSize; i++) {
			exportEntity(session, entityType, ids, i*batchSize, batchSize, batchSize, exportDir);
			// clear session to free memory
			session.clear();
		}
		
		if (count%batchSize != 0) {
			exportEntity(session, entityType, ids, count/batchSize*batchSize, count%batchSize, batchSize, exportDir);
		}
		logger.info("");
	}
}
 
Example 6
Project: gitplex-mit   File: DefaultPersistManager.java   View Source Code Vote up 5 votes
@Sessional
@Override
public void importData(Metadata metadata, File dataDir) {
	Session session = dao.getSession();
	List<Class<?>> entityTypes = getEntityTypes(sessionFactory);
	Collections.reverse(entityTypes);
	for (Class<?> entityType: entityTypes) {
		File[] dataFiles = dataDir.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.startsWith(entityType.getSimpleName() + "s.xml");
			}
			
		});
		for (File file: dataFiles) {
			try {
				logger.info("Importing from data file '" + file.getName() + "'...");
				session.beginTransaction();
				VersionedDocument dom = VersionedDocument.fromFile(file);
				
				for (Element element: dom.getRootElement().elements()) {
					element.detach();
					AbstractEntity entity = (AbstractEntity) new VersionedDocument(DocumentHelper.createDocument(element)).toBean();
					session.replicate(entity, ReplicationMode.EXCEPTION);
				}
				session.flush();
				session.clear();
				session.getTransaction().commit();
			} catch (Throwable e) {
				session.getTransaction().rollback();
				throw Throwables.propagate(e);
			}
		}
	}	
}
 
Example 7
Project: maven_EWorld_OpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static String isUserExists(String uidoreml, String passw)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Object []> lst=new ArrayList<Object []> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select usr_id, pass, email_id from UserDetails ud where (ud.usr_id=:urid or ud.email_id=:eml) and ud.pass=:passd");
                qry.setParameter("urid", uidoreml);
                qry.setParameter("eml", uidoreml);
                qry.setParameter("passd",passw);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (null);
                  }
                  else
                  {
                       if ( ( (uidoreml.equals((lst.get(0))[0].toString())) || (uidoreml.equals((lst.get(0))[2].toString())) ) && (passw.equals((lst.get(0))[1].toString())))
                       {
                            return ((lst.get(0))[0].toString());
                       }
                       else
                       {
                            return (null);
                       }
                  }
            }
            else
            {
                 return (null);
            }
}
 
Example 8
Project: hibernate-ogm-ignite   File: ManyToOneTest.java   View Source Code Vote up 4 votes
@Test
@TestForIssue(jiraKey = "OGM-931")
public void canPersistAssociationToDetachedEntity() throws Exception {
	Session session = openSession();
	Transaction transaction = session.beginTransaction();

	// Persist employer
	Employer employer = new Employer();
	employer.setId( "employer-1" );
	employer.setName( "Hibernate" );
	session.save( employer );

	transaction.commit();
	session.clear();

	transaction = session.beginTransaction();

	// Create employee and associate it with detached employer
	Employee employee = new Employee();
	employee.setId( "employee-1" );
	employee.setName( "DNadar" );
	employee.setEmployer( employer );
	session.save( employee );

	transaction.commit();
	session.clear();

	transaction = session.beginTransaction();

	// Load association from main side
	Employee loadedEmployee = session.get( Employee.class, "employee-1" );
	assertThat( loadedEmployee.getEmployer().getName() ).isEqualTo( "Hibernate" );

	transaction.commit();
	session.clear();

	transaction = session.beginTransaction();

	// Load association from inverse side
	Employer loadedEmployer = session.get( Employer.class, "employer-1" );
	assertThat( loadedEmployer.getEmployees() ).onProperty( "name" ).containsOnly( "DNadar" );

	// Clean up
	session.delete( loadedEmployer.getEmployees().iterator().next() );
	session.delete( loadedEmployer );

	transaction.commit();
	session.close();
}
 
Example 9
Project: maven_EWorld_OpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isUserPasswordExists(String pass)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Object> lst=new ArrayList<Object> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select usr_id from UserDetails ud where ud.pass=:pss");
                qry.setParameter("pss", pass);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                        return (true);
                  }
            }
            else
            {
                 return (true);
            }
}
 
Example 10
Project: maven_EWorld_OpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateUserPhone(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserDetails ud set ud.mob_no=:mobno where ud.usr_id=:uid");
                qry.setParameter("uid", vals[0]);
                qry.setParameter("mobno", vals[1]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(nor!=0)
                 {
                      return (true);
                 }
                 else
                 {
                      return (false);
                 }
            }
            else
            {
                 return (false);
            }
}
 
Example 11
Project: maven_EWorld_OpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isPlaceExists(String desc)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Object> lst=new ArrayList<Object> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select pl_id from Places pl where pl.pl_desc=:pldesc");
                qry.setParameter("pldesc", desc);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                       return (true);
                  }
            }
            else
            {
                 return (true);
            }
}
 
Example 12
Project: EBwn2_ForOpenShift   File: Insert.java   View Source Code Vote up 4 votes
synchronized private static Places setPlace(String ... vals)
{
         UserDetails ud=null;
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         try{
               s=sf.openSession();
               s.beginTransaction();
               Query qry=s.createQuery("from UserDetails ud where ud.usr_id=:uid");
               qry.setParameter("uid", vals[0]);
               ud=(UserDetails)qry.getSingleResult();
               s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
                
           if(chk==0)
           {
                   Places pl=new Places ();
                   Address addr = pl.getAddr();
                   pl.setPname(vals[1]);
                   pl.setPl_type(vals[2]);
                   pl.setPl_id(generatePLACE_ID());
                   addr.setLocality(vals[3]);
                   addr.setCity(vals[4]);
                   addr.setPincode(vals[5]);
                   addr.setState(vals[6]);
                   addr.setCountry(vals[7]);
                   pl.setAddr(addr);
                   pl.setPl_desc(vals[8]);
                   pl.setUd(ud);
                   return (pl);
           }
           else
           {
                return (null);
           }
 }
 
Example 13
Project: maven_EWorld_OpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isUserPhoneExists(String pno)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Object> lst=new ArrayList<Object> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select usr_id from UserDetails ud where ud.mob_no=:mobno");
                qry.setParameter("mobno", pno);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                        return (true);
                  }
            }
            else
            {
                 return (true);
            }
}
 
Example 14
Project: EBwn2_ForOpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateUserEmail(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserDetails ud set ud.email_id=:email where ud.usr_id=:uid");
                qry.setParameter("uid", vals[0]);
                qry.setParameter("email", vals[1]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                if(nor!=0) 
                {
                     return (true);
                }
                else
                {
                     return (false);
                }
            }
            else
            {
                 return (false);
            }
}
 
Example 15
Project: EBwn2_ForOpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateUserPhone(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserDetails ud set ud.mob_no=:mobno where ud.usr_id=:uid");
                qry.setParameter("uid", vals[0]);
                qry.setParameter("mobno", vals[1]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(nor!=0)
                 {
                      return (true);
                 }
                 else
                 {
                      return (false);
                 }
            }
            else
            {
                 return (false);
            }
}
 
Example 16
Project: EBwn2_ForOpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateUserPassword(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserDetails ud set ud.pass=:passw where ud.usr_id=:uid");
                qry.setParameter("uid", vals[0]);
                qry.setParameter("passw", vals[1]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(nor!=0)
                 {
                     return (true);
                 }
                 else
                 {
                    return (false);
                 }
            }
            else
            {
                 return (false);
            }
}
 
Example 17
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 4 votes
@Override
public List<App> getApps(List<Integer> appIds) {
    String hql = "select id,name,catalog,subCatalog,keywords,description,downloadRank,adPopupTypes,signatureSha1,officialSigSha1,marketName from App app where app.hidden=0 and app.id in(:appIds)";

    Session session = null;
    try {
        session = this.sessions.openSession();
        Query query = session.createQuery(hql);
        query.setParameterList("appIds", appIds);
        List<Object[]> list = HibernateHelper.list(query);

        List<App> apps = null;
        if (list != null && !list.isEmpty()) {
            apps = new ArrayList<App>(list.size());
            for (Object[] obj : list) {
                App a = new App();
                String tmp = null;
                a.setId((Integer) obj[0]);
                a.setName(((String) obj[1]).toLowerCase().trim());
                a.setCatalog((Short) obj[2]);
                a.setSubCatalog((Integer) obj[3]);
                if ((tmp = (String) obj[4]) == null) {
                    a.setKeywords("");
                } else {
                    a.setKeywords(tmp.toLowerCase().trim());
                }
                if ((tmp = (String) obj[4]) == null) {
                    a.setDescription("");
                } else {
                    a.setDescription(tmp.toLowerCase().trim());
                }
                a.setDownloadRank((Integer) obj[6]);
                /* safe scan */
                a.setAdPopupTypes((String) obj[7]);
                a.setSignatureSha1((String) obj[8]);
                a.setOfficialSigSha1((String) obj[9]);
                /* safe scan */
                a.setMarketName((String) obj[10]);
                apps.add(a);
            }
            list.clear();
        }
        return apps;
    } catch (Exception e) {
        logger.error("error:", e);
        return null;
    } finally {
        if (session != null && session.isOpen()) {
            session.clear();
            session.close();
        }
    }
}
 
Example 18
Project: EBwn2_ForOpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isUserPhoneExists(String pno)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<String> lst=new ArrayList<String> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select usr_id from UserDetails ud where ud.mob_no=:mobno");
                qry.setParameter("mobno", pno);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                       return (true);
                  }
            }
            else
            {
                 return (true);
            }
}
 
Example 19
Project: maven_EWorld_OpenShift   File: Fetch.java   View Source Code Vote up 4 votes
synchronized public static List<Object> getPlaceRating(String plcid)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Object> lo=new ArrayList<Object> (); 
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("from Places pl where pl.pl_id=:plid");
                qry.setParameter("plid", plcid);
                lo.add(qry.getSingleResult());
                qry=s.createQuery("select ur from UserRatings ur left join ur.pl as plc where plc.pl_id=:plid");
                qry.setParameter("plid", plcid);
                lo.add(qry.getResultList());
                List<UserRatings> ur = (List<UserRatings>)lo.get(1);
                List<Object[]> o = new ArrayList<Object[]>();
                for(UserRatings urt:ur)
                {
                     o.add((Object[])((s.createQuery("select ud.firstname, ud.lastname from UserDetails ud left join ud.ur as urat where urat.rat_id=:rid")).setParameter("rid",urt.getRat_id())).getSingleResult());
                }
                lo.add(o);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(lo.isEmpty())
                 {
                      return (null);
                 }
                 else
                 {
                      return (lo);
                 }
            }
            else
            {
                 return (null);
            }
}
 
Example 20
Project: EBwn2_ForOpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isPlaceExists(String desc)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<String> lst=new ArrayList<String> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select pl_id from Places pl where pl.pl_desc=:pldesc");
                qry.setParameter("pldesc", desc);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                       return (true);
                  }
            }
            else
            {
                 return (true);
            }
}