我正在使用 Hibernate 和 RESTeasy,我尽量避免与这些实体形成循环,因为我在 Artiste 和 Oeuvre 实体之间存在 OneToMany (ManyToOne) 双向关系:
Oeuvre.java
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@XmlRootElement(name = "oeuvre")
public abstract class Oeuvre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Embedded
private Dimension dimension;
@XmlElement(defaultValue = "true")
private boolean hasBeenReproduced;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name = "artiste_id")
@XmlIDREF
private Artiste artiste;
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// @XmlTransient
@XmlInverseReference(mappedBy = "oeuvres")
public Artiste getArtiste() {
return artiste;
}
public void setArtiste(Artiste artiste) {
this.artiste = artiste;
artiste.addOeuvre(this);
}
}
Personne.java
import javax.persistence.*;
import javax.xml.bind.annotation.XmlID;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Personne {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@XmlID
private int id;
}
Artiste.java
import java.util.*;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
@Entity
@XmlRootElement(name = "artiste")
public class Artiste extends Personne {
private String bibliographie;
@OneToMany(mappedBy = "artiste", orphanRemoval = true, cascade = {
CascadeType.PERSIST, CascadeType.REMOVE })
private List<Oeuvre> oeuvres = new ArrayList<Oeuvre>();
@XmlElement
public List<Oeuvre> getOeuvres() {
return oeuvres;
}
public void setOeuvres(List<Oeuvre> oeuvres) {
this.oeuvres = oeuvres;
}
}
所以我决定使用 MOXy,
这是我的POM
<repository>
<id>EclipseLink</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy </artifactId>
<version>2.3.2</version>
</dependency>
nb :我只想使用 org.eclipse.persistence.moxy-2.3.2.jar,因为我正在使用 hibernate(我不想要 eclipseLink),但我还有其他 3 个 jar(包括核心)
然后我将一个 jaxb.properties 文件放入我的实体包中:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
并且将 @XmlInverseReference(mappedBy="oeuvres") 添加到 getArtiste() 而不是 @XmlTranscient ==> 我不再有循环(比如 xmlTranscient),但我仍然没有任何后向指针。
然后我添加了@XmlID & @XmlIDREF,艺术家的 id 现在在艺术作品的 xml 结果中表示,但它没有好的值(0 但应该是其他值)
<Oeuvre>
<hasBeenReproduced>false</hasBeenReproduced>
<artiste>0</artiste>
<year>2010</year>
<id>2</id>
<titre>La joconde</titre>
</Oeuvre>
我做错了什么?提前致谢
编辑:
好的,当我编码“Artiste”对象时,使用@XmlInverseReference 得到以下输出:
<artiste>
<id>1</id>
<nom>a</nom>
<prenom>b</prenom>
<oeuvres>
<hasBeenReproduced>false</hasBeenReproduced>
<year>2010</year>
<id>25</id>
<titre>La joconde</titre>
</oeuvres>
</artiste>
根据您的示例,这是正确的行为。 因此,如果我理解得很好,就不可能在“Oeuvre”输出(上面给出)中引用艺人 ID。我们无法从艺术品中找回艺术家。 就我而言,我不必使用 @XmlID 吗?
感谢 Blaise Doughan 的完整回答,非常感谢
最佳答案
注意:我是 EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导和成员专家组。
下面是如何使用 MOXy 的 @XmlInverseReference 扩展的简化示例。
福
Foo 是这个例子的根对象。
package forum13957068;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
String fooProp;
@XmlElement(name="bar")
List<Bar> bars;
}
栏
Bar 是一个子对象。我们使用 @XmlInverseReference 注释通过对父对象的引用填充 foo 字段。
package forum13957068;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
private String barProp;
@XmlInverseReference(mappedBy="bars")
Foo foo;
}
jaxb.properties
要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个名为 jaxb.properties 的文件,其中包含以下条目:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
输入.xml
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<fooProp>A</fooProp>
<bar>
<barProp>B</barProp>
</bar>
<bar>
<barProp>B</barProp>
</bar>
</foo>
演示
以下演示代码将解码 XML,检查后向指针,然后将对象编码回 XML。
package forum13957068;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum13957068/input.xml");
Foo foo = (Foo) unmarshaller.unmarshal(xml);
for(Bar bar : foo.bars) {
System.out.println(bar.foo == foo);
}
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foo, System.out);
}
}
输出
下面是运行演示代码的输出。了解如何正确填充后向指针。
true
true
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<fooProp>A</fooProp>
<bar>
<barProp>B</barProp>
</bar>
<bar>
<barProp>C</barProp>
</bar>
</foo>
MOXy 和 Maven
您可以在 pom.xml 文件中使用以下内容来引入 MOXy。
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>EclipseLink Repo</id>
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
</repositories>
有关引入 MOXy 的一些示例 POM 文件,请查看我在 GitHub 上托管的示例:
更新
According to your example this is the correct behaviour. So if i understand well, it's not possible to have a reference of the artiste id in the "Oeuvre" output (given above). We can't retrieve the artist from a work of art. In my case i don't have to use @XmlID ?
下面我提供了一个替代映射,它使用 @XmlID/@XmlIDREF 而不是 @XmlInverseReference。
福
package forum13957068;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlID
String id;
String fooProp;
@XmlElement(name="bar")
List<Bar> bars;
}
栏
package forum13957068;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Bar {
private String barProp;
@XmlIDREF
Foo foo;
}
输入.xml
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<id>1</id>
<fooProp>A</fooProp>
<bar>
<foo>1</foo>
<barProp>B</barProp>
</bar>
<bar>
<foo>1</foo>
<barProp>B</barProp>
</bar>
</foo>
了解更多信息
关于xml - 使用 MOXy 避免循环,@XmlInverseReference @XmlID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957068/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He