linshouyi2010 2013-04-25 15:32
浏览 993
已采纳

hibernate 关于 注解配置@Basic(fetch=FetchType.LAZY) 不起效果

intro字段配置成@Basic(fetch=FetchType.LAZY) 

但是无论用get还是load方法,intro和其他字段都是一样加载的,和其他字段没有区别啊?配了和没配是一样的?

有没有人和我一样的问题啊,我用的是hibernate3.3

 

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.test.HibernateSessionFactory;


@Entity
//@Table(name="t_student",uniqueConstraints=@UniqueConstraint(columnNames={"name","sex"}))
@Table(name="t_student")
public class Student{
    
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Date birthday;
    private Boolean sex;
    private Integer age;
    private Double grade;
    @Basic(fetch=FetchType.LAZY)
    private String intro;
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public Date getBirthday() {
        return birthday;
    }


    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Boolean getSex() {
        return sex;
    }


    public void setSex(Boolean sex) {
        this.sex = sex;
    }


    public Integer getAge() {
        return age;
    }


    public void setAge(Integer age) {
        this.age = age;
    }


    public Double getGrade() {
        return grade;
    }


    public void setGrade(Double grade) {
        this.grade = grade;
    }

    public String getIntro() {
        return intro;
    }


    public void setIntro(String intro) {
        this.intro = intro;
    }

    public static void main(String[] args) {
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = session.beginTransaction();
        transaction.begin();
        try{
//          School school = new School();
//          school.setName("清华大学");
//          
//          school.setIntro("bca");
//          Student t = new Student();
//          t.setName("武林");
//          t.setAge(100);
//          t.setBirthday(new Date());
//          t.setSex(true);
//          t.setIntro("xx");
//          t.setGrade(59.99d);
//          t.setSchool(school);
//          session.save(t);
            
            Student t = (Student)session.load(Student.class, 1);
            String name = t.getName();
            String intro = t.getIntro();
            
            transaction.commit();
            
        }catch(Exception e){
            e.printStackTrace();
            transaction.rollback();
        }finally{
            session.close();
        }
    }
}
  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注