我有一张桌子:
table:
id
name
phone-area
phone-number
此XML
<person>
...
<phone>
<area>111</area>
<number>123-4567</number>
</phone>
</person>
和此代码:
@XmlRootElement(name="person")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "person", schema = "test")
public class UserLinkedIn {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
// ...
@XmlElement(name = "area")
@XmlElementWrapper(name="phone")
@Column(name = "phone-area")
double area;
@XmlElement(name = "number")
@XmlElementWrapper(name="phone")
@Column(name = "phone-number")
double number;
}
但是当我运行它时,出现此错误:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
@XmlElementWrapper is only allowed on a collection property but "com.myproject.user.person" is not a collection property.
this problem is related to the following location:
at @javax.xml.bind.annotation.XmlElementWrapper(namespace=##default, name=phone, required=false, nillable=false)
我认为“ wrapper”注释将处理wrapper元素以获取子值。我想念什么吗?
**我无法更改架构或xml文件。
答案 0 :(得分:0)
我找到了解决方法
我必须创建另一个类“ phone”,并将值映射到每个元素
所以在我的主班上:
@Transient
@XmlElement(name = "phone")
private Phone phone;
然后在我的新班上
@XmlRootElement(name = "phone")
static class Phone {
@XmlElement(name = "area")
@Column(name = "area")
int area;
@XmlElement(name = "number")
@Column(name = "number")
int number;
// here area = 111
// pnumber = 123-4567
}