简体   繁体   English

在JSP中获取多对一休眠

[英]Fetch many to one hibernate in JSP

How can I fetch a many to one relationship in a JSP page? 如何在JSP页面中获取多对一关系? I tried 我试过了

<s:property value="group.division.name" />

but no data appeared on the JSP. 但JSP上没有数据。

The Group can belong to one Division . Group可以属于一个Division

public class Group implements java.io.Serializable {
    ..
    private Division division;
    ..

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "div_id", nullable = false)
    public Division getDivision() {
            return this.division;
    }

    public void setDivision(Division division) {
            this.division = division;
    }
}

And

public class Division implements java.io.Serializable {
    ...
    private String name;
    private Set<Group> groups = new HashSet<Group>(0);

    @Column(name = "name", nullable = false, length = 50)
    public String getName() {
            return this.name;
    }

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

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "division")
    public Set<Group> getGroups() {
            return this.groups;
    }

    public void setGroups(Set<Group> groups) {
            this.groups = groups;
    }
}

I think, its because of your FetchType.LAZY . 我认为,这是因为您的FetchType.LAZY Remove that part, it'll fetch it eagerly by default, if I'm not mistaken -- didn't touch Hibernate for a long time, since its @ManyToOne . 删除该部分,如果没有记错的话,默认情况下它会急切地获取它-因为它的@ManyToOne很久没有触摸Hibernate了。

EAGER will try to use an outer join to retrieve the associated object, while LAZY will only trigger an explicit SELECT statement when the associated object is accessed for the first time. EAGER将尝试使用外部联接来检索关联的对象,而LAZY仅在首次访问关联的对象时才触发显式的SELECT语句。 Now, here is caveat, the LAZY thing will only work, and fire an explicit SELECT to load the related entities, within the transaction. 现在,请注意, LAZY事情将只起作用,并在事务内触发显式SELECT来加载相关实体。 In your case, the transaction is already ended, it seems; 在您的情况下,交易似乎已经结束; therefore its not able to retrieve the related entity. 因此,它无法检索相关实体。

You might like to read this question here , it discussed this thing briefly, in the question and one of the answer. 您可能想在这里阅读这个问题 ,它在问题和答案之一中简要讨论了这个问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM