Hibernate 无法初始化代理-jax ws+;冬眠

Hibernate 无法初始化代理-jax ws+;冬眠,hibernate,jaxb,jax-ws,Hibernate,Jaxb,Jax Ws,我坚持使用JAX-WS公开web服务。出现问题的原因是JAXB在hibernate关闭会话后尝试序列化对象。在过去的两天里,我在谷歌上搜索了一下,但没有找到正确的答案。以下是其他人给出的一些解决方案 即时加载对象 使用@xmltransive SOAPHandler,但它不是完整的答案 下面是详细的场景。我写了一个模型类,它有@OneTOMany关系、@manytone关系和@OneToOne关系。Web服务返回该类的对象作为结果。当我通过soapui调用服务时,它会给我以下错误消息 <

我坚持使用JAX-WS公开web服务。出现问题的原因是JAXB在hibernate关闭会话后尝试序列化对象。在过去的两天里,我在谷歌上搜索了一下,但没有找到正确的答案。以下是其他人给出的一些解决方案

  • 即时加载对象
  • 使用@xmltransive
  • SOAPHandler,但它不是完整的答案
下面是详细的场景。我写了一个模型类,它有@OneTOMany关系、@manytone关系和@OneToOne关系。Web服务返回该类的对象作为结果。当我通过soapui调用服务时,它会给我以下错误消息

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>could not initialize proxy - no Session</faultstring>
      </S:Fault>
   </S:Body>
</S:Envelope> 

S:服务器
无法初始化代理-没有会话
模型类(不带getter和setter)

