|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
Best PracticeHi,
I hope some can answer my question about JPA and best practice. Suppose you have a typical JPA datamodel example: Example: An "User" entity with One-to-Many relationship to "Contact" entity and "Contact" entity with Many-to-One relationship to "User" entity. On the presentation layer you have a page showing a list of user "contacts" and the user may choose to update/delete/insert a contact. My question is what "manager" you are going to use to make CRUD. 1. userManager to make CRUD? User user = userManager.findUser(1); List<Contact> contacts = user.getContacts(); Contact contact = contact.get(222); contact.setName("bla bla"); userManager.update(user); 2. contactManager to make CRUD? Contact contact = contactManager.findContactByUser(1); contact.setName("bla bla"); contactManager.update(contact); or contactManager.delete(contact); 3. A combination of userManager and contactManager to make CRUD? User user = userManager.findUser(1); List<Contact> contacts = user.getContacts(); Contact contact = contact.get(222); contact.setName("bla bla"); contactManager.update(contact); What is the rules ? Thanks in advance! -taltun |
|
|
Re: Best PracticeIn JPA you only have one EntityManager, not managers specific to each class. So I assume by "manager" you mean some application object that you are using to manage your persistence.
For adding/removing a Contact you should be adding/removing the object to/from the User's collection of contacts, so you should be finding the User for those cases. For update it is still probably better to access the contact through the user, but if the collection is large just updating the contact would be more efficient. ---
James Sutherland EclipseLink, TopLink Wiki: EclipseLink, TopLink Forums: TopLink, EclipseLink Book: Java Persistence |
|
|
Re: Best PracticeThanks a lot - James Sutherland!
By "manager" I actually mean "service" - userService/contactService the place where I put the businesslogic for CRUD. To be more clear about my understanding of your answer. I have one User entity. Normally when I make CRUD on User specific data than I use "userService" to make that. For instance.: userService.update(user) Is it correct that I as a rule should use the "userService" make CRUD on all relationships (e.g. contacts) in the User entity. For instance if you want to delete.: ContactManager contactManager = new ContactManager(); Contact contact = contactManager.findContactById(10); User user = userService.findUser(1); user.removeContact(contact); userService.updateUser(user); I mean are you going to use userManager and when are your going to use contactManager ??? --thanks.. point or rule of when it is best to make use of the "userService" to make CRUD on releationships (e.g. Contact entity) on the User entity and when it is best to make use of the "contactService" to "userService" which operates on the "User" entity wit collections of other enteties like "Contact" then it is best to make CRUD by only using "userService" instead of fetching the collection of contacts using "userService" and when insert/update/remove then to use "contactService" to do that. generally |
|
|
Re: Best PracticeIs this a really problem. Are there other solution ?
http://www.handytutorials.com/2008/06/23/jpa-onetomany-remove-problem/ Then I can't remove one contact with relationship "on-to-many" between an User and Contact using userService. hmmm? -taltu |
|
|
Re: Best PracticeYes, when you remove from a OneToMany JPA will not delete the removed object, (it could be an independent object). The Cascade remove annotation just means cascade the removal of the source object, not remove objects removed from the collection. You just need to call EntityManager.remove() on these, which should not be a major problem.
In EclipseLink there is an @PrivateOwned annotation that gives you this, Hibernate also has a similar option. JPA 2.0 plans to add an option to allow removing from a OneToMany to trigger deletion of the object from the database. ---
James Sutherland EclipseLink, TopLink Wiki: EclipseLink, TopLink Forums: TopLink, EclipseLink Book: Java Persistence |
|
|
Re: Best PracticeI will trye - Thanks !
|
| Free Forum Powered by Nabble | Forum Help |