我堅持使用JAX-WS公開Web服務。由於JAXB在休眠關閉會話後嘗試序列化對象,因此發生問題。我在過去的兩天Google,但無法得出正確的答案。以下是其他人提供的某些解決方案。無法初始化代理 - 沒有會話在jax-ws +休眠
- 貪婪加載對象
- 使用@XmlTransient
- SOAPHandler但它沒有完成答案
下面是詳細的情況。我寫了一個模型類,它有@OneTOMany關係的數量,@ManyToOne關係和@OneToOne關係。 Web服務返回該類的對象。當我通過SOAP UI調用服務時,它給了我下面的錯誤信息。
@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;
}
}
<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>
Model類(不含getter和setter)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@Entity
@AttributeOverrides({ @AttributeOverride(name = "id", column = @Column(name = "mas_form_id")),
@AttributeOverride(name = "tenantId", column = @Column(name = "mas_form_tenant_id")) })
@FilterDef(name = "tenantFilter", parameters = @ParamDef(name = "tenantIdParam", type = "string"))
@Filters(@Filter(name = "tenantFilter", condition = "mas_form_tenant_id = :tenantIdParam"))
@Table(name = "mas_form")
public class Form extends BaseRevolution {
@Column(name = "mas_form_action")
private String formAction;
@Column(name = "mas_form_is_active")
private boolean isActive;
@Column(name = "mas_form_is_lock")
private boolean isLock;
@ManyToOne
@JoinColumn(name = "mas_ft_id")
private FormType formType;
@OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
private List<FormQuestion> formQuestions = new ArrayList<FormQuestion>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
@LazyCollection(LazyCollectionOption.FALSE)
private Set<DocumentRequired> documentRequiredSet = new HashSet<DocumentRequired>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
private List<Application> applications = new ArrayList<Application>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
@LazyCollection(LazyCollectionOption.FALSE)
private List<FormField> formFields = new ArrayList<FormField>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
private List<FormCharge> formCharges = new ArrayList<FormCharge>(0);
@Column(name = "mas_form_method")
private String formMethod;
@OneToMany(fetch = FetchType.LAZY, mappedBy = FORM)
private List<FormFieldGroup> formFieldGroup;
@Column(name = "mas_form_name_en")
private String formNameEn;
@Column(name = "mas_form_name_si")
private String formNameSi;
@Column(name = "mas_form_name_ta")
private String formNameTa;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "activity")
private ElgActivity elgActivity;
@Column(name = "description", nullable = true, length = 1000)
private String description;
//getters and setters
}
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);
}
Web服務實現幫我解決這個問題。提前致謝。
嗨卡斯滕,我需要更多這方面的細節:我們最近通過添加(EJB3)攔截器方法的會話Bean(我們使用JBoss7)克服這一點。請幫忙。我從你那裏得到的答覆是我們需要在返回Form實體之前調用Collection的getter方法。是這樣嗎 ???謝謝。 –
是的,這是關於正確的。通過調用Hibernate.initialize(Form.getAnyCollection()),您可以在離開會話/事務之前初始化集合。至少那是什麼對我有用。 – Carsten