我有一个带有复合键的实体类,它是实体的公共(public)静态内部类。我想解码一个文件并将值放入内部类的字段中。
我用@XmlKey、@XmlPath、@XmlJoinNode尝试了一些方法,但没有任何效果,我什至不确定我是否走在正确的道路上。
我的 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<numbers>
<one>one_text</one>
<two>two_text</two>
<three>three_text</three>
<four>four_text</four>
</numbers>
我的类(class):
@XmlRootElement(name = "numbers")
public class Numbers {
public static class Id {
@XmlElement
private String one;
@XmlElement
private String two;
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
}
@XmlElement
private String three;
@XmlElement
private String four;
private Id id = new Id();
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
public String getFour() {
return four;
}
public void setFour(String four) {
this.four = four;
}
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
}
不用担心缺少 JPA 注释。感谢您的回答。
更新
精度:XML 文件无法修改,如果没有办法保存这样的内容,则 java 类可能会修改,但它必须可以被 hibernate 解释为具有复合主键的实体。
使用 MOXy 的解决方案
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Numbers {
@Embeddable
public static class Id implements Serializable {
private static final long serialVersionUID = -2153062768685935342L;
@Column
@XmlElement
private String one;
@Column
@XmlElement
private String two;
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((one == null) ? 0 : one.hashCode());
result = prime * result + ((two == null) ? 0 : two.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Id other = (Id) obj;
if (one == null) {
if (other.one != null)
return false;
} else if (!one.equals(other.one))
return false;
if (two == null) {
if (other.two != null)
return false;
} else if (!two.equals(other.two))
return false;
return true;
}
}
@Column
@XmlElement
private String three;
@Column
@XmlElement
private String four;
@EmbeddedId
@XmlPath(".")
private Id id = new Id();
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
public String getFour() {
return four;
}
public void setFour(String four) {
this.four = four;
}
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
}
MOXy 的替代品
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
public class Numbers {
@Embeddable
public static class Id implements Serializable {
private static final long serialVersionUID = -2153062768685935342L;
@Column
private String one;
@Column
private String two;
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((one == null) ? 0 : one.hashCode());
result = prime * result + ((two == null) ? 0 : two.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Id other = (Id) obj;
if (one == null) {
if (other.one != null)
return false;
} else if (!one.equals(other.one))
return false;
if (two == null) {
if (other.two != null)
return false;
} else if (!two.equals(other.two))
return false;
return true;
}
}
@Column
@XmlElement
private String three;
@Column
@XmlElement
private String four;
@EmbeddedId
@XmlTransient
private Id id = new Id();
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
public String getFour() {
return four;
}
public void setFour(String four) {
this.four = four;
}
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
@XmlElement
public String getOne() {
return id.getOne();
}
public void setOne(String one) {
id.setOne(one);
}
@XmlElement
public String getTwo() {
return id.getTwo();
}
public void setTwo(String two) {
id.setTwo(two);
}
}
最佳答案
由于您的问题被标记为 [moxy],您可以使用 MOXy 的 @XmlPath
扩展,如下所示以获得所需的行为。
@XmlPath(".")
private Id id = new Id();
关于java - 解码到内部静态类字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25123980/