2010-07-26 2 views
1

나는 다음과 같은 엔티티 구조 (A, B, C, D는 엔티티) :최대 절전 모드와 웹 서비스를 사용하여 두 부모와 함께 엔티티를 지속시키는 방법은 무엇입니까?

A-> one-to-many B, 
A-> one-to-many C, 
B-> one-to-many D, 
C-> one-to-many D. 

내가 최대 절전 모드로 엔티티 A를 계속하고 싶지만 내가 웹 서비스에 그것을 보낸다 (순환 참조는 제거). 그래서, 서버에 나는 아이들에 대해 "안다"는 부모님을받습니다. 그리고 아이들은 부모님에 대해 알지 못합니다. 나는 모든 것을 다시 연결해야합니다. 문제는 두 개의 부모와 D를 매치해야한다는 것입니다. 클라이언트의 단일 D 인스턴스는 서버에서 병합해야하는 두 인스턴스가되었고 D는 이전에 저장되지 않았으므로 고유 ID가 포함되지 않았습니다. 일치시킬 수 있습니다. 두 가지 솔루션에 대해 생각하고 있습니다.

1. Call web service twice – in first call persist Ds and then call it to persist A 
2. XmlIDRef, and XmlID annotations so I don’t have to merge Ds (jaxb will do the job for me) but in that case client will have to generate unique ids for that fields and I wanted to avoid that. 

어떻게해야합니까? 나는 올바른 길을 가고 있는가?

Btw, 나는 hibernate, cxf 및 jaxb를 사용하고 있습니다.

답변

1

두 가지 접근 방법이 합리적 : 유일한 개인 소유의 데이터가 하나의 메시지에 와이어를 통해 전송되도록 두 번

일부 사용자가 작은 덩어리로 메시지를 깨고있다

전화 웹의 serice. 개인 소유가 아닌 데이터에 대한 참조는 링크로 표시됩니다 (링크는 다른 JAX-RS 서비스에서 오브젝트를 얻는 방법을 지정합니다). 그런 다음 링크를 해결하는 XmlAdapters을 가질 수 있습니다 (아래 참조) :

import java.net.HttpURLConnection; 
import java.net.URL; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 

import org.example.product.Product; 

public class ProductAdapter extends XmlAdapter<String, Product>{ 

    private JAXBContext jaxbContext; 

    public ProductAdapter() { 
     try { 
      jaxbContext = JAXBContext.newInstance(Product.class); 
     } catch(JAXBException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    @Override 
    public String marshal(Product v) throws Exception { 
     if(null == v) { 
      return null; 
     } 
     return "http://localhost:9999/products/" + v.getId(); 
    } 

    @Override 
    public Product unmarshal(String v) throws Exception { 
     if(null == v) { 
      return null; 
     } 

     URL url = new URL(v); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 
     connection.setRequestProperty("Accept", "application/xml"); 

     Product product = (Product) jaxbContext.createUnmarshaller().unmarshal(connection.getInputStream()); 
     connection.disconnect(); 
     return product; 
    } 

} 

@ XmlID/@

XMLIDREF 모든 데이터를 하나의 호출과 B와 C의 주 참조를 보내려고하는 경우 D의 인스턴스로 변환하려면 @XmlID/@ XmlIDREF가 필요합니다. D의 인스턴스를 중첩하는 객체가 필요합니다. 이 경우에 A가 적합 할 것입니다.

순환 참조

MOXY JAXB 구현이 순환 관계를 처리하기위한 확장이 있습니다 아래는 내가이 자동화에 대한 사용자와 가진 스레드입니다. 이 작업은 @XmlInverseReference 주석을 통해 수행됩니다. 자세한 내용은 다음을 참조 : 자세한 답변

+0

감사합니다. – draganstankovic