Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Ideas about Fetching entities in 3-tier application

  Asked By: Meenachi    Date: Feb 17    Category: Java    Views: 947
  

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);

}

}

}

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Sultana Tabassum     Answered On: Feb 17

A few notes:

- EMF is used as an implementation  mechanism in some of SDO implementations, such as Tuscany. EclipseLink SDO for example doesn't use it at all.
- Probably you're using some old jars. You'll see SDO in Websphere too, but it's the old SDO classes, and latest Websphere doesn't support the SDO standard.
- don't look at EMF to find the answers. It's got nothing to do with your problem. Read more on SDO and DAS, how payloads are specifically defined with XSD and how datagrpahs and dataobjects sit on top of it.
- SDO and SCA are closer to architectural concepts than technologies. You just can't include an sdo jar in your app and have it fly! You've got to design your application  that way. So you've got a lot to do if you want to go this way!

 
Answer #2    Answered By: Hollie Hughes     Answered On: Feb 17

Our research for our architecture  has been thoroughly around Sun & IBM specifications, but I haven't seen anything about SCA. Thank you so much for the right approach you show us to go. Not even it solves our current problem; it serves us some architecture for our future plans.

 
Answer #3    Answered By: Jackson Williams     Answered On: Feb 17

Thats a hell lotta classes !
Is it worth all the hassle !?

 
Answer #4    Answered By: Ethan Evans     Answered On: Feb 17

Yes it is, because some friends in group don't understand, my problem isn't about persistence layer & ORM. We were looking for something like SCA which Ara help us to find it.

 
Answer #5    Answered By: Komal Mohammad     Answered On: Feb 17

Could you please explain how "SCA" help you directly to solve the lazy-association data graph loading by the proxy pattern inside  of DTOs (if it was your question) ?

As i know :
"Service Component architecture  (SCA) is a set of specifications which describe a model for building applications and systems using a Service-Oriented Architecture. SCA extends and complements prior approaches to implementing services, and SCA builds on open standards such as Web services."

"Service Data Objects (SDO) are designed to simplify and unify the way in which applications handle data. Using SDO, application  programmers can uniformly access and manipulate data from heterogeneous data sources, including relational databases, XML data sources, Web services, and enterprise information systems."

SCA and SDO can each be used on their own - there is no requirement to use both of them in the same application. SCA and SDO used together provide a powerful and flexible way of building applications around a service-oriented architecture and some experts believe this combination ( SCA + SDO) makes the next-generation SOAs.



DTOs can be propagated from Client-tier to Integration-tier, they are in all tiers and layers so whenever about the lazy-association data graph loading or persisting is talked, it doesn't mean has been just mentioned about the persistent layer or ORMs , obviously they can be loaded by your own service  out of the persistence-context (e.g. in the service consumer side ).

 
Answer #6    Answered By: Chau Tran     Answered On: Feb 17

as I remembered you said you are using SUN and IBM technologies, SCA is also an IBM promoted technology.
I am also eager to know what is the relation of SCA with lazy loading here, I know how you can solve your problem using SDO, but I can't understand the relation to SCA.
In addition, if I get it correct it is a "single" 3 tier application, not a application  that need to be connected to an existing legacy one, or giving service  to some unknown external applications. if this is the case SCA will be a heavily over kill solution.
you said you are using EJB3 and JPA if this is the case Seam will solve your lazy loading problem very easily, without need to change your architecture  by going from JPA to SDO or putting an SDO wrapper around your JPA

 
Answer #7    Answered By: Viheke Fischer     Answered On: Feb 17

SCA is not relevant to this problem. My emphasize was on SDO. SDO is what JavaBean spec should have been from day one, and webservice envelopes should have been from day one, and DTO should have been form day one

 
Didn't find what you were looking for? Find more on Ideas about Fetching entities in 3-tier application Or get search suggestion and latest updates.




Tagged: