使用@ManyToOne(fetch=FetchType.LAZY),懒加载无效,这是怎么回事

wzjsun 2014-11-17 02:15:49
@Entity
@Table(name = "product")
public class Product implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;//编号

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="uid")
private User user;


这是实体类的一部分
查询数据库的代码是
public List<Product> showList(String city,String name,Integer lowAmount,Integer highAmount,Integer lowTerm,Integer highTerm,final int page,final int num) throws Exception {
//final String hql= showListHql(city,name,lowAmount,highAmount, lowTerm, highTerm);
final String hql= "from Product a";
List<Product> list = hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return session.createQuery(hql).list();
}
});
return list;
}

结果把user也给取出来了,hibernate发出了两条sql语句,先取出了product,再取出了user,我只想要product,怎么办?
...全文
9731 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 7 楼 lwphk 的回复:
多对一貌似没有懒加载!
这个是正确的,@manytoone 是在主表的一方,加载主表的时候就会把从表的数据加载出来。设置fetchtype自然无效
你们王哥 2015-07-02
  • 打赏
  • 举报
回复
引用 13 楼 oryjk 的回复:
其实这个问题很简单 ,因为product上面没有建立user的外健,那这个时候,hibernate去查询你的product,理论上懒加载是需要使用proxy的,需要proxy的情况是,知道每个product都对应了一个user对象,但是这个时候,hibernate不知道是不是每一个product都对应于一个user,那么这个时候,他就回去查询一次,确定是否有user,那么他都去查询一次了,所以这个时候懒加载也就失效了
补充一下,还可以通过加上 optional=false 这个配置去强制创建代理,那么这个时候,也是会在真正需要的时候去懒加载
你们王哥 2015-07-02
  • 打赏
  • 举报
回复
其实这个问题很简单 ,因为product上面没有建立user的外健,那这个时候,hibernate去查询你的product,理论上懒加载是需要使用proxy的,需要proxy的情况是,知道每个product都对应了一个user对象,但是这个时候,hibernate不知道是不是每一个product都对应于一个user,那么这个时候,他就回去查询一次,确定是否有user,那么他都去查询一次了,所以这个时候懒加载也就失效了
qq_27422325 2015-05-18
  • 打赏
  • 举报
回复
你试下这样@Entity @Table(name = "product") public class Product implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id;//编号 @ManyToOne(fetch=FetchType.EAGER)//修改这里 @JoinColumn(name="uid") private User user;
  • 打赏
  • 举报
回复
小哥,你得多读书啊!不能滥竽充数啊! 首先你教导我去看官方文档,可是你木有说是是看hibernate.org的还是jboss的。好吧,我知道你抄别人的那段英文是jboss上的,redhat网站也有: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/5/html/Hibernate_Annotations_Reference_Guide/entity-mapping-property.html 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. 这个是property level的啊!楼主在property level上用manytoone本来就不是推荐的用法吧(或者根本不行,谁知道啊,反正我不是java程序员)?你作为一个资深的SAP专家,应该给人家指出来啊!不能将错就错啊!本来写一个getter,setter揍完事的,现在你非要人家实现一个接口,这不是脱裤子放屁吗?再说也和你们java pojo的信仰不符吧? 好吧,还是说官方文档: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/ Example 18.9. Using dot notation in @FieldResult for specifying associations

@Entity
@SqlResultSetMapping(name="compositekey",
        entities=@EntityResult(entityClass=SpaceShip.class,
            fields = {
                    @FieldResult(name="name", column = "name"),
                    @FieldResult(name="model", column = "model"),
                    @FieldResult(name="speed", column = "speed"),
                    @FieldResult(name="captain.firstname", column = "firstn"),
                    @FieldResult(name="captain.lastname", column = "lastn"),
                    @FieldResult(name="dimensions.length", column = "length"),
                    @FieldResult(name="dimensions.width", column = "width")
                    }),
        columns = { @ColumnResult(name = "surface"),
                    @ColumnResult(name = "volume") } )

@NamedNativeQuery(name="compositekey",
    query="select name, model, speed, lname as lastn, fname as firstn, length, width, length * width as surface from SpaceShip", 
    resultSetMapping="compositekey")
} )
public class SpaceShip {
    private String name;
    private String model;
    private double speed;
    private Captain captain;
    private Dimensions dimensions;

    @Id
    public String getName() {
        return name;
    }

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

