Skip to content

Commit

Permalink
Oracle NoSQL Database - Lob type fix (#1764)
Browse files Browse the repository at this point in the history
This is fix when Lob type is loaded from the database. There are some additional tests too to verify various JPA/NoSQL data types.

Signed-off-by: Radek Felcman <radek.felcman@oracle.com>
  • Loading branch information
rfelcman committed Dec 12, 2022
1 parent e2ac8a6 commit 6d10143
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

// Contributors:
// Oracle - initial API and implementation
package org.eclipse.persistence.testing.models.jpa.nosql;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;

import org.eclipse.persistence.nosql.annotations.NoSql;

import java.sql.Timestamp;
import java.util.Arrays;

@Entity
@Table(name = "DATA_TYPES_TAB")
@NamedQueries({
@NamedQuery(name = "DataTypesEntity.findAll", query = "SELECT t FROM DataTypesEntity t"),
@NamedQuery(name = "DataTypesEntity.findById", query = "SELECT t FROM DataTypesEntity t WHERE t.id = :id"),
@NamedQuery(name = "DataTypesEntity.findByIdAndName", query = "SELECT t FROM DataTypesEntity t WHERE t.id = :id AND t.fieldString = :name")
})
@NoSql
public class DataTypesEntity {
@Id
private long id;
@Lob
@Column(name = "col_binary", columnDefinition = "BINARY")
private byte[] fieldBinary;
@Column(name = "col_boolean")
private boolean fieldBoolean;
@Column(name = "col_json", columnDefinition = "JSON")
private String fieldJson;
@Column(name = "col_null")
private String fieldNull;
@Column(name = "col_string")
private String fieldString;
@Column(name = "col_timestamp")
private Timestamp fieldTimestamp;

public DataTypesEntity() {
}

public DataTypesEntity(long id) {
this.id = id;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public byte[] getFieldBinary() {
return fieldBinary;
}

public void setFieldBinary(byte[] fieldBinary) {
this.fieldBinary = fieldBinary;
}

public boolean isFieldBoolean() {
return fieldBoolean;
}

public void setFieldBoolean(boolean fieldBoolean) {
this.fieldBoolean = fieldBoolean;
}

public String getFieldJson() {
return fieldJson;
}

public void setFieldJson(String fieldJson) {
this.fieldJson = fieldJson;
}

public String getFieldNull() {
return fieldNull;
}

public void setFieldNull(String fieldNull) {
this.fieldNull = fieldNull;
}

public String getFieldString() {
return fieldString;
}

public void setFieldString(String name) {
this.fieldString = name;
}

public Timestamp getFieldTimestamp() {
return fieldTimestamp;
}

public void setFieldTimestamp(Timestamp fieldTimestamp) {
this.fieldTimestamp = fieldTimestamp;
}

@Override
public String toString() {
return "DataTypesEntity{" +
"id=" + id +
", fieldBinary=" + Arrays.toString(fieldBinary) +
", fieldBoolean=" + fieldBoolean +
", fieldJson='" + fieldJson + '\'' +
", fieldNull='" + fieldNull + '\'' +
", fieldString='" + fieldString + '\'' +
", fieldTimestamp=" + fieldTimestamp +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

// Contributors:
// Oracle - initial API and implementation.
package org.eclipse.persistence.testing.tests.jpa.nosql.sdk;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.persistence.Query;
import jakarta.resource.cci.Connection;
import oracle.nosql.driver.NoSQLHandle;
import oracle.nosql.driver.ops.TableLimits;
import oracle.nosql.driver.ops.TableRequest;
import org.eclipse.persistence.internal.nosql.adapters.sdk.OracleNoSQLConnection;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.testing.models.jpa.nosql.DataTypesEntity;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
* Oracle NoSQL JPA database tests. Focused on various datatypes.
*/
public class NoSQLJPADataTypesTest {

/** The persistence unit name. */
private static final String PU_UNIT_NAME = "nosql-datatypes-sdk";

/* Name of your table */
private static final String TABLE_NAME = "DATA_TYPES_TAB";

private static final long ID = 1L;
private static final byte[] FIELD_BINARY = {1, 2, 3, 4};
private static final boolean FIELD_BOOLEAN = true;
private static final String FIELD_JSON = "{\"a\": 1.006, \"b\": null," +
"\"bool\" : true, \"map\": {\"m1\": 5}," +
"\"ar\" : [1,2.7,3]}";
private static final String FIELD_STRING = "abcd";
private static final Timestamp FIELD_TIMESTAMP = new Timestamp(123456L);

/** Logger. */
private static final SessionLog LOG = AbstractSessionLog.getLog();

@Test
public void testJPAFind() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
try {
DataTypesEntity dataTypesEntity = em.find(DataTypesEntity.class, ID);
LOG.log(SessionLog.INFO, String.format("Entity by em.find(...):\t%s", dataTypesEntity));

assertEquals(ID, dataTypesEntity.getId());
assertArrayEquals(FIELD_BINARY, dataTypesEntity.getFieldBinary());
assertEquals(FIELD_BOOLEAN, dataTypesEntity.isFieldBoolean());
assertNull(dataTypesEntity.getFieldNull());
assertEquals(FIELD_STRING, dataTypesEntity.getFieldString());
assertEquals(FIELD_TIMESTAMP, dataTypesEntity.getFieldTimestamp());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (em != null) {
em.close();
}
}
}

@Test
public void testJPAQueryFindAll() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
try {
Query query = em.createNamedQuery("DataTypesEntity.findAll", DataTypesEntity.class);
List<DataTypesEntity> testEntities = query.getResultList();
assertTrue(testEntities.size() > 0);
for (DataTypesEntity dataTypesEntity : testEntities) {
LOG.log(SessionLog.INFO, String.format("Entity by DataTypesEntity.findAll query:\t%s", dataTypesEntity));
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (em != null) {
em.close();
}
}
}

@Test
public void testJPAQueryFindById() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
try {
Query query = em.createNamedQuery("DataTypesEntity.findById", DataTypesEntity.class);
query.setParameter("id", ID);
List<DataTypesEntity> testEntities = query.getResultList();
assertTrue(testEntities.size() > 0);
for (DataTypesEntity dataTypesEntity : testEntities) {
LOG.log(SessionLog.INFO, String.format("Entity by DataTypesEntity.findById query:\t%s", dataTypesEntity));
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (em != null) {
em.close();
}
}
}

public static void testJPAPersist() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
try {
em.getTransaction().begin();
DataTypesEntity entity = prepareEntity(ID);
em.persist(entity);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em != null) {
em.close();
}
}
}

public static void testJPAMerge() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
try {
em.getTransaction().begin();
DataTypesEntity entity = prepareEntity(ID);
em.merge(entity);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em != null) {
em.close();
}
}
}

