在j2se环境中ejb3.0操作entity bean的通用类 PerfectEntityManager
package com.vsc.study.ejb.util;
import com.vsc.study.ejb.entitybean.Function;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class PerfectEntityManager {
private EntityManagerFactory emf;
private EntityManager em;
public PerfectEntityManager() {
emf = Persistence.createEntityManagerFactory("study-ejbPU");
em = emf.createEntityManager();
}
public PerfectEntityManager(String utinName) {
emf = Persistence.createEntityManagerFactory(utinName);
em = emf.createEntityManager();
}
public Map<Integer, List> getCurrentResults(int currentPage, int pageSize,
int maxResults, String querySql) {
if (maxResults == 0) {
maxResults = em.createQuery(querySql).getResultList().size();
}
List list = em.createQuery(querySql).setFirstResult(
(currentPage - 1) * pageSize).setMaxResults(pageSize)
.getResultList();
HashMap<Integer, List> hashMap = new HashMap<Integer, List>();
hashMap.put(maxResults, list);
return hashMap;
}
public Map<Integer, List> getCurrentResults(int currentPage, int pageSize,
int maxResults, Object object) {
if (maxResults == 0) {
maxResults = em.createQuery("from " + object.getClass())
.getResultList().size();
}
List list = em.createQuery("from " + object.getClass()).setFirstResult(
(currentPage - 1) * pageSize).setMaxResults(pageSize)
.getResultList();
HashMap<Integer, List> hashMap = new HashMap<Integer, List>();
hashMap.put(maxResults, list);
return hashMap;
}
String separator = " ";
public String setEjbql(Object object, List<Ejbql> ejbqlList) {
StringBuilder sb = new StringBuilder("select o from "
+ object.getClass() + " as o where 1=1 ");
for (Ejbql ejbql : ejbqlList) {
if (ejbql == null) {
continue;
}
if (ejbql.getCloumns() == null) {
continue;
}
if (ejbql.getOperator() == null) {
continue;
}
if (ejbql.getValues() == null) {
continue;
}
sb.append(ejbql.getAndOr() + separator);
sb.append("o." + ejbql.getCloumns());
if (ejbql.getOperator().equals("like")) {
sb.append(separator + ejbql.getOperator() + separator);
sb.append("'%" + ejbql.getValues() + "%'" + separator);
} else {
sb.append(ejbql.getOperator() + separator);
if (ejbql.getValues() instanceof String) {
sb.append("'" + ejbql.getValues() + "'" + separator);
} else {
sb.append(ejbql.getValues() + separator);
}
}
}
return sb.toString();
}
public String setEjbql(String classname, List<Ejbql> ejbqlList) {
StringBuilder sb = new StringBuilder("select o from " + classname
+ " as o where 1=1 ");
for (Ejbql ejbql : ejbqlList) {
if (ejbql == null) {
continue;
}
if (ejbql.getCloumns() == null) {
continue;
}
if (ejbql.getOperator() == null) {
continue;
}
if (ejbql.getValues() == null) {
continue;
}
sb.append(ejbql.getAndOr() + separator);
sb.append("o." + ejbql.getCloumns());
if (ejbql.getOperator().equals("like")) {
sb.append(separator + ejbql.getOperator() + separator);
sb.append("'%" + ejbql.getValues() + "%'" + separator);
} else {
sb.append(ejbql.getOperator() + separator);
if (ejbql.getValues() instanceof String) {
sb.append("'" + ejbql.getValues() + "'" + separator);
} else {
sb.append(ejbql.getValues() + separator);
}
}
}
return sb.toString();
}
public int remove(Object object) {
if (object == null)
throw new NullPointerException();
try {
em.getTransaction().begin();
em.remove(this.search(object));
em.getTransaction().commit();
em.close();
emf.close();
return 0;
} catch (Exception e) {
return -2;
}
}
public int update(Object object) {
if (object == null || this.getId(object) == null)
throw new NullPointerException();
Object obj = this.search(object);
this.updateBean(object, obj);
try {
em.getTransaction().begin();
em.merge(obj);
em.getTransaction().commit();
em.close();
emf.close();
return 0;
} catch (Exception e) {
return -1;
}
}
public Object search(Object object) {
if (object == null)
throw new NullPointerException();
try {
Object obj = em.find(object.getClass(), this.getId(object));
return obj;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public List searchAll(Object object) {
if (object == null)
throw new NullPointerException();
try {
em.getTransaction().begin();
List list = em.createQuery("from " + object.getClass()).getResultList();
em.getTransaction().commit();
em.close();
emf.close();
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Long insert(Object object) {
try {
em.getTransaction().begin();
em.persist(object);
em.getTransaction().commit();
em.close();
emf.close();
return this.getId(object);
} catch (Exception e) {
e.printStackTrace();
return -1l;
}
}
public Long getId(Object object) {
if (object == null)
throw new NullPointerException();
try {
return (Long) object.getClass().getDeclaredMethod("getId").invoke(object);
} catch (Exception e) {
e.printStackTrace();
return -5l;
}
}
/**
* obj is update Object
*
* @param object
* @param obj
*/
public void updateBean(Object object, Object obj) {
try {
Method[] methods = object.getClass().getDeclaredMethods();
Method method;
Object objectMethodValue;
Object objMethodValue;
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().trim().startsWith("get")) {
if (methods[i].invoke(object) == null) {
System.out
.println("----------this field is null--------------");
continue;
}
objectMethodValue = methods[i].invoke(object);
objMethodValue = methods[i].invoke(obj);
if (objectMethodValue.equals(objMethodValue)) {
System.out
.println("----------field is equals--------------");
continue;
}
method = obj.getClass().getDeclaredMethod(
methods[i].getName().trim().replaceFirst("g", "s"),
methods[i].getReturnType());
method.invoke(obj, objectMethodValue);
System.out.println("==============="
+ methods[i].invoke(obj));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import com.vsc.study.ejb.entitybean.Function;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class PerfectEntityManager {
private EntityManagerFactory emf;
private EntityManager em;
public PerfectEntityManager() {
emf = Persistence.createEntityManagerFactory("study-ejbPU");
em = emf.createEntityManager();
}
public PerfectEntityManager(String utinName) {
emf = Persistence.createEntityManagerFactory(utinName);
em = emf.createEntityManager();
}
public Map<Integer, List> getCurrentResults(int currentPage, int pageSize,
int maxResults, String querySql) {
if (maxResults == 0) {
maxResults = em.createQuery(querySql).getResultList().size();
}
List list = em.createQuery(querySql).setFirstResult(
(currentPage - 1) * pageSize).setMaxResults(pageSize)
.getResultList();
HashMap<Integer, List> hashMap = new HashMap<Integer, List>();
hashMap.put(maxResults, list);
return hashMap;
}
public Map<Integer, List> getCurrentResults(int currentPage, int pageSize,
int maxResults, Object object) {
if (maxResults == 0) {
maxResults = em.createQuery("from " + object.getClass())
.getResultList().size();
}
List list = em.createQuery("from " + object.getClass()).setFirstResult(
(currentPage - 1) * pageSize).setMaxResults(pageSize)
.getResultList();
HashMap<Integer, List> hashMap = new HashMap<Integer, List>();
hashMap.put(maxResults, list);
return hashMap;
}
String separator = " ";
public String setEjbql(Object object, List<Ejbql> ejbqlList) {
StringBuilder sb = new StringBuilder("select o from "
+ object.getClass() + " as o where 1=1 ");
for (Ejbql ejbql : ejbqlList) {
if (ejbql == null) {
continue;
}
if (ejbql.getCloumns() == null) {
continue;
}
if (ejbql.getOperator() == null) {
continue;
}
if (ejbql.getValues() == null) {
continue;
}
sb.append(ejbql.getAndOr() + separator);
sb.append("o." + ejbql.getCloumns());
if (ejbql.getOperator().equals("like")) {
sb.append(separator + ejbql.getOperator() + separator);
sb.append("'%" + ejbql.getValues() + "%'" + separator);
} else {
sb.append(ejbql.getOperator() + separator);
if (ejbql.getValues() instanceof String) {
sb.append("'" + ejbql.getValues() + "'" + separator);
} else {
sb.append(ejbql.getValues() + separator);
}
}
}
return sb.toString();
}
public String setEjbql(String classname, List<Ejbql> ejbqlList) {
StringBuilder sb = new StringBuilder("select o from " + classname
+ " as o where 1=1 ");
for (Ejbql ejbql : ejbqlList) {
if (ejbql == null) {
continue;
}
if (ejbql.getCloumns() == null) {
continue;
}
if (ejbql.getOperator() == null) {
continue;
}
if (ejbql.getValues() == null) {
continue;
}
sb.append(ejbql.getAndOr() + separator);
sb.append("o." + ejbql.getCloumns());
if (ejbql.getOperator().equals("like")) {
sb.append(separator + ejbql.getOperator() + separator);
sb.append("'%" + ejbql.getValues() + "%'" + separator);
} else {
sb.append(ejbql.getOperator() + separator);
if (ejbql.getValues() instanceof String) {
sb.append("'" + ejbql.getValues() + "'" + separator);
} else {
sb.append(ejbql.getValues() + separator);
}
}
}
return sb.toString();
}
public int remove(Object object) {
if (object == null)
throw new NullPointerException();
try {
em.getTransaction().begin();
em.remove(this.search(object));
em.getTransaction().commit();
em.close();
emf.close();
return 0;
} catch (Exception e) {
return -2;
}
}
public int update(Object object) {
if (object == null || this.getId(object) == null)
throw new NullPointerException();
Object obj = this.search(object);
this.updateBean(object, obj);
try {
em.getTransaction().begin();
em.merge(obj);
em.getTransaction().commit();
em.close();
emf.close();
return 0;
} catch (Exception e) {
return -1;
}
}
public Object search(Object object) {
if (object == null)
throw new NullPointerException();
try {
Object obj = em.find(object.getClass(), this.getId(object));
return obj;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public List searchAll(Object object) {
if (object == null)
throw new NullPointerException();
try {
em.getTransaction().begin();
List list = em.createQuery("from " + object.getClass()).getResultList();
em.getTransaction().commit();
em.close();
emf.close();
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Long insert(Object object) {
try {
em.getTransaction().begin();
em.persist(object);
em.getTransaction().commit();
em.close();
emf.close();
return this.getId(object);
} catch (Exception e) {
e.printStackTrace();
return -1l;
}
}
public Long getId(Object object) {
if (object == null)
throw new NullPointerException();
try {
return (Long) object.getClass().getDeclaredMethod("getId").invoke(object);
} catch (Exception e) {
e.printStackTrace();
return -5l;
}
}
/**
* obj is update Object
*
* @param object
* @param obj
*/
public void updateBean(Object object, Object obj) {
try {
Method[] methods = object.getClass().getDeclaredMethods();
Method method;
Object objectMethodValue;
Object objMethodValue;
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().trim().startsWith("get")) {
if (methods[i].invoke(object) == null) {
System.out
.println("----------this field is null--------------");
continue;
}
objectMethodValue = methods[i].invoke(object);
objMethodValue = methods[i].invoke(obj);
if (objectMethodValue.equals(objMethodValue)) {
System.out
.println("----------field is equals--------------");
continue;
}
method = obj.getClass().getDeclaredMethod(
methods[i].getName().trim().replaceFirst("g", "s"),
methods[i].getReturnType());
method.invoke(obj, objectMethodValue);
System.out.println("==============="
+ methods[i].invoke(obj));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}