如何使用hibernate加载实体中的所有内部对象

时间:2014-07-29 14:03:19

标签: java hibernate

我在我的项目中使用hibernate将数据库表加载到具有其他内部关联对象的对象(实体)中。例如:

class ApplicationEntity:

 class ApplicationEntity{

    private RoutesEntity routesEntity;

    private ViewsEntity viewsEntity;

    //....... some other class body

 }

类RoutesEntity:

 class RoutesEntity{

       private UrlEntity urlEntity;

       private PermissionEntity permmisionEntity;

 }

class ViewsEntity:

  class ViewsEntity{

        private ContentEntity contentsEntity;

        private LayoutEntity layoutEntity;


  }

现在我想使用HQL加载完全对象ApplicationEntity(带有内部对象)。但是避免使用join fetch 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");

但是在懒惰模式下但是像渴望模式一样工作

1 个答案:

答案 0 :(得分:0)

首次获取财产,标记为“懒惰”'将获取它(或抛出着名的'没有会话或会话被关闭'例外)。 所以只需创建如下函数:

public void fetchAll(ApplicationEntity a) {

          a.getRoutesEntity().getUrlEntity();
          ..
          a.getViewsEntity().getLayoutEntity();

}

并从hibernate会话中调用它。