File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes OneToOne persisting two objects in a single transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToOne persisting two objects in a single transaction" Watch "OneToOne persisting two objects in a single transaction" New topic
Author

OneToOne persisting two objects in a single transaction

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
On a facade bean I am trying to persist two objects relationed between eachother as shown belowe


In a facade bean I try to persist them in a single transaction, I'd like to have suggestions on my approach. Is there a better way to do it?


Thanks in advance for your suggestions
[ April 18, 2007: Message edited by: Alessandro Ilardo ]

trying to decode a woman mind....
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

How about



The cascade option of All in the User mapping should automatically insert the UserRole too when persisting the User, that is what Cascade options are all about.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

"On a facade bean"

Why that just flew right by me the first time I read your post.

Wouldn't you want that kind of method in a DAO object? And not tie EJBs or your facade bean to JPA?

Mark
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
thanks for your reply Mark,
As the user must have one relation with a row in the USR_ROLE, I created a constructore like:

from the client I run


and the EJB

em.persist(user);
and it partially works.

But I'm getting different values from the Primary Keys. I expected to find the same key in both records.

About the DAO.
I used that pattern when I had to write SQL querys, since I've started with EJB3 I'm using a stateless bean to access entity's.
Wrong approach?
Thanks again.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Each table has their own primary key, they wouldn't necessarily have the same ID, you can map so that the two records share the same PK, but I don't remember of the top of my head the mapping to do that.

I still like the model of seperating EJB business logic from Data Access logic. I also still like the idea of even not putting code directly in Beans, but other POJOs, but there is also a way to do that with having your Bean extend the POJO. But having the business logic in the Bean isn't wrong anymore. Since that POJO with the EJB annotations can still work outside the container.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OneToOne persisting two objects in a single transaction