public Object ObjectSplit(Object obj){
Session session = sf.getCurrentSession();
//获取关联文件key
AbstractEntityPersister classMetadata = (AbstractEntityPersister)sf.
getClassMetadata(obj.getClass());
String s = classMetadata.getIdentifierPropertyName();
Object value=null;
try {
//调用get方法获取属性
value= obj.getClass()
.getMethod("get" + FormatUtil.toUpperCaseFirstOne(s), new Class[] {})
.invoke(obj, new Object[] {}).toString();
Object seqObj=session.createQuery("select count(*) from "+
obj.getClass().getSimpleName()+" where "+s+" like :code").
setString("code", value.toString()+"%")
.uniqueResult();
int num=1;
if(seqObj!=null){
num+=Integer.parseInt(seqObj.toString());
}
if(value!=null){
List>list=session.createQuery("from "+obj.getClass()
.getSimpleName()+" where "+s+" =:code")
.setString("code", value.toString()).list();
if(list.size()==0)
return null;
else{
Object retObj=list.get(0);
//调用get方法获取属性 修改主键,并将对象持久化到数据库
retObj.getClass().getMethod("set" +
FormatUtil.toUpperCaseFirstOne(s),
new Class[] {value.getClass()})
.invoke(retObj, new Object[] {value+"_"+num});
session.evict(retObj);
session.save(retObj);
return value;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}