简体   繁体   English

休眠@MappedSuperclass @ManyToOne(fetch = FetchType.LAZY)

[英]Hibernate @MappedSuperclass @ManyToOne(fetch=FetchType.LAZY)

I have an abstract class AbstractDAO that all of my other DAO objects extend. 我有一个抽象类AbstractDAO,我所有其他DAO对象都进行了扩展。

@MappedSuperclass
public abstract class AbstractDAO implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="feed_id", insertable=false, updatable=false)
    @Getter @Setter private FeedDAO feed;

    @Column(name="feed_id")
    @Getter @Setter private Long feedId;
}

Then I have the InventoryDAO that extends this class: 然后,我有InventoryDAO来扩展此类:

@Entity
@Table(catalog="feed", name = "inventory")
public class InventoryDAO extends AbstractDAO {
    /**  Serial ID for this class  **/
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy="increment")
    @Column(name="id")
    @Getter @Setter private Long id;
}

And this is the FeedDAO: 这是FeedDAO:

@Entity
@Table(catalog="feed", name = "feed")
public class FeedDAO extends AbstractDAO {
    /**  Serial ID  **/
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy="increment")
    @Column(name="feed_id")
    @Getter @Setter private Long id;
}

When I query for the InventoryDAO, the feedId field is set and the feed field is null. 当我查询InventoryDAO时,将设置feedId字段,并且feed字段为null。

InventoryDAO: {"id":1,"feed":null,"feedId":10} InventoryDAO:{“ id”:1,“ feed”:null,“ feedId”:10}

However, if I query for the FeedDAO first and then for the InventoryDAO, the feed field is set. 但是,如果我先查询FeedDAO,然后再查询InventoryDAO,则将设置feed字段。

InventoryDAO: {"id":1,"feed":{"id":10},"feedId":10} InventoryDAO:{“ id”:1,“ feed”:{“ id”:10},“ feedId”:10}

Now, if I change the @ManyToOne(fetch=FetchType.LAZY) to @ManyToOne(fetch=FetchType.EAGER) and query for the InventoryDAO, the feed field is always set. 现在,如果我将@ManyToOne(fetch = FetchType.LAZY)更改为@ManyToOne(fetch = FetchType.EAGER)并查询InventoryDAO,则始终设置了提要字段。

InventoryDAO: {"id":1,"feed":{"id":10},"feedId":10} InventoryDAO:{“ id”:1,“ feed”:{“ id”:10},“ feedId”:10}

Is this a mapping issue or a limitation of the @MappedSuperclass? 这是@MappedSuperclass的映射问题还是限制?

According to the answer given here: 根据此处给出的答案

** **

It is not efficient to load all attributes when they are not needed. 不需要所有属性时,加载它们的效率不高。 So in suchlike cases, you can declare that you want entities to be loaded when they are actually needed. 因此,在这种情况下,您可以声明希望实体在实际需要时加载。 This is called lazy loading. 这称为延迟加载。

** **

So after further testing, it appears that the com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module JSON mapper ignores lazy-loaded objects unless they are already in the session. 因此,在进一步测试之后,似乎com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module JSON映射器会忽略延迟加载的对象,除非它们已在会话中。 If I call the getFeed().getId() on the InventoryDAO object, Hibernate pulls the feed object from the database and it is included in the JSON. 如果我在InventoryDAO对象上调用getFeed()。getId(),则Hibernate从数据库中提取feed对象,并且该对象包含在JSON中。 When I enable the Hibernate4Module.Feature.FORCE_LAZY_LOADING feature for the Hibernate4Module it prints the lazy loaded objects. 当我为Hibernate4Module启用Hibernate4Module.Feature.FORCE_LAZY_LOADING功能时,它将打印延迟加载的对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Kotlin进行Hibernate:@ManyToOne(fetch = FetchType.LAZY) - Hibernate with Kotlin: @ManyToOne(fetch = FetchType.LAZY) Hibernate @ManyToOne(fetch = FetchType.LAZY)被忽略 - Hibernate @ManyToOne(fetch = FetchType.LAZY) ignored FetchType.LAZY在休眠中不适用于@ManyToOne映射 - FetchType.LAZY not working for @ManyToOne mapping in hibernate Hibernate ManyToOne FetchType.LAZY无法正常工作? - Hibernate ManyToOne FetchType.LAZY is not working? Hibernate @OneToOne(fetch = FetchType.LAZY)无效 - Hibernate @OneToOne(fetch = FetchType.LAZY) is not working 尽管已设置FetchType.Lazy,但Hibernate / Jpa还是急切地获取@ManyToOne对象。 - Hibernate/Jpa fetch eagerly @ManyToOne object although FetchType.Lazy is setted 休眠:@ManyToOne(fetch = FetchType.LAZY) 对非主键引用列不起作用 - Hibernate: @ManyToOne(fetch = FetchType.LAZY) does not work on non-primary key referenced column 为什么Hibernate(JPA)对于ManyToOne关系忽略FetchType.LAZY? - Why does Hibernate (JPA) ignore FetchType.LAZY for ManyToOne relations? @ManyToOne(fetch=FetchType.LAZY, optional=false) 仍在获取 - @ManyToOne(fetch=FetchType.LAZY, optional=false) still fetching Spring Data JPA,Hibernate,@ ManyToOne(fetch = FetchType.LAZY)和org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Spring Data JPA, Hibernate, @ManyToOne(fetch=FetchType.LAZY) and org.hibernate.LazyInitializationException: could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM