users@glassfish.java.net

ManyToMany - How to set Relationship from RemoteClient?

From: <glassfish_at_javadesktop.org>
Date: Fri, 05 Oct 2007 08:47:03 PDT

Hi!

I have 2 JPA-POJOs/Entities, with a ManyToMany Relationship...

public class Schueler {
...
@ManyToMany(mappedBy = "schueleridCollection")
    private Collection<Klasse> klasseidCollection;
...
}

private class Klasse {
...
 @JoinTable(name = "SCHULERINKLASSE", joinColumns = {_at_JoinColumn(name = "KLASSEID", referencedColumnName = "ID")}, inverseJoinColumns = {_at_JoinColumn(name = "SCHUELERID", referencedColumnName = "ID")})
    @ManyToMany
    private Collection<Schueler> schueleridCollection;
...
}

I automatically created a SessionFacade for the entities with Netbeans - This is the way I access the entities from a remote (Java SE Client)

Now I'm having trouble as on how to establish a relationship between the instaces of these entities...

I tried this:

Collection c = s.getKlasseidCollection();
            if (c == null)
                c = new Vector();
            c.add(cmbKlasse.getSelectedItem());
            s.setKlasseidCollection(c);
            SchuelerFacadeRemote sfr = (SchuelerFacadeRemote) ctx.lookup("SchuelerFacadeRemote");
            KlasseFacadeRemote kfr = (KlasseFacadeRemote) ctx.lookup("KlasseFacadeRemote");
            Klasse k = kfr.find(((Klasse)cmbKlasse.getSelectedItem()).getId());
            
            Collection c2 = k.getSchueleridCollection();
            if (c2 == null)
                c2 = new Vector();
            c2.add(s);
            
            sfr.create(s);
            kfr.edit(k);

however I get this exception when trying to run the code:

Exception in thread "AWT-EventQueue-0" Local Exception Stack:
Exception [TOPLINK-7242] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization.
        at oracle.toplink.essentials.exceptions.ValidationException.instantiatingValueholderWithNullSession(ValidationException.java:887)
        at oracle.toplink.essentials.internal.indirection.UnitOfWorkValueHolder.instantiate(UnitOfWorkValueHolder.java:233)
        at oracle.toplink.essentials.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:105)
        at oracle.toplink.essentials.indirection.IndirectList.buildDelegate(IndirectList.java:208)
        at oracle.toplink.essentials.indirection.IndirectList.getDelegate(IndirectList.java:330)
        at oracle.toplink.essentials.indirection.IndirectList.add(IndirectList.java:155)
        at adatmp.exacom.swng.FrmSchueler.btnAnlegenActionPerformed(FrmSchueler.java:259)
        at adatmp.exacom.swng.FrmSchueler.access$200(FrmSchueler.java:29)
        at adatmp.exacom.swng.FrmSchueler$3.actionPerformed(FrmSchueler.java:142)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6038)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5803)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4410)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2429)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


I understand the basic problem, which is that due to the LAZY fetching, the collections will always be emtpy, but I don't know how to solve it...
[Message sent by forum member 'vajav' (vajav)]

http://forums.java.net/jive/thread.jspa?messageID=238678