简体   繁体   中英

JPA One-to-Many Relationship in Oracle NoSql DB - Insert, OracleNoSQLRecord Exception

I am working on JPA with Oracle NoSql DB (using eclipselink api).

I have two classes Person and Address, which have one-to-many relationship. The code snippet below:

@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<>();

And

@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;

My Main function is as below:

    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();

My persistence.xml is as below:

<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>

When I am executing my main method following exception is thrown:

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)

However, I am able to insert data same data when I am using @Embedded/@Embeddable annotations instead of @OneToMany relationship annotations.

Any help appreciated.

Using @NoSql (dataFormat = DataFormatType.XML)

instead of

@NoSql (dataFormat = DataFormatType.MAPPED)

resolves the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM