首页 > Web开发 > 详细

hibernate的lazy初始化结果

时间:2018-07-25 00:15:46      阅读:152      评论:0      收藏:0      [点我收藏+]
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初始化结果

原文:https://www.cnblogs.com/Danial7777777/p/9363266.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!