问题描述
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@GeneratedValueprivate 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();}}}
解决方案
To enable property level lazy fetching, your classes have to be instrumented: bytecode is added to the original one to enable such feature, please refer to the Hibernate reference documentation. If your classes are not instrumented, property level lazy loading is silently ignored.https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_Web_Server/1.0/html-single/Hibernate_Annotations_Reference_Guide/index.html需要字节码增强后才能使用https://community.jboss.org/thread/98334http://www.darkmi.com/man/SpringSide3/Hibernate.html