【发布时间】:2014-08-04 17:08:51
【问题描述】:
我有一个带有复合键的实体类,它是实体的公共静态内部类。我想解组文件并将值放入内部类的字段中。
我用@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>
我的班级:
@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);
}
}
【问题讨论】: