小编hro*_*hro的帖子

无法从JSF2访问延迟注释但初始化的hibernate集合

我在称为事件和艺术家的两个实体之间存在多对多关系,两者都注释为延迟加载.当我加载一个艺术家时,我会初始化它的事件,因为会话将在之后关闭

Hibernate.initialize(artist.getEvents());
Run Code Online (Sandbox Code Playgroud)

纯Java中的测试工作正常,之后我可以访问事件及其属性.

但是在显示结果的.xhtml页面中,我只能访问艺术家的属性并测试是否有任何可用事件,Artist是支持bean,getData()返回Artist,以下行仍然有效:

<h:outputText value="No events available" rendered="#{empty artist.data.events}"/>
Run Code Online (Sandbox Code Playgroud)

但是当我想使用dataTable访问事件的属性时

<h:dataTable value="#{artist.data.events}" var="event" rendered="#{not empty artist.data.events}">
  <h:column>
    <h:outputText value="#{event.title}"/>
  </h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)

我得到了followig例外:

/artist.xhtml @48,63 value="#{event.title}": The class 'org.hibernate.collection.PersistentSet' does not have the property 'title'.
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是Hibernate的初始化方法不能与JSF2一起使用,但是当我将FetchType从LAZY更改为EAGER时,我最终得到相同的结果.

Event类看起来像这样,为简洁起见,我只包含与title属性相关的部分:

@Entity()
@Table(name="Events")
@SequenceGenerator(name="events_id", sequenceName="event_seq", initialValue=1, allocationSize=1)
public class EventData implements Serializable {

    private String title;
    // other private variables

    public EventData() {}

    public EventData(String title, ...) {
        this.title = title;
        // ...
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String …
Run Code Online (Sandbox Code Playgroud)

java jsf hibernate jsf-2

2
推荐指数
1
解决办法
2949
查看次数

标签 统计

hibernate ×1

java ×1

jsf ×1

jsf-2 ×1