Java – can I return hibernate entities as the return value in jaxws web service methods?

Anyone can tell me that I can return a hibernate entity as a return value in a jaxws web service method!

Indeed, I have some such entities:

@Entity
public class Parent {
...
    private Childone childoneByChildoneid;    
    @ManyToOne
    public
    @javax.persistence.JoinColumn(name="ChildOneId",referencedColumnName="Id")
    Childone getChildoneByChildoneid() {
        return childoneByChildoneid;
    }    
    public void setChildoneByChildoneid(Childone childoneByChildoneid) {
        this.childoneByChildoneid = childoneByChildoneid;
    }
...
}

@Entity
public class Childone {
...
    private Collection<Parent> parentsById;
    @OneToMany(mappedBy = "childoneByChildoneid")
    public Collection<Parent> getParentsById() {
        return parentsById;
    }
    public void setParentsById(Collection<Parent> parentsById) {
        this.parentsById = parentsById;
    }
...
}

And have such services:

@Stateless
@WebService()
public class MasterDataService {
    @EJB
    private MasterDataManager manager;
    @WebMethod
    public Parent getParent(int parentId) {
        return manager.getParent(parentId);
    }
}
@Stateless
public class MasterDataManager {
    @PersistenceContext
    EntityManager em;

    public Parent getParent(int parentId) {
        Parent parent = (Parent) em.createQuery(
            "select p from Parent p where p.id=:parentId")
            .setParameter("parentId",parentId).getSingleResult();
        return parent;
    }
}

When I call this web method from the client, I get lazyinitializationexception:(

I tested the serializable and Cloneable interfaces and overridden the cloning method, but unfortunately it didn't work. I used em.detach (parent) in the manager, but it still didn't work

Who can help me?

tnax

Solution

This is debatable Typically, you have two options:

>Return entities, but make sure they are initialized Use fetch = fetchtype Eagle tag @ * tomany or use hibernate initialize(..). The reason for the exception is that by default, all collections in the entity are not extracted from the database before the request But when you request them from the Jax WS serializer, the hibernate session is closed Technically, you can have some opensessioninviceinterceptors, but I don't think there are some jax-ws that can be used at any time. Writing one may be a problem If you don't want to transfer these collections, you can annotate them using @ xmltransient (or @ jsonignore, according to serialization technology) It makes entities a little messy, but I still prefer it to encode repetition. > Use dto (data transfer object) – transfer all data from the entity to a new object with similar structure, which will be exposed by the web service Again, you must ensure that the dto is populated when the sleep session is active

I prefer the first option because it requires less biolerplate code, but I agree that you should be very careful with entity state management when using it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>