gpt4 book ai didi

java - JAXB - 如何以两种方式解析元素

转载 作者:行者123 更新时间:2023-11-30 04:01:56 25 4
gpt4 key购买 nike

我正在尝试解析一个具有与其他类的多个双向连接的类。为了防止无限循环问题和其他原因,我想以自定义方式解析一些属性。

即我有类 MovieImpl (实现电影接口(interface)),其中包含 Actor 列表 (PersonImpl) 和导演的一个字段 (PersonImpl)(当然还有其他字段):

@XmlRootElement
@XmlSeeAlso({ PersonImpl.class })
@Entity
public class MovieImpl implements Movie {

@Id
@Column(name = "movieId", unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
int _id;

...

@ManyToOne(fetch = FetchType.LAZY, targetEntity = PersonImpl.class)
@JoinColumn(name = "directorId")
Person _director;

...

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = PersonImpl.class)
@JoinTable(name = "movie_has_actors", joinColumns = { @JoinColumn(name = "movieId", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "personId", nullable = false) })
Collection<Person> _actors;

...
}

我的 PersonImpl 类有一个电影列表:

@XmlRootElement
@XmlSeeAlso({ MovieImpl.class })
@Entity
public class PersonImpl implements Person {

...
private int _id;
private String _name;

@ManyToMany(mappedBy = "_actors", targetEntity = MovieImpl.class)
private Collection<Movie> _movies;

...

}

那么,jaxb 有没有一种方法可以在从电影对象渲染 Actor 时仅显示 Actor 的姓名和 id。另一方面:当电影从人物对象渲染时,只显示电影中的一些其他字段?

即:

<movie>
<id>82303482</id>
...
<director>
<id>340980</id>
<name>xyz</name>
</director>
<actors>
<actor>
<id>479834</id>
<name>xyz</name>
</actor>
<actor>
<id>3434234</id>
<name>abc</name>
</actor>
...
</movie>


<actor>
<id>34843</id>
<name>abc</name>
...
<movies>
<movie>
<id>3489</id>
<title>asdf</title>
...
</movie>
<movie>
<id>4534534</id>
<title>qwer</title>
...
</movie>
</movies>
</actor>

感谢您提前提供帮助。

更新:当我将 @XmlInverseReference 注释添加到字段时,它只给我类似的内容:

...
<_actors>
com.company.package...PersonImpl@2450c3
</_actors>
<_actors>
com.company.package...PersonImpl@4e98166
</_actors>
<actors>
com.company.package...PersonImpl@2450c3
</actors>
<actors>
com.company.package...PersonImpl@4e98166
</actors>
...
  1. 为什么有 2 个来自 actor 的字段(_actor 和 actor)?
  2. 为什么 Actor 的字段没有显示?

最佳答案

由于根据您的问题标签,您正在使用 MOXy:

MOXy 允许通过 @XmlInverseReference 扩展进行双向映射(您需要使用 EclipseLink 2.5.0 或更高版本才能在两个方向上写入关系):

MOXy 还允许您使用对象图扩展来编码数据子集:

<小时/>

注意

下面是答案应该是什么,但是 @XmlElementWrapper@XmlInverseReference 似乎存在编码错误。

Java 模型

MovieImpl

import java.util.Collection;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlRootElement(name="movie")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlNamedObjectGraph(
name="partial",
attributeNodes={
@XmlNamedAttributeNode("_id"),
@XmlNamedAttributeNode(value="_director", subgraph="partial"),
@XmlNamedAttributeNode(value="_actors", subgraph="partial")
},
subgraphs={
@XmlNamedSubgraph(
name="location",
attributeNodes = {
@XmlNamedAttributeNode("_id"),
@XmlNamedAttributeNode("_name")
}
)
}
)
@Entity
public class MovieImpl implements Movie {

@XmlElement(name="id")
@Id
@Column(name = "movieId", unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
int _id;

@XmlElement(name="director", type=PersonImpl.class)
@ManyToOne(fetch = FetchType.LAZY, targetEntity = PersonImpl.class)
@JoinColumn(name = "directorId")
Person _director;

@XmlElementWrapper(name="actors")
@XmlElement(name="actor", type=PersonImpl.class)
@XmlInverseReference(mappedBy="_actors")
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = PersonImpl.class)
@JoinTable(name = "movie_has_actors", joinColumns = { @JoinColumn(name = "movieId", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "personId", nullable = false) })
Collection<Person> _actors;

public Collection<Person> getActors() {
return _actors;
}

}

PersonImpl

import java.util.Collection;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlRootElement(name="actor")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlNamedObjectGraph(
name="partial",
attributeNodes={
@XmlNamedAttributeNode("_id"),
@XmlNamedAttributeNode("_name"),
@XmlNamedAttributeNode(value="_movies", subgraph="partial")
},
subgraphs={
@XmlNamedSubgraph(
name="location",
attributeNodes = {
@XmlNamedAttributeNode("_id"),
@XmlNamedAttributeNode("_name")
}
)
}
)
@Entity
public class PersonImpl implements Person {

@XmlElement(name="id")
private int _id;

@XmlElement(name="name")
private String _name;

@XmlElementWrapper(name="movies")
@XmlElement(name="movie", type=MovieImpl.class)
@XmlInverseReference(mappedBy="_actors")
@ManyToMany(mappedBy = "_actors", targetEntity = MovieImpl.class)
private Collection<Movie> _movies;

}

演示代码

演示

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MovieImpl.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum21764131/input.xml");
Movie movie = (Movie) unmarshaller.unmarshal(xml);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "partial");
marshaller.marshal(movie.getActors().toArray()[0], System.out);
}

}

关于java - JAXB - 如何以两种方式解析元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com