    @ManyToOne(fetch= FetchType.LAZY)
    @JoinColumns( {
            @JoinColumn(name="fname", referencedColumnName = "firstname"),
            @JoinColumn(name="lname", referencedColumnName = "lastname")
            } )
    public Captain getCaptain() {
        return captain;
    }

    public void setCaptain(Captain captain) {
        this.captain = captain;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public Dimensions getDimensions() {
        return dimensions;
    }

    public void setDimensions(Dimensions dimensions) {
        this.dimensions = dimensions;
    }
}

@Entity
@IdClass(Identity.class)
public class Captain implements Serializable {
    private String firstname;
    private String lastname;

    @Id
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @Id
    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}
你来说说文档中的例子需要实现FieldHandled接口吗?需要吗? 我书读的少,说的不对,您指正啊!
引用 6 楼 ooppookid 的回复:
[quote=引用 5 楼 micropentium6 的回复:] 小哥,我书读的少,木有用过hibernate,说错了你别打我! 楼主介个问题,@ManyToOne(fetch=FetchType.LAZY) 和 @JoinColumn(name="uid")是不是该放在User的getter上啊? [quote=引用 4 楼 ooppookid 的回复:] 看你这用法,理论上没什么问题,建议你发一下User,还有你private User user;的get set。 另外,参考一下这篇,不过我想不是你这个问题,由于你提供的信息无法判断是什么问题,所以你还是看看吧: http://blog.csdn.net/dys1990/article/details/38728175 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.
[/quote] 呵呵 ,你确实读书少,自己看看官方文档再来回复吧。[/quote]
woai7845519 2015-01-23
  • 打赏
  • 举报
回复
many-to-one 默认就是懒加载的,没必要去配置lazy
skyhitnow 2015-01-23
  • 打赏
  • 举报
回复
The EAGER strategy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that the associated entity should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch associations for which the LAZY strategy hint has been specified.
猿人林克 2015-01-22
  • 打赏
  • 举报
回复
引用 5 楼 micropentium6 的回复:
小哥,我书读的少,木有用过hibernate,说错了你别打我! 楼主介个问题,@ManyToOne(fetch=FetchType.LAZY) 和 @JoinColumn(name="uid")是不是该放在User的getter上啊? [quote=引用 4 楼 ooppookid 的回复:] 看你这用法,理论上没什么问题,建议你发一下User,还有你private User user;的get set。 另外,参考一下这篇,不过我想不是你这个问题,由于你提供的信息无法判断是什么问题,所以你还是看看吧: http://blog.csdn.net/dys1990/article/details/38728175 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.
[/quote] 呵呵 ,你确实读书少,自己看看官方文档再来回复吧。
lwphk 2015-01-22
  • 打赏
  • 举报
回复
如果是字段查询 可以使用 new Map() 封装成map对象.. 或者 new User(xxxx,xxx) user类里面必须提供此构造方法 http://blog.csdn.net/lwphk/article/details/39252241
lwphk 2015-01-22
  • 打赏
  • 举报
回复
多对一貌似没有懒加载!
  • 打赏
  • 举报
回复
小哥,我书读的少,木有用过hibernate,说错了你别打我! 楼主介个问题,@ManyToOne(fetch=FetchType.LAZY) 和 @JoinColumn(name="uid")是不是该放在User的getter上啊?
引用 4 楼 ooppookid 的回复:
看你这用法,理论上没什么问题,建议你发一下User,还有你private User user;的get set。 另外,参考一下这篇,不过我想不是你这个问题,由于你提供的信息无法判断是什么问题,所以你还是看看吧: http://blog.csdn.net/dys1990/article/details/38728175 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.
猿人林克 2015-01-21
  • 打赏
  • 举报
回复
看你这用法,理论上没什么问题,建议你发一下User,还有你private User user;的get set。 另外,参考一下这篇,不过我想不是你这个问题,由于你提供的信息无法判断是什么问题,所以你还是看看吧: http://blog.csdn.net/dys1990/article/details/38728175 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.
wangwei27932987 2015-01-16
  • 打赏
  • 举报
回复
没人能回答么
hlssdlc 2014-12-17
  • 打赏
  • 举报
回复
我也遇到了这个问题,不知道你的解决了没有
wzjsun 2014-11-17
  • 打赏
  • 举报
回复
知道的大神,麻烦告诉我,急啊

50,504

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