Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OneToOne persisting two objects in a single transaction

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic