You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by karlgold <ka...@gmail.com> on 2007/11/18 04:32:21 UTC

Proxying an object by ID with CXF/Spring/JPA/Hibernate

Greetings,

I'm using CXF with Spring and JPA/Hibernate.  

I'm implementing a basic CRUD web service that has a method like this:

Car save(Car)

Say that Car has a Company property that is itself an entity with an Id
property, mapped something like this:

@Entity
@XmlType
public class Car {

  ...

  @ManyToOne(fetch = FetchType.LAZY)
  @XmlElement(required = true)
  @XmlJavaTypeAdapter(CompanyAdapter.class)
  private Company company;

  ...

}

Now, when someone calls the web service, I just want them to be able to
specify the Id of the company as a proxy for the object itself:

<Car>
  <Id>123</Id>
  <Company>456</Company>
  ...
</Car>

As you can see in the code snippet above, I tried writing an
XmlJavaTypeAdaptor to marshal a Company to an Id, and unmarshal an Id to a
Company.  Here's the problem: this results in a data access outside the
context of a JPA/Hibernate session:

SEVERE: could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no
Session
	at
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
	at
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
	at
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
	at com.convio.record.Company$$EnhancerByCGLIB$$986844ca.getId(<generated>)
	at com.convio.record.CompanyAdapter.marshal(CompanyAdapter.java:16)

This makes sense, since I'm delegating my service from CXF to a
Spring-managed bean.  Spring is wrapping my save method in a transaction for
me, and the marshalling to XML occurs after it has ended the database
session.

Any advice on the best way to handle proxied objects like this?  I guess I
need to move the transactional boundary to include unmarshalling and
marshalling, but I'm not sure how to do that with CXF.

Much obliged for any advice,

Karl

-- 
View this message in context: http://www.nabble.com/Proxying-an-object-by-ID-with-CXF-Spring-JPA-Hibernate-tf4829036.html#a13816063
Sent from the cxf-user mailing list archive at Nabble.com.