Java 如何使用hibernate加载实体中的所有内部对象
在我的项目hibernate中,我使用hibernate将数据库表加载到具有其他内部关联对象的对象(实体)中。例如:Java 如何使用hibernate加载实体中的所有内部对象,java,hibernate,Java,Hibernate,在我的项目hibernate中,我使用hibernate将数据库表加载到具有其他内部关联对象的对象(实体)中。例如: application = getSession().createQuery("from ApplicationEntity appEntity" + " join fetch appEntity.routesEntity rEntity" + " join fetch appEntity.viewsE
application = getSession().createQuery("from ApplicationEntity appEntity"
+ " join fetch appEntity.routesEntity rEntity"
+ " join fetch appEntity.viewsEntity vEntity"
+ " join fetch rEntity.urlEntity uEntity"
+ " join fetch rEntity.perrmisioEntity pEntity"
etc ......... to get alot of joins
);
commit();
类应用程序性:
class ApplicationEntity{
private RoutesEntity routesEntity;
private ViewsEntity viewsEntity;
//....... some other class body
}
类路由实体:
class RoutesEntity{
private UrlEntity urlEntity;
private PermissionEntity permmisionEntity;
}
类视知觉:
class ViewsEntity{
private ContentEntity contentsEntity;
private LayoutEntity layoutEntity;
}
现在我想使用HQL完全加载对象ApplicationEntity(带有内部对象)。但避免使用连接获取i HQL,例如:
application = getSession().createQuery("from ApplicationEntity appEntity"
+ " join fetch appEntity.routesEntity rEntity"
+ " join fetch appEntity.viewsEntity vEntity"
+ " join fetch rEntity.urlEntity uEntity"
+ " join fetch rEntity.perrmisioEntity pEntity"
etc ......... to get alot of joins
);
commit();
可以使用简单的
application = getSession().createQuery("from ApplicationEntity");
但在惰性模式下,但像渴望模式一样工作第一次访问标记为“惰性”的属性将获取它(或抛出著名的“无会话或会话已关闭”异常)。 因此,只需创建如下函数:
public void fetchAll(ApplicationEntity a) {
a.getRoutesEntity().getUrlEntity();
..
a.getViewsEntity().getLayoutEntity();
}
从hibernate会话内部调用它。您的问题是什么?如何使用HQL完全加载对象应用程序属性(带有内部对象)。但是避免使用join fetch,这是hibernate中的默认行为,除非您将属性设置为显式地延迟加载。