TEST类
package pojo;import org.hibernate.Session;
import pojohelp.HibernateUtil;
import org.hibernate.Query;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class test {
public static void main(String args[])
{
HibernateUtil hibernateutil=new HibernateUtil();
//
//SessionFactory d;
//d=new Configuration().configure().buildSessionFactory();
//Session session = d.openSession();
//Session session = hibernateutil.currentSession();
String hql="";
String code="03130";
hql = "from Student u where u.nameid like '" + code + "%'";
try
{
Session session = hibernateutil.currentSession();
Query query = session.createQuery(hql);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
HibernateUtil.closeSession();
}
}
}
HibernateUtil 类
package pojohelp;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.apache.commons.beanutils.BeanUtils;
import java.util.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;public class HibernateUtil
{
private static final SessionFactory sessionFactory; static
{
try
{
sessionFactory = new Configuration().configure().
buildSessionFactory();
}
catch (HibernateException ex)
{
throw new RuntimeException("Exception building SessionFactory: " +
ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException
{
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
public static Object toVO(Object poObject)throws Exception
{
if(poObject==null)
return null;
if(poObject instanceof List)
{
return toVOList((List)poObject);
}
else if(poObject instanceof Set)
{
return toVOSet((Set)poObject);
}
else
{
return toSingleVO(poObject);
}
}
public static List toVOList(List poList)throws Exception
{
if(poList==null)
return null;
List list=new ArrayList();
Iterator iterator=poList.iterator();
while(iterator.hasNext())
{
list.add(toSingleVO(iterator.next()));
}
return list;
}
public static Set toVOSet(Set set)throws Exception
{
if(set==null)
return null;
Set tempSet=new HashSet();
Iterator iterator=set.iterator();
while(iterator.hasNext())
{
tempSet.add(toSingleVO(iterator.next()));
}
return tempSet;
}
public static Object toSingleVO(Object po)throws Exception
{
if(po==null)
return null;
String className=getRealName(po.getClass().getName());
Object vo=Class.forName(className).newInstance();
clearNullNumber(vo,po);
BeanUtils.copyProperties(vo,po);
return vo;
}
private static String getRealName(String className)
{
int x=className.indexOf('$');
if(x<0)
{
return className;
}
else
return className.substring(0,x);
}
private static void clearNullNumber(Object vo,Object po)throws Exception
{
Field[] fields=vo.getClass().getDeclaredFields();
String fieldName;
for(int i=0;i<fields.length;i++)
{
fieldName=fields[i].getName();
checkZero(fieldName,po);
}
}
private static void checkZero(String fieldName,Object po)throws Exception
{
String getterName=getMethodGetter(fieldName);
String setterName=getMethodSetter(fieldName);
Method[] methods=po.getClass().getDeclaredMethods();
Method getter=null,setter=null;
for(int i=0;i<methods.length && (getter==null || setter==null);i++)
{
if(getterName.equals(methods[i].getName()))
{
getter=methods[i];
}
else if(setterName.equals(methods[i].getName()))
{
setter=methods[i];
}
}
if(getter==null || setter==null)
return;
Object obj=getter.invoke(po,null);
if(obj!=null)
return;
Class returnType=getter.getReturnType();
if(returnType.equals(java.math.BigDecimal.class))
{
System.out.println("发现 fieldName="+fieldName+" 为BigDecimal的情况 并且值为NULL");
Object[] args={new java.math.BigDecimal(0)};
setter.invoke(po,args);
}
}
private static String getMethodGetter(String fieldName)
{
char ch = fieldName.charAt(0);
if (ch <= 'Z')
return "get" + fieldName;
ch = (char) (ch - 'a' + 'A');
fieldName = ch + fieldName.substring(1, fieldName.length());
return "get" + fieldName;
}
private static String getMethodSetter(String fieldName)
{
char ch = fieldName.charAt(0);
if (ch <= 'Z')
return "set" + fieldName;
ch = (char) (ch - 'a' + 'A');
fieldName = ch + fieldName.substring(1, fieldName.length());
return "set" + fieldName;
}
}
package pojo;import org.hibernate.Session;
import pojohelp.HibernateUtil;
import org.hibernate.Query;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class test {
public static void main(String args[])
{
HibernateUtil hibernateutil=new HibernateUtil();
//
//SessionFactory d;
//d=new Configuration().configure().buildSessionFactory();
//Session session = d.openSession();
//Session session = hibernateutil.currentSession();
String hql="";
String code="03130";
hql = "from Student u where u.nameid like '" + code + "%'";
try
{
Session session = hibernateutil.currentSession();
Query query = session.createQuery(hql);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
HibernateUtil.closeSession();
}
}
}
HibernateUtil 类
package pojohelp;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.apache.commons.beanutils.BeanUtils;
import java.util.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;public class HibernateUtil
{
private static final SessionFactory sessionFactory; static
{
try
{
sessionFactory = new Configuration().configure().
buildSessionFactory();
}
catch (HibernateException ex)
{
throw new RuntimeException("Exception building SessionFactory: " +
ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException
{
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null)
{
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
public static Object toVO(Object poObject)throws Exception
{
if(poObject==null)
return null;
if(poObject instanceof List)
{
return toVOList((List)poObject);
}
else if(poObject instanceof Set)
{
return toVOSet((Set)poObject);
}
else
{
return toSingleVO(poObject);
}
}
public static List toVOList(List poList)throws Exception
{
if(poList==null)
return null;
List list=new ArrayList();
Iterator iterator=poList.iterator();
while(iterator.hasNext())
{
list.add(toSingleVO(iterator.next()));
}
return list;
}
public static Set toVOSet(Set set)throws Exception
{
if(set==null)
return null;
Set tempSet=new HashSet();
Iterator iterator=set.iterator();
while(iterator.hasNext())
{
tempSet.add(toSingleVO(iterator.next()));
}
return tempSet;
}
public static Object toSingleVO(Object po)throws Exception
{
if(po==null)
return null;
String className=getRealName(po.getClass().getName());
Object vo=Class.forName(className).newInstance();
clearNullNumber(vo,po);
BeanUtils.copyProperties(vo,po);
return vo;
}
private static String getRealName(String className)
{
int x=className.indexOf('$');
if(x<0)
{
return className;
}
else
return className.substring(0,x);
}
private static void clearNullNumber(Object vo,Object po)throws Exception
{
Field[] fields=vo.getClass().getDeclaredFields();
String fieldName;
for(int i=0;i<fields.length;i++)
{
fieldName=fields[i].getName();
checkZero(fieldName,po);
}
}
private static void checkZero(String fieldName,Object po)throws Exception
{
String getterName=getMethodGetter(fieldName);
String setterName=getMethodSetter(fieldName);
Method[] methods=po.getClass().getDeclaredMethods();
Method getter=null,setter=null;
for(int i=0;i<methods.length && (getter==null || setter==null);i++)
{
if(getterName.equals(methods[i].getName()))
{
getter=methods[i];
}
else if(setterName.equals(methods[i].getName()))
{
setter=methods[i];
}
}
if(getter==null || setter==null)
return;
Object obj=getter.invoke(po,null);
if(obj!=null)
return;
Class returnType=getter.getReturnType();
if(returnType.equals(java.math.BigDecimal.class))
{
System.out.println("发现 fieldName="+fieldName+" 为BigDecimal的情况 并且值为NULL");
Object[] args={new java.math.BigDecimal(0)};
setter.invoke(po,args);
}
}
private static String getMethodGetter(String fieldName)
{
char ch = fieldName.charAt(0);
if (ch <= 'Z')
return "get" + fieldName;
ch = (char) (ch - 'a' + 'A');
fieldName = ch + fieldName.substring(1, fieldName.length());
return "get" + fieldName;
}
private static String getMethodSetter(String fieldName)
{
char ch = fieldName.charAt(0);
if (ch <= 'Z')
return "set" + fieldName;
ch = (char) (ch - 'a' + 'A');
fieldName = ch + fieldName.substring(1, fieldName.length());
return "set" + fieldName;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货