如何在hibernate中加载Collection,其中fetch类型是Lazy?

时间:2014-04-15 11:33:25

标签: java hibernate lazy-loading dozer lazy-initialization

我在我的项目中使用Hibernate,Spring和Dozer。

我的问题是我无法加载Lazy抓取Collection(List)。 我正在使用Dozer将实体类转换为(Dto)类。 我认为Dozer用户知道这些事情。

当我调试然后我从实体类中的DB获取所有数据但是当我通过Dozer转换为Dto时,我在Collection中获得了Lazy Fetched。

请帮帮我。提前谢谢!

当我将实体转换为Dto时,我无法获得开发人员列表。我有确切的Dto类名DeveloperFounder,我在字符串配置文件中映射它们。我试过@Select在联接表上,然后加载并正常工作,但我不想这样。如果我必须在我的servicemanager课程中增加功能,那么请帮助我。

@Entity
@Table(name = "table")
public class Founder {

    @Id
    @Column(name = "foun_id")
    @GeneratedValue(generator = "uuid")
    private String id;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "reg_address_id")
    private Address registeredAddress;


    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "jointable", joinColumns = { @JoinColumn(name = "foun_id", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "dev_id", nullable = true) })
    private List<Developer> developer;


    public String getId() {
        return id;
    }

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

    public Address getRegisteredAddress() {
        return registeredAddress;
    }

    public void setRegisteredAddress(Address registeredAddress) {
        this.registeredAddress = registeredAddress;
    }

    public List<Developer> getDeveloper() {

        return developer;
    }

    public void setDeveloper(List<Developer> developer) {
        this.developer = developer;
    }

}

@Entity
@Table(name = "tbl")
public class Developer {

    @Id
    @Column(name = "dev_id")
    @GeneratedValue(generator = "uuid")
    private String id;

    @Column(name = "file_number")
    private String fileNumber;

    @Column(name = "flat_info")
    private String flatInfo;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional = false)
    @JoinColumn(name = "address_id")
    private Address address;

    public String getId() {
        return id;
    }

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

    public String getFileNumber() {
        return fileNumber;
    }

    public void setFileNumber(String fileNumber) {
        this.fileNumber = fileNumber;
    }

    public String getFlatInfo() {
        return flatInfo;
    }

    public void setFlatInfo(String flatInfo) {
        this.flatInfo = flatInfo;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

1 个答案:

答案 0 :(得分:1)

正如我在评论中所写,很可能是Dozer直接访问FounderDeveloper的字段而忽略了getter。由于Hibernate为延迟加载创建代理,因此转换后最终会出现空集合。考虑更改类以将映射信息添加到getter而不是字段。

这个问题可能对您有用 - Is it possible to configure Dozer such that by default fields are rather accessed directly that through setter-/getter method