hibernate的lazy初始化结果

Posted Danial7777777

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate的lazy初始化结果相关的知识,希望对你有一定的参考价值。

package com.ehcache;

import java.io.Serializable;


public class User implements Serializable{
    private int id;
    private String name;

    public User() {
        
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }
    
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.ehcache">
    <!-- name属性:哪个类 table属性:对应哪个表,如果不写,默认的表名就是类的简单名称 -->
    <class name="User" table="tt_user" >
        <cache usage="read-write" />
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name" not-null="true" />
    </class>
</hibernate-mapping>
        @Test
        public void test2() {
            Session session = sessionFactory.openSession();
            session.beginTransaction();

            User u1 = session.load(User.class, 1);
//这边注释session关闭后打印u1会报错 no session // Hibernate.initialize(u1); session.getTransaction().commit(); session.close(); System.out.println(u1);
// return c1; }

 


以上是关于hibernate的lazy初始化结果的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate中Session之get和load方法的真正区别

Hibernate延迟加载Lazy

Hibernate延迟加载Lazy

使用 hibernate.enable_lazy_load_no_trans 解决 Hibernate Lazy-Init 问题

Hibernate延迟加载

Hibernate延迟加载Lazy