-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Oracle NoSQL Database - Lob type fix (#1764)
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
Showing
5 changed files
with
406 additions
and
1 deletion.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...e.nosql/src/it/java/org/eclipse/persistence/testing/models/jpa/nosql/DataTypesEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
252 changes: 252 additions & 0 deletions
252
...rc/it/java/org/eclipse/persistence/testing/tests/jpa/nosql/sdk/NoSQLJPADataTypesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.