JPA Отношение один-ко-многим в Oracle NoSql DB - вставка, исключение OracleNoSQLRecord
Я работаю над JPA с Oracle NoSql DB (используя eclipselink api).
У меня есть два класса Person и Address, которые имеют отношение один ко многим. Фрагмент кода ниже:
@Entity
@NoSql (dataFormat = DataFormatType.MAPPED)
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Field(name = "_id")
private String id;
private String firstName;
private String lastName;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Address> addresses = new ArrayList<>();
А также
@Entity
@NoSql (dataFormat = DataFormatType.MAPPED)
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Field(name = "_id")
private String id;
private String street;
private String city;
private String country;
private Integer pincode;
@JoinField(name = "PERSON_ID", referencedFieldName = "_id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Person person;
Моя основная функция, как показано ниже:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person();
p1.setFirstName("fn");
p1.setLastName("ln");
List<Address> addrList = new ArrayList<>();
Address addr1 = new Address();
addr1.setStreet("str");
addr1.setCity("City");
addr1.setCountry("Ctry");
addr1.setPincode(123456);
addrList.add(addr1);
Address addr2 = new Address();
addr2.setStreet("str2");
addr2.setCity("City2");
addr2.setCountry("Ctry2");
addr2.setPincode(223456);
addrList.add(addr2);
p1.setAddresses(addrList);
em.persist(p1);
tx.commit();
Мой файл persistence.xml выглядит так:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.nosql.OracleNoSQLPlatform"/>
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.nosql.OracleNoSQLConnectionSpec"/>
<property name="eclipselink.nosql.property.nosql.host" value="dockerHost:5002"/>
<property name="eclipselink.nosql.property.nosql.store" value="camiantDBstore"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
Когда я выполняю свой основной метод, выдается следующее исключение:
javax.persistence.PersistenceException: Exception [EclipseLink-90000] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.eis.EISException
Error Code: 000
Call: Executing MappedInteraction()
spec => null
properties => {nosql.operation=PUT_IF_ABSENT}
Internal Exception: javax.resource.ResourceException: java.lang.ClassCastException: java.lang.String cannot be cast to org.eclipse.persistence.internal.nosql.adapters.nosql.OracleNoSQLRecord
input => [DatabaseRecord(
PERSON.FIRSTNAME => fn
PERSON.LASTNAME => ln
PERSON.ID => F9A2D757-BA55-4218-BC8F-0D6304649AEC
Error Code: 000
Call: Executing MappedInteraction()
PERSON.ADDRESSES_ID => [8E6F3B7F-C16E-4378-9BC7-7BBB2EBE5654, 638D01DA-6B06-4AB2-9E8B-F4A6DACA1D6C, B92A85D0-9161-4C35-8A44-C06C7C53E25F])]
spec => null
Query: InsertObjectQuery(test.Person@5dda768f)
properties => {nosql.operation=PUT_IF_ABSENT}
input => [DatabaseRecord(
PERSON.FIRSTNAME => fn
PERSON.LASTNAME => ln
PERSON.ID => F9A2D757-BA55-4218-BC8F-0D6304649AEC
PERSON.ADDRESSES_ID => [8E6F3B7F-C16E-4378-9BC7-7BBB2EBE5654, 638D01DA-6B06-4AB2-9E8B-F4A6DACA1D6C, B92A85D0-9161-4C35-8A44-C06C7C53E25F])]
Query: InsertObjectQuery(test.Person@5dda768f)
at org.eclipse.persistence.eis.EISException.resourceException(EISException.java:65)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:879)
at testnosqljpa.TestNoSqlJPA.main(TestNoSqlJPA.java:60)
Caused by: Exception [EclipseLink-90000] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.eis.EISException
at org.eclipse.persistence.eis.EISException.resourceException(EISException.java:74)
Однако я могу вставлять данные в те же данные, когда использую аннотации @Embedded/@Embeddable вместо аннотаций отношений @OneToMany.
Любая помощь приветствуется.
1 ответ
Использование @NoSql (dataFormat = DataFormatType.XML)
вместо
@NoSql (dataFormat = DataFormatType.MAPPED)
решает проблему.