Discussions

EJB design: Topic: Design :: How to expose API for adding new object to a li

  1. Hey, I have a swt client and a stateless facade that expose the server API to the client. Suppose that I have two objects - Company the contain list of workers I want to write a generic API to the client that create,add,update,delete objects. Some objects, like worker need to be created in a parent context. I mean that in order to create new worker we need to have the company that the worker belongs. So, I thought about the following API: Object public createObj(parentId,parentType,obj); The function will add the worker to the list of workers in company and will return the new Worker object (with the ID). There are some issues here: 1. In order to save the new worker I need to get the workers list in company than add the new worker and save the company. The problem is that in case that there are thousands of workers we will need to load all the workers for only save one worker. 2. JPA required to save the company object in order to save the worker (there are OneToMany), so the persist return Company object but i want to return to the client the new Worker that has been created. I don't want to run on all the thousands of workers in order to search this worker. How you overcome the problem that I mention above? Thank you
  2. First, you *must* have a lazy relation between the Company class and the Worker class. It will be unacceptable to retreive all workers each time you fetch a Company. Perhaps you don't need a bi-directional relation at all? Consider to only have a Worker->Company relation, but not the other way. Secondly, when your server gets a Worker to persist, it simply does that the easiest way, i.e. by a direct save (in Hibernate: HibernateSession.save() ). First, you must get your Company instance, but it will be a cheap operation if you don't cascade load related objects. The save op. will return the persisted instance, which you then pass on to the client. /Niklas