繁体   English   中英

Java EE-JSF-实体类@ManyToOne

[英]Java EE - JSF - Entity class @ManyToOne

首先,对不起我的转储英语,但我会问一点帮助。

我有两个Oracle Db表(事件,文档),我做了两个实体类。

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字段的外键。当用户在视图上创建新事件时,程序将为事件表创建新实例,如果事件具有附加文档,则我将为documents表创建一个实例。 当然,新事件实例的ID将是新文档实例的event_id。

它工作正常..如果我用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>

有一个具有方法showChosedEventDetails()的托管bean,经过一些验证检查后,它将调用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;
    }
}

它也可以正常工作,但是Collection allDocumentsCollection为空! 我不能为空,因为我可以在数据库中看到应该包含的所有内容。

现在来提出一个真正的大问题..在我清理干净并在netbeans中进行构建和重新部署,然后在Collection AllDocumentsCollection视图上单击同一事件后,我将不会为空…我真的不知道为什么该Collection为空在重新部署之前..为什么不在重新部署之后..

如果有人可以帮助我,我将非常感激。

谢谢!

奇怪的是,除了导致问题的getBDocumentsCollection映射或用于设置关系的代码外,您什么都不显示。 但是,由于您说的是数据库已正确填充,并且这是双向关系,因此仅填充关系的一侧是一个很常见的错误。 当使用BEvents实例设置BDocuments.bEventId时,还必须将该BDocuments实例添加到BEvents.BDocumentsCollection中,以使关系与数据库保持同步。 JPA不会为您维护关系,因此当您使用em.refresh或提供程序特定的API提交事务时,您可以设置双方,也可以从数据库中不断刷新对象。 由于刷新会产生额外的开销,因此最好始终保持双方同步。

暂无
暂无

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

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