我正在 JPA 上使用 Oracle NoSql 数据库(使用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>
当我执行main方法时抛出异常:
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 关系注释时,我能够插入相同数据。
任何帮助表示感谢。
答案 0 :(得分:0)
使用 @NoSql(dataFormat = DataFormatType.XML)
而不是
@NoSql(dataFormat = DataFormatType.MAPPED)
解决了这个问题。