为此,我找到了两个修复程序。最简单的一个是进行交易。如果您在业务方法中启动事务,则可以在该方法的生命周期内的任何时间懒惰地初始化这些事务。如果您的事务是容器管理的,那么使用该方法的简单操作就足以完成此操作。另一种方法是使用它也会获取你的对象,但也需要一个事务。@TransactionAttribute(TransactionAttributeType.REQUIRED)
Hibernate.initialize(object.getDesiredColletion())
我的最后一个解决方案是,如果你没有交易。这个泛型方法基本上将获取你的接口,并使用 setter 方法在父对象中设置它们。您可以通过不传入id并通常获取它来改进此过程,如果您不关心更改java上的安全设置,则可以直接将集合设置为父对象(即使它是私有的),在这种情况下,可以大大减少此代码的大部分。
public Object fetchCollections(Object parent, Long id, Class<?>... childs) {
logger.debug("Need to fetch " + (childs.length) + " collections");
String fieldName = "";
String query = "";
for (int i = 0; i < childs.length; i++) {
logger.debug("Fetching colletion " + (i + 1) + " of "
+ (childs.length));
logger.debug("Collection type is " + childs[i].getSimpleName());
fieldName = findFieldName(childs[i], parent.getClass());
if (fieldName == null) {
logger.debug("Trying to search with parent class");
logger.debug(parent.getClass().getSuperclass());
fieldName = findFieldName(childs[i], parent.getClass()
.getSuperclass());
}
logger.debug("Creating query");
query = "from " + childs[i].getSimpleName() + " obj " + "where "
+ " obj." + fieldName + ".id=" + id;
logger.debug("Query= " + query);
Set collection = new HashSet(em.createQuery(query).getResultList());
setCollection(parent, collection, fieldName, childs[i]);
}
return parent;
}
private String findFieldName(Class parentClass, Class childClass) {
String fieldName = null;
boolean isCollection = false;
logger.debug("Searching for field of type "
+ childClass.getSimpleName());
for (Field f : parentClass.getDeclaredFields()) {
String type = f.getGenericType().toString();
if (f.getType().isInterface()
&& f.getGenericType().toString().contains("java.util.Set")) {
logger.debug("This field is a collection");
isCollection = true;
type = type.substring(type.indexOf("<") + 1);
type = type.substring(0, type.length() - 1);
}
if (isCollection) {
logger.debug("Field= " + f.getName() + " "
+ f.getGenericType());
if (type.equals(childClass.getName())) {
logger.debug("*****MATCH FOUND");
fieldName = f.getName();
break;
}
} else {
logger.debug("Type=" + f.getType().getName() + " childType="
+ childClass.getName());
if (f.getType().getName().equals(childClass.getName())) {
logger.debug("*****MATCH FOUND");
fieldName = f.getName();
break;
}
}
}
return fieldName;
}
private void setCollection(Object result, Set collection, String fieldName,
Class childClass) {
String methodName = "set" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
logger.debug("trivial setter is :" + methodName);
Class<?>[] args = new Class<?>[] { java.util.Set.class };
// try the trivial case
boolean methodFound = false;
Method method = null;
try {
method = result.getClass().getMethod(methodName, args);
methodFound = true;
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
logger.debug("Method not found by trivial method");
}
if (!methodFound) {
FindMethod: for (Method m : result.getClass().getMethods()) {
// logger.debug(m.getName());
for (Type t : m.getGenericParameterTypes()) {
// logger.debug("\t"+t);
String type = t.toString();
type = type.substring(type.indexOf("<") + 1);
type = type.substring(0, type.length() - 1);
if (type.equals(childClass.getName())) {
logger.debug("***Found the Setter Method");
method = m;
break FindMethod;
}
}// end for parameter Types
}// end for Methods
}// end if
invokeMethod(method, result, false, collection);
}
private void invokeMethod(Method method, Object obj, boolean initialize,
Object... args) {
try {
if (method != null) {
if (initialize)
Hibernate.initialize(method.invoke(obj, args));
else
method.invoke(obj, args);
}
logger.debug("Method executed successfully");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}