@xmlacessortype(xmlacesstype.FIELD)
@XmlRootElement
@实体
@AttributeOverrides({@AttributeOverride(name=“id”,column=@column(name=“mas\u form\u id”),
@AttributeOverride(name=“tenantId”,column=@column(name=“mas\u form\u tenant\u id”))}
@FilterDef(name=“tenantFilter”,parameters=@ParamDef(name=“tenantipparam”,type=“string”))
@过滤器(@Filter(name=“tenantFilter”,condition=“mas\u form\u tenant\u id=:tenantipparam”))
@表(name=“mas_表”)
公共阶级形式是一场革命{
@列(name=“mas\u表单\u动作”)
私有字符串形成;
@列(name=“mas\u表单处于活动状态”)
私有布尔非活动;
@列(name=“mas\u form\u is\u lock”)
私有布尔isLock;
@许多酮
@JoinColumn(name=“mas\u ft\u id”)
私有FormType-FormType;
@OneToMany(fetch=FetchType.LAZY,mappedBy=FORM)
private List formQuestions=new ArrayList(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy=FORM)
@LazyCollection(LazyCollectionOption.FALSE)
私有集documentRequiredSet=新哈希集(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy=FORM)
私有列表应用程序=新的ArrayList(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy=FORM)
@LazyCollection(LazyCollectionOption.FALSE)
private List formFields=new ArrayList(0);
@OneToMany(fetch=FetchType.LAZY,mappedBy=FORM)
private List formCharges=new ArrayList(0);
@列(name=“mas\u表单\u方法”)
私有字符串格式方法;
@OneToMany(fetch=FetchType.LAZY,mappedBy=FORM)
私有列表formFieldGroup;
@列(name=“mas\u form\u name\u en”)
私有字符串formNameEn;
@列(name=“mas\u form\u name\u si”)
私有字符串formNameSi;
@列(name=“mas\u form\u name\u ta”)
私有字符串formNameTa;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name=“活动”)
私人活动;
@列(name=“description”,nullable=true,长度=1000)
私有字符串描述;
//接球手和接球手
}
Web服务接口

@WebService(name = "formWebServicePort", targetNamespace = "http://lk.gov.elg/core/ws/form/")
public interface FormWebService {

    @WebMethod(operationName = "getAllFormsWithDocumentReferences", action = "urn:GetAllFormsWithDocumentReferences")
    @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
    @WebResult(name = "formsWithDocumentReferences")
    List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName);
}
@WebService(name=“formWebServicePort”,targetNamespace=”http://lk.gov.elg/core/ws/form/")
公共接口FormWebService{
@WebMethod(operationName=“getAllFormsWithDocumentReferences”,action=“urn:getAllFormsWithDocumentReferences”)
@SOAPBinding(parameterStyle=SOAPBinding.parameterStyle.WRAPPED)
@WebResult(name=“formsWithDocumentReferences”)
列出getAllFormsWithDocumentReferences(
@WebParam(name=“tenantId”,targetNamespace=”http://lk.gov.elg/core/ws/form/)字符串,
@WebParam(name=“categoryName”,targetNamespace=”http://lk.gov.elg/core/ws/form/)字符串类别名称);
}
Web服务实现

@SchemaValidation
@WebService(targetNamespace = "http://lk.gov.elg/core/ws/form/", endpointInterface = "lk.gov.elg.core.ws.citizen.FormWebService")
public class FormWebServiceImpl extends SpringBeanAutowiringSupport implements FormWebService {

    private static final Logger logger = LoggerFactory.getLogger(FormWebServiceImpl.class);
    @Autowired
    private FormService formService;

    @Override
    public List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName) {

        List<Form> formList = formService.getAllFormsWithDocRefs(tenantId, categoryName);
        return formList;
    }
}
@SchemaValidation
@WebService(targetNamespace=”http://lk.gov.elg/core/ws/form/“,endpointInterface=“lk.gov.elg.core.ws.citizen.FormWebService”)
公共类FormWebServiceImpl扩展了SpringBeanAutowiringSupport实现FormWebService{
私有静态最终记录器Logger=LoggerFactory.getLogger(FormWebServiceImpl.class);
@自动连线
私人表格服务;
@凌驾
公共列表getAllFormsWithDocumentReferences(
@WebParam(name=“tenantId”,targetNamespace=”http://lk.gov.elg/core/ws/form/)字符串,
@WebParam(name=“categoryName”,targetNamespace=”http://lk.gov.elg/core/ws/form/)字符串类别名称){
List formList=formService.getAllFormsWithDocRefs(tenantId,categoryName);
返回表单列表;
}
}

帮我克服这个问题。提前感谢。

在退出会话之前,您是否尝试过初始化表单实体的集合。这将导致加载集合。因此,基本上,它做了同样的渴望加载应该做的,但不知何故,渴望加载并不总是有帮助的。如果需要的话,我可以在以后或明天挖掘更多细节。

只是一个想法:我们最近通过向sessionbean添加一个(EJB3)拦截器方法克服了这个问题(我们使用JBoss7):


嗨,卡斯滕,我需要更多的细节。请帮忙。我从您的回复中得到的是,在返回表单实体之前,我们需要调用集合的getter方法。是这样吗???谢谢,没错。您可以在离开会话/事务之前通过调用Hibernate.initialize(Form.getAnyCollection())初始化集合;至少这对我起了作用。
@SchemaValidation
@WebService(targetNamespace = "http://lk.gov.elg/core/ws/form/", endpointInterface = "lk.gov.elg.core.ws.citizen.FormWebService")
public class FormWebServiceImpl extends SpringBeanAutowiringSupport implements FormWebService {

    private static final Logger logger = LoggerFactory.getLogger(FormWebServiceImpl.class);
    @Autowired
    private FormService formService;

    @Override
    public List<Form> getAllFormsWithDocumentReferences(
            @WebParam(name = "tenantId", targetNamespace = "http://lk.gov.elg/core/ws/form/") String tenantId,
            @WebParam(name = "categoryName", targetNamespace = "http://lk.gov.elg/core/ws/form/") String categoryName) {

        List<Form> formList = formService.getAllFormsWithDocRefs(tenantId, categoryName);
        return formList;
    }
}
@javax.interceptor.AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
        Object result = ctx.proceed();

        // Add code here to inspect the result and initialize all collections.

        return result;
}