Actually we are using EMF that has SDO inside. After searching through references about SDO and EMF, I have found nothing about what we are looking for.
To expand the subject, I should say that we are using EJB3, so there is no place for H3T in our architecture, we are also using JPA and wish not to be limited to an ORM like TopLink or Hibernate.
We know all about patterns like DTO, Service Locator or other related patterns, but we are looking for an implementation for our purpose.
Finally we have caught this solution:
// Entities
public class BookStore implements Serializable {
public int getId() {...}
public void setId(int id) {...}
public String getBookstorename() {...}
public void setBookstorename(String bookstorename) {...}
public Collection<Seller> getSellerCollection() {...}
public void setSellerCollection(Collection<Seller> sellerCollection) {...}
}
public class Seller implements Serializable {
public BookStore getBookStore() {...}
public void setBookStore(BookStore bookStore) {...}
public BigDecimal getId(){...}
public void setId(BigDecimal id) {...}
public String getSellername() {...}
public void setSellername(String sellername) {...}
}
// Session Bean Remote Interfaces
@Remote
public interface BookStoreServiceRemote {
List<BookStore> listAll();
}
@Remote
public interface SellerServiceRemote {
List<Seller> listSellers(Integer bookStoreId);
}
// Session Bean Implementations
@Stateless
public class BookStoreServiceBean implements BookStoreServiceRemote {
@PersistenceContext
EntityManager em;
public List<BookStore> listAll() {
Query query = em.createQuery("from BookStore bs");
return query.getResultList();
}
}
@Stateless
public class SellerServiceBean implements SellerServiceRemote {
@PersistenceContext
EntityManager em;
public List<Seller> listSellers(Integer bookStoreId) {
Query query = em.createQuery("from Seller s where s.bookStore.id = :bookStoreId");
query.setParameter("bookStoreId", bookStoreId);
return query.getResultList();
}
}
// Session Bean Client Proxies
public class BookStoreClientProxy implements BookStoreServiceRemote {
private static BookStoreClientProxy uniqueInstance = null;
private BookStoreClientProxy() {
}
public static BookStoreClientProxy getUniqueInstance() {
if(uniqueInstance == null) {
uniqueInstance = new BookStoreClientProxy();
}
return uniqueInstance;
}
public List<BookStore> listAll() {
try {
Context jndiContext = InitialContextFactory.getUniqueInstance().getInitialContext();
Object ref = jndiContext.lookup("BookStoreServiceBean/remote");
BookStoreServiceRemote bookStroeservice = (BookStoreServiceRemote) PortableRemoteObject.narrow(
ref, BookStoreServiceRemote.class);
return bookStroeservice.listAll();
} catch (NamingException ex) {
Logger.getLogger(BookStoreClientProxy.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
public class SellerClientProxy implements SellerServiceRemote {
private static SellerClientProxy uniqueInstance = null;
private SellerClientProxy() {
}
public static SellerClientProxy getUniqueInstance() {
if(uniqueInstance == null) {
uniqueInstance = new SellerClientProxy();
}
return uniqueInstance;
}
public List<Seller> listSellers(Integer bookStoreId) {
try {
Context jndiContext = InitialContextFactory.getUniqueInstance().getInitialContext();
Object ref = jndiContext.lookup("SellerServiceBean/remote");
SellerServiceRemote sellerService = (SellerServiceRemote) PortableRemoteObject.narrow(
ref, SellerServiceRemote.class);
return sellerService.listSellers(bookStoreId);
} catch (NamingException ex) {
Logger.getLogger(SellerClientProxy.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
// Our Javassist Class Loader
public class MyClassLoader extends ClassLoader {
private Loader javassistLoader;
public MyClassLoader(ClassLoader parent, Loader javassistLoader) {
super(parent);
this.javassistLoader = javassistLoader;
}
@Override
protected Class findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
}
@Override
protected Class loadClass(String name, boolean resolve) throws ClassFormatError, ClassNotFoundException {
try {
System.out.println(name);
changeMethod(name);
Class clazz = javassistLoader.loadClass(name);
return clazz;
} catch (ClassNotFoundException ex) {
}
return super.loadClass(name, resolve);
}
private void changeMethod(String className) {
try {
if (className.contains("entities.BookStore")) {
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.get(className);
CtMethod method = ctClass.getDeclaredMethod("getSellerCollection");
if (method != null) {
try {
String methodBody = "System.out.println(123456);\n";
methodBody = "return testclient.SellerClientProxy.getUniqueInstance().listSellers(new Integer(getId()));";
method.insertAfter(methodBody);
} catch (CannotCompileException ex) {
Logger.getLogger(MyClassLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} catch (NotFoundException ex) {
Logger.getLogger(MyClassLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Test Class
public class TestBookStore {
public static void test() {
List<BookStore> bookStores = BookStoreClientProxy.getUniqueInstance().listAll();
for (BookStore bookStore : bookStores) {
System.out.println("bookStore.getId(): " + bookStore.getId());
System.out.println("bookStore.getSellerCollection(): " + bookStore.getSellerCollection());
List<Seller> sellers = SellerClientProxy.getUniqueInstance().listSellers(bookStore.getId());
for (Seller seller : sellers) {
System.out.println("seller.getId(): " + seller.getId());
}
}
}
}
// Client Main Class
public class BookStoreClient {
public static void main(String[] args) {
try {
Loader loader = new Loader(ClassPool.getDefault());
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClassLoader cl = new MyClassLoader(classLoader,loader);
Thread.currentThread().setContextClassLoader(cl);
cl.loadClass("testclient.SellerClientProxy");
cl.loadClass("entities.BookStore");
Class clazz = cl.loadClas("testclient.TestBookStore");
Method method = clazz.getDeclaredMethod("test");
method.invoke(null, null);
} catch (IllegalAccessException ex) {
Logger.getLogger(BookStoreClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(BookStoreClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(BookStoreClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(BookStoreClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(BookStoreClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(BookStoreClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}