• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to attach entity in JAX-RS service?

 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am validating and uploading data in my webapp. In this process, I need to get user input, so the validating and uploading is split between 2 requests. In the 1st request I setup/fetch all the information and then save it in the session. This includes a Entity object that I later access in the 2nd request.

My question is how do I reattach the entity in the 2nd request so I don't get a LazyInitializationException? I have tried the following:



I am getting the error: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call, even though I have the annotated the method with Spring's @Transactional.

I am able to get this to work with the following code, however it is hibernate specific and I would rather it not.



Any ideas on how to improve this would be helpful. I also realize that we should be using HttpSessions for REST calls, but this is legacy code and that's how it is done for now.
These request method are Jersey REST service calls if that makes a difference. The jpa code lives in a commons jar file and this service is in the project that uses the commons jar.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the error: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call, even though I have the annotated the method with Spring's @Transactional.

How did you configure JPA in Spring?

You might find this link spring-hibernate-jpa useful.
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll take a look at the link. For now here is my config xml:

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just had a quick glance at you configuration.

The difference between the two configurations is in the declaration of the TransactionManager. You might want to change yours according to the one given in the link.
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried the changing the TransactionManager config as shown in the link with no luck. I still have the same exception being thrown. Here's the jersey class to provide more context.

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason why you don't inject the EntityManager like this? Then you could use em in the upload() method. More info about how the Spring @Transactional annotation works, can be found in this article.

Hope it helps!
Kind regards,
Roel
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Is there any reason why you don't inject the EntityManager like this? Then you could use em in the upload() method. More info about how the Spring @Transactional annotation works, can be found in this article.

Hope it helps!
Kind regards,
Roel


When I inject the EntityManager like that, it is null. I'll take a look at the link. Thanks
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Ewing wrote:When I inject the EntityManager like that, it is null. I'll take a look at the link. Thanks

You could try the following instead
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Rj Ewing wrote:When I inject the EntityManager like that, it is null. I'll take a look at the link. Thanks

You could try the following instead


That is essentially what I am doing, just in upload() and not the constructor. I get a lazyinitializationException if I try:

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Ewing wrote:That is essentially what I am doing, just in upload() and not the constructor.

And what happens if you create the entity manager in the constructor?
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Rj Ewing wrote:That is essentially what I am doing, just in upload() and not the constructor.

And what happens if you create the entity manager in the constructor?


I still get a LazyInitializationException
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Ewing wrote:I still get a LazyInitializationException

Does this happen on the merge() call or on the getLazilyLoadedData() one?
 
Rj Ewing
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the getLazilyLoadedData() call
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Ewing wrote:the getLazilyLoadedData() call

And I assume the lazy loaded data was not yet loaded in the entity you have added on the session. Based on this article, that seems to be the expected behavior of the merge() method. A merge() consists of:
  • retrieve the entity with the same ID as the detached one passed as argument,
  • copy the state of the detached entity to the attached one,
  • and return the attached entity.


  • So you'll probably need to do update your entity model and cascade merging using the cascade attribute of the relationship annotation e.g. @OneToOne). Something likeMore info provided in this article.
     
    Rj Ewing
    Ranch Hand
    Posts: 93
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Rj Ewing wrote:the getLazilyLoadedData() call

    And I assume the lazy loaded data was not yet loaded in the entity you have added on the session. Based on this article, that seems to be the expected behavior of the merge() method. A merge() consists of:
  • retrieve the entity with the same ID as the detached one passed as argument,
  • copy the state of the detached entity to the attached one,
  • and return the attached entity.


  • So you'll probably need to do update your entity model and cascade merging using the cascade attribute of the relationship annotation e.g. @OneToOne). Something likeMore info provided in this article.


    Thanks for this. Although it didn't solve my issue, I was able to figure out the problem. When you call manager.merge(myEntity), it returns a new entity. I didn't realize this and was calling the old entity. Anyways I've got it working now. Thanks for the help.
     
    Roel De Nijs
    Sheriff
    Posts: 10666
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rj Ewing wrote:When you call manager.merge(myEntity), it returns a new entity. I didn't realize this and was calling the old entity. Anyways I've got it working now.

    Glad to hear you were able to fix your issue and it's working now!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic