Java 解组到内部静态类字段
我有一个带有复合键的实体类,它是实体的公共静态内部类。我想解组一个文件,并将值放入内部类的字段中 我用@XmlKey、@XmlPath、@XmlJoinNode做了几次尝试,但都没有成功,我甚至不确定自己是否走对了路 我的xml文件:Java 解组到内部静态类字段,java,jaxb,moxy,Java,Jaxb,Moxy,我有一个带有复合键的实体类,它是实体的公共静态内部类。我想解组一个文件,并将值放入内部类的字段中 我用@XmlKey、@XmlPath、@XmlJoinNode做了几次尝试,但都没有成功,我甚至不确定自己是否走对了路 我的xml文件: <?xml version="1.0" encoding="UTF-8"?> <numbers> <one>one_text</one> <two>two_text</two>
<?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>
不要担心缺少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;
}
}
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的替代方案
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;
}
}
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);
}
}
可以使用您的类编号(取消)封送的XML如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<numbers>
<three>tres</three>
<four>quattro</four>
<id>
<one>uno</one>
<two>due</two>
</id>
</numbers>
达到
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<numbers>
<four>quattro</four>
<one>uno</one>
<three>tres</three>
<two>due</two>
</numbers>
或者您可以委托:
@XmlRootElement(name = "numbers")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Numbers {
public static class Id {
private String one;
private String two;
// getters, setters
}
private String three;
private String four;
private Id id = new Id();
@XmlElement
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
@XmlElement
public String getFour() {
return four;
}
public void setFour(String four) {
this.four = four;
}
@XmlTransient
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();
不幸的是,xml文件结构必须保持与POST相同。如果可能的话,我想保留类结构,但不是强制性的。那么,忘掉内部类,创建一个和两个数字域。-将此添加到我的答案中。我的类是一个具有复合PK的实体。它的结构不能是这样的。可能可以将一个适配器从没有内部类的Number写到有内部类的NumberX。但只有当你真的,真的需要它时…代理版本似乎不起作用。编辑:我的意思是一/两个字段仍然为空。是的,我在这个项目上使用MOXy,你的解决方案正是我想要的。
@XmlPath(".")
private Id id = new Id();