@AfterClass
public static void testJPADelete() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
try {
em.getTransaction().begin();
DataTypesEntity entity = em.find(DataTypesEntity.class, ID);
assertEquals(ID, entity.getId());
assertEquals(FIELD_STRING, entity.getFieldString());
em.remove(entity);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em != null) {
em.close();
}
}
}

private static DataTypesEntity prepareEntity(long id) {
DataTypesEntity dataTypesEntity = new DataTypesEntity(id);
dataTypesEntity.setFieldBinary(FIELD_BINARY);
dataTypesEntity.setFieldBoolean(FIELD_BOOLEAN);
dataTypesEntity.setFieldJson(FIELD_JSON);
dataTypesEntity.setFieldNull(null);
dataTypesEntity.setFieldString(FIELD_STRING);
dataTypesEntity.setFieldTimestamp(FIELD_TIMESTAMP);
return dataTypesEntity;
}

@BeforeClass
public static void testJPASetup() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PU_UNIT_NAME);
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Connection connection = em.unwrap(jakarta.resource.cci.Connection.class);
NoSQLHandle noSQLHandle = ((OracleNoSQLConnection)connection).getNoSQLHandle();
dropTable(noSQLHandle);
createTable(noSQLHandle);
testJPAPersist();
testJPAMerge();
em.getTransaction().commit();
em.close();
}


private static void createTable(NoSQLHandle handle) {
/*
* Create a simple table with an integer key and various other columns to test data types
*/
String createTableDDL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " " +
"(id INTEGER, " +
"col_binary BINARY, " +
"col_boolean BOOLEAN, " +
"col_json JSON, " +
"col_null STRING, " +
"col_string STRING, " +
"col_timestamp TIMESTAMP(3), " +
"PRIMARY KEY(id))";
TableLimits limits = new TableLimits(1, 2, 1);
TableRequest tableRequest = new TableRequest().setStatement(createTableDDL).setTableLimits(limits);
LOG.log(SessionLog.INFO, String.format("Creating table:\t%s", TABLE_NAME));
handle.doTableRequest(tableRequest, 60000, 1000);
LOG.log(SessionLog.INFO, String.format("Table \t%s is active", TABLE_NAME));
}

private static void dropTable(NoSQLHandle handle) {
/* Drop the table and wait for the table to move to dropped state */
LOG.log(SessionLog.INFO, String.format("Dropping table:\t%s", TABLE_NAME));
TableRequest tableRequest = new TableRequest().setStatement("DROP TABLE IF EXISTS " + TABLE_NAME);
handle.doTableRequest(tableRequest, 60000, 1000);
LOG.log(SessionLog.INFO, String.format("Table \t%s has been dropped", TABLE_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
@RunWith(Suite.class)
@SuiteClasses({
NoSQLJPATest.class,
NoSQLJPAMappedTest.class
NoSQLJPAMappedTest.class,
NoSQLJPADataTypesTest.class
})
public class NoSQLJPATestSuite {

Expand Down

0 comments on commit 6d10143

Please sign in to comment.