获取List对象HQL

时间:2014-09-09 12:24:42

标签: hibernate hql lazy-initialization

我有一个包含CubeBase列表的母类(SchemaBase),所以我想恢复包含SchemaBase id的CubeBase列表:

public List<CubeBase>listCube(){

    listCubeSchema.addAll(cubeService.listcubeByIdSchema(shmanged.getSchema().getId_schema()));

    return listCubeSchema;

}

我在managedBean中编写了这段代码:

public List<CubeBase>listCube(){

        listCubeSchema.addAll(cubeService.listcubeByIdSchema(shmanged.schema.getId_schema()));

        return listCubeSchema;

    }

我收到此错误:

15:27:24,159 Avertissement [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-localhost-127.0.0.1-8383-4) #{xmlMan.GenerateXml()}: 
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: esprit.olap.domain.CubeBase.namedSets, no session or session was closed:

这是Cubebase类:

public class CubeBase implements Serializable{

    /**
     * Private Attribute 
     */
    private static final long serialVersionUID = 1L;




    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id_cube;

    private String name_cube;

    private String description_cube;

    private String caption_cube;

    private boolean visible;

    private boolean cache;

    private boolean enabled;



    @ManyToOne
    private SchemaBase schema;


    @OneToMany (mappedBy="cube",cascade={CascadeType.MERGE,CascadeType.ALL})
    private List<NamedSetBase> namedSets;

    @OneToMany  (mappedBy="cube",cascade={CascadeType.MERGE,CascadeType.ALL})
    private List<MeasureBase> measures;

    @OneToMany  (mappedBy="cube",fetch = FetchType.LAZY)
    private List<DimensionBase> dimensions;

    @OneToOne(cascade=CascadeType.ALL)
    private ViewBase view;

    ....
}

这是SchemaClass:

public class SchemaBase implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id_schema;  

    private String name_schema;

    private String description_schema;

    private String measuresCaption_schema;

    private String defaultRole_schema;


    @ManyToOne(fetch=FetchType.LAZY)
    private CatalogBase catalogBase;




    @OneToMany (cascade={CascadeType.MERGE,CascadeType.ALL})
    private List<CubeBase>cubes;

    @OneToMany (cascade={CascadeType.MERGE,CascadeType.ALL}, fetch=FetchType.LAZY)
    private List<ParameterBase>parameters;

    @ManyToMany
    private List<DimensionBase> dimensions;
....}

但她对我不起作用,即使我觉得它不对,所以你可以帮助我PLZ而且我不知道为什么问题来自其他类(NamedSet)!!

4 个答案:

答案 0 :(得分:1)

见这个例子:

public List<User> getUsers() {
    Query qry = getSession().createQuery("from User u order by upper(u.username)");
    return qry.list();
}

答案 1 :(得分:0)

List<CubeBase> listCube=ent.createQuery(from CubeBase);

要研究在hibernate中编写查询,请参阅此链接http://www.tutorialspoint.com/hibernate/hibernate_query_language.htm

答案 2 :(得分:0)

这项工作对我来说:

final String sql="your hql request";
List<AAA> aaas =  getSession().createQuery(sql).list()

答案 3 :(得分:0)

您可以使用sessionFactory.getCurrentSession()获取会话,然后使用此会话创建查询。