Java EE - JSF - Entity class @ManyToOne



首先很抱歉我的英语很差,但是我想请求一点帮助…

我有两个oracle数据库表(事件,文档),我做了两个实体类。

one:
public class BDocuments implements Serializable {
 @JoinColumn(name = "B_EVENT_ID", referencedColumnName = "ID")
    @ManyToOne
    private BEvents bEventId;
...
...
two:
public class BEvents implements Serializable {
 @Id
    @Basic(optional = false)
    @GeneratedValue(generator="EventSeq")
    @SequenceGenerator(name="EventSeq",sequenceName="B_EVENTS_SEQ", allocationSize=1)
    @Column(name = "ID")
    private Integer id;
...
...
事件表的id字段是文档表event_id字段的外键。当用户在视图上创建一个新事件时,程序将创建一个新的实例到事件表,如果事件有附加的文档,那么我将创建一个实例到文档表。当然,新的事件实例的id将是新的文档实例的event_id..

it work fine..如果我用TOAD检查表,我可以看到2个新实例。

之后,我将在视图中看到新的事件,如果我在新的对话框窗口中单击它,我应该看到事件的详细信息。像这个事件有任何附加文件…

JSF:

<p:column>
 <f:facet name="header">
<h:outputText value="Esemény ID"/>
</f:facet>
<h:commandLink value="#{item.id}" >
<p:ajax  listener="#{mainWorkingBean.showChosedEventDetails('dlgshowevent',item.id)}"  />
</h:commandLink>
</p:column>
<p:column>

有一个托管bean,它有一个showChosedEventDetails()方法,经过一些验证检查后,它调用DAO EJB的以下方法,然后通过返回的List为视图创建一个数据表。这是EJB方法:

 public List<BDocuments> showAllDocumentsOnEvents(Integer chosedEventid) {
    if (chosedEventid != null) {
        try {
            System.out.println("showAllDocumentsOnEvents: event id:" + chosedEventid);
            assert emf != null;
            EntityManager em = null;
            em = emf.createEntityManager();
            List<BDocuments> documentList = new ArrayList();
            BEvents documentDetailsInstance = (BEvents)  em.createNamedQuery("BEvents.findById").setParameter("id", chosedEventid).getSingleResult();
            //get the event instance by the id that received in the method paramter…  
            System.out.println("showAllDocumentsOnEvents NEW CHOSED INSTANCE:  " + documentDetailsInstance);
            Collection<BDocuments> allDocumentsCollection = documentDetailsInstance.getBDocumentsCollection(); //a @ManytoOne..
            System.out.println("showAllDocumentsOnEvents EVENTINSTANCE: " + documentDetailsInstance.getEventText());
            System.out.println("showAllDocumentsOnEvents COLLECTION: " + allDocumentsCollection.toString());
            System.out.println("showAllDocumentsOnEvents NEW COLLECTION SIZE..: " + allDocumentsCollection.size() + "collection: " + allDocumentsCollection.toString()); // lame debugging.. checking the values..  
            int x;
            for (x = 0; x < allDocumentsCollection.size();) {
                System.out.println("Show all: for ciklus: Iterables kiszedve:  " + Iterables.get(allDocumentsCollection, x));
                BDocuments document = Iterables.get(allDocumentsCollection, x);
                documentList.add(document);
                x++;
            } //check the collection and put the elements to the list.. .
            return documentList;
        } catch (NullPointerException e) {
            System.out.println(e.getMessage());
            return null;
        }
    } else {
        System.out.println(" showAllDocumentsOnEvents ELSE ÁG...showAllDocumentsOnEvents: event id:" + chosedEventid);
        return null;
    }
}

它也工作得很好,但集合allDocumentsCollection是空的!我不能是空的,因为我可以在数据库中看到所有应该在里面的东西。

现在真正的大问题来了……在我在netbeans中进行清洁和构建并重新部署之后,然后单击视图上的相同事件,集合allDocumentsCollection将不会为空……我真的不知道为什么这个集合在重新部署之前是空的…为什么不能在重新部署之后…如果有人能帮助我,我将非常感激。

谢谢!

奇怪的是,除了导致问题的getBDocumentsCollection映射或用于设置关系的代码之外,您显示了所有内容。但是,由于您说数据库是正确填充的,并且这是一个双向关系,因此仅填充关系的一侧是一个相当常见的错误。当你设置BDocuments。如果使用BEvents实例,还必须将BDocuments实例添加到BEvents中。BDocumentsCollection使关系与数据库保持同步。JPA不会为您维护关系,因此您要么设置双方,要么在事务提交时使用em.refresh或特定于提供程序的api从数据库不断刷新对象。因为刷新有额外的开销,所以最好总是保持两边同步。

最新更新