Hibernate-常见错误

  • Mapping (RESOURCE) not found

    IDEA中,entityxml应放在resources文件夹下,并将hibernate.cfg.xml中对应的mapping-resource修改

  • No CurrentSessionContext configured

    使用getCurrentSession时出现

    • session = sessionFactory.openSession();
    • hibernate.cfg.xml中添加配置文件

<property name="hibernate.current_session_context_class">thread</property>

  • Calling method 'get' is not valid without an active transaction

    使用getCurrentSession时出现

    hibernate要求对数据库的所有操作都必须开启事务管理

    transaction = session.beginTransaction();

    try{

    操作;

    transcation.commit();

    }catch( Exception e){

    transcation.rollback();

    }

  • failed to lazily initialize a collection of role

    当使用一对多映射时,默认延迟加载

    修改加载方式:

    在映射文件的集合映射处,添加lazy属性

    可选值:

    false:不延迟加载,一次性加载所有值

    true(默认)延迟加载,在调用具体实体的时加载(不能调用size

    extra:自动,只加载需要的信息

猜你喜欢

转载自www.cnblogs.com/AlMirai/p/12526456.html
今日推荐