Я использую EclipseLink JPA и JAXB (MOXy) для преобразования JPA-объектов в XML. Для нормальной операции «один-ко-многим» система работает нормально, но если это отношение двунаправлено, а одно из объектов имеет составной идентификатор, использует класс java.util.Map, система генерирует исключение.EclipseLink JAXB (MOXy) с отношениями «один ко многим» и java.util.Map
Отношения:
Таблица1:
Fields: id, col1.
Primary Key: id
Table2:
Fields: id, table1_id, col1
Primary Key: (id, table1_id)
Мои классы:
Таблица 1 Класс:
@Entity
@Table(name = "table1")
@XmlRootElement
public class Table1 implements Serializable {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "table1")
@MapKey(name = "table2PK")
@XmlElement
@XmlInverseReference(mappedBy="table1")
private Map<Table2PK, Table2> table2;
@XmlElementWrapper(name="table2s")
public Map<Table2PK, Table2> getTable2() {
return table2;
}
// Gettters and setters methods
}
Класс Table2:
@Entity
@Table(name = "table2")
@XmlRootElement
public class Table2 implements Serializable {
@EmbeddedId
protected Table2PK table2PK;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "table1_id", insertable = false, unique = false, nullable = false, updatable = false)
private Table1 table1;
// Gettters and setters methods
}
Класс Table2PK:
@Embeddable
public class Table2PK implements Serializable {
@Basic(optional = false)
@Column(name = "id")
private int id;
@Basic(optional = false)
@Column(name = "table1_id")
private int table1Id;
// Gettters and setters methods
}
THe JPA работает отлично, но предводитель JAXB и распаковать операции с этим кодом:
JAXBContext jc1 = JAXBContext.newInstance(Table1.class);
Marshaller marshaller = jc2.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(ts, System.out);
javax.xml. bind.JAXBException выбрано.
Послание:
Exception [EclipseLink-110] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Descriptor is missing for class [java.util.Map].
Mapping: org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping[table2]
Descriptor: XMLDescriptor(org.jbiowhpersistence.datasets.gene.gene.entities.Table1 --> [DatabaseTable(table1)])
Exception [EclipseLink-110] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Descriptor is missing for class [java.util.Map].
Mapping: org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping[table2]
Descriptor: XMLDescriptor(org.jbiowhpersistence.datasets.gene.gene.entities.Table1 --> [DatabaseTable(table1)])
Что плохого в моем классе Definiton? Спасибо заранее и с наилучшими пожеланиями.
'java.util.Map' можно преобразовать с помощью JAXB (см .: http://blog.bdoughan.com/2013/03/jaxb-and-javautilmap.html). «XmlAdaper» требуется использовать, когда вы хотите настроить, как «Карта» представлена в XML. –