-
15. Re: Does many to many relationship work in Jboss3.0 ?
michele Jan 29, 2002 3:59 PM (in response to nick)-----------------------------------------------------
A.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import javax.ejb.EJBLocalObject;
public interface A extends EJBLocalObject {
public Collection getB();
public void setB(Collection b);
public Integer getId();
}
-----------------------------------------------------
AHome.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
import javax.ejb.FinderException;
public interface AHome extends EJBLocalHome {
public A create(Integer id) throws CreateException;
public A findByPrimaryKey(Integer id) throws FinderException;
public Collection findAll() throws FinderException;
}
-----------------------------------------------------
ABean.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
public abstract class ABean implements EntityBean {
transient private EntityContext ctx;
public Integer ejbCreate(Integer id) {
setId(id);
return null;
}
public void ejbPostCreate(Integer id) {}
public abstract Integer getId();
public abstract void setId(Integer id);
public abstract Collection getB();
public abstract void setB(Collection b);
public void setEntityContext(EntityContext ctx) {this.ctx = ctx;}
public void unsetEntityContext() {this.ctx = null;}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbRemove() {}
}
-----------------------------------------------------
B.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import javax.ejb.EJBLocalObject;
public interface B extends EJBLocalObject {
public Collection getA();
public void setA(Collection a);
public abstract Integer getId();
}
-----------------------------------------------------
BHome.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
import javax.ejb.FinderException;
public interface BHome extends EJBLocalHome {
public B create(Integer id) throws CreateException;
public B findByPrimaryKey(Integer id) throws FinderException;
public Collection findAll() throws FinderException;
}
-----------------------------------------------------
BBean.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
public abstract class BBean implements EntityBean {
transient private EntityContext ctx;
public Integer ejbCreate(Integer id) {
setId(id);
return null;
}
public void ejbPostCreate(Integer id) {}
public abstract Integer getId();
public abstract void setId(Integer id);
public abstract Collection getA();
public abstract void setA(Collection a);
public void setEntityContext(EntityContext ctx) {this.ctx = ctx;}
public void unsetEntityContext() {this.ctx = null;}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbRemove() {}
}
-----------------------------------------------------
Controller.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import javax.ejb.EJBObject;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
// interfaccia 'remote' Controller
public interface Controller extends EJBObject
{
public void test1() throws RemoteException;
public void test2() throws RemoteException;
}
-----------------------------------------------------
ControllerHome.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface ControllerHome extends EJBHome
{
Controller create() throws RemoteException, CreateException;
}
-----------------------------------------------------
ControllerBean.java
-----------------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
public class ControllerBean implements SessionBean
{
public void test1() throws Exception {
AHome aHome = getAHome();
BHome bHome = getBHome();
A a1 = aHome.create(new Integer(1));
A a2 = aHome.create(new Integer(2));
B b1 = bHome.create(new Integer(-1));
B b2 = bHome.create(new Integer(-2));
B b3 = bHome.create(new Integer(-3));
B b4 = bHome.create(new Integer(-4));
}
public void test2() throws Exception {
AHome aHome = getAHome();
BHome bHome = getBHome();
A a1 = aHome.findByPrimaryKey(new Integer(1));
A a2 = aHome.findByPrimaryKey(new Integer(2));
B b1 = bHome.findByPrimaryKey(new Integer(-1));
B b2 = bHome.findByPrimaryKey(new Integer(-2));
B b3 = bHome.findByPrimaryKey(new Integer(-3));
B b4 = bHome.findByPrimaryKey(new Integer(-4));
System.out.println(a1.getId());
System.out.println(a2.getId());
System.out.println(b1.getId());
System.out.println(b2.getId());
System.out.println(b3.getId());
System.out.println(b4.getId());
a1.getB().add(b1);
a1.getB().add(b2);
a2.getB().add(b3);
a2.getB().add(b4);
}
private AHome getAHome() {
try {
InitialContext jndiContext = new InitialContext();
return (AHome) jndiContext.lookup("relation/manyToMany/bidirectional/A");
} catch(Exception e) {
e.printStackTrace();
fail("Exception in getAHome: " + e.getMessage());
}
return null;
}
private BHome getBHome() {
try {
InitialContext jndiContext = new InitialContext();
return (BHome) jndiContext.lookup("relation/manyToMany/bidirectional/B");
} catch(Exception e) {
e.printStackTrace();
fail("Exception in getBHome: " + e.getMessage());
}
return null;
}
public void fail(String s)
{
System.out.println(s);
}
// metodi del bean
public void setSessionContext(SessionContext sc){}
public void ejbCreate(){}
public void ejbPostCreate(){}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
} -
16. Re: Does many to many relationship work in Jboss3.0 ?
michele Jan 29, 2002 4:02 PM (in response to nick)---------------------------------------------
ABTest1
---------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import java.util.Iterator;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
public class ABTest1
{
public static void main(String[] args)
{
try
{
InitialContext jndiContext = new InitialContext();
ControllerHome controllerHome= (ControllerHome) jndiContext.lookup("ejb/Controller");
// Init
Controller c = controllerHome.create();
System.out.println("executing test1");
c.test1();
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
}
---------------------------------------------
ABTest2
---------------------------------------------
package org.jboss.test.cmp2.relationship.manyToManyBidirectional;
import java.util.Collection;
import java.util.Iterator;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
public class ABTest2 //extends EJBTestCase {
{
public static void main(String[] args)
{
try
{
InitialContext jndiContext = new InitialContext();
ControllerHome controllerHome= (ControllerHome) jndiContext.lookup("ejb/Controller");
// Inizializzazione
Controller c = controllerHome.create();
System.out.println("esecuzione test1");
c.test2();
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
} -
17. Re: Does many to many relationship work in Jboss3.0 ?
michele Jan 29, 2002 4:07 PM (in response to nick)These are the steps:
1) start JBoss3.0
2) compile and deploy the ejb.jar
3) execute ABTest1
4) restart JBoss3.0
5) execute ABTest2
then JBoss report the error.
Where I make a mistake?
Thanks
Michele