我有以下实体结构(A、B、C、D是实体):
A-> one-to-many B,
A-> one-to-many C,
B-> one-to-many D,
C-> one-to-many D.
我想用hibernate持久化实体A,但我通过web服务发送它(消除了循环引用)。所以,在服务器上,我收到的父母“知道”孩子和孩子不知道的父母,我需要再次链接所有的东西。问题是,我需要将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.
我怎么发动汽车呢?我在正确的轨道上吗?
顺便说一句,我使用的是hibernate、cxf和jaxb。
发布于 2010-07-27 22:15:49
这两种方法都是合理的:
两次调用Web服务
一些用户正在将消息分成更小的块,以便只在单个消息中通过网络发送私有数据。对非私有数据的引用表示为链接(这些链接指定如何从另一个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下是合适的。下面是我和一个用户关于自动化这一点的帖子:
循环引用
JAXB实现具有处理循环关系的扩展。这是通过@XmlInverseReference注释完成的。有关详细信息,请参阅:
https://stackoverflow.com/questions/3339502
复制相似问题