码迷,mamicode.com
首页 > Web开发 > 详细

hibernate的lazy初始化结果

时间:2018-07-25 01:11:34      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:pre   nts   string   www.   actor   int   www   org   oid   

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初始化结果

标签:pre   nts   string   www.   actor   int   www   org   oid   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!