Oracle XML DB and the Java Persistence API - oracle

I've stumbled upon Oracles XML DB functionality, but so far, from my reading I only see examples with JDBC implementations.
This is one of the examples:
import oracle.xdb.XMLType;
...
PreparedStatement stmt = conn.prepareStatement(
"SELECT e.poDoc FROM po_xml_tab e" );
ResultSet rset = stmt.executeQuery();
while( rset.next() ) {
// get the XMLType
XMLType poxml = ( XMLType )rset.getObject( 1 );
// get the XML as a string...
String poString = poxml.getStringVal();
}
According to the official xml db developers guide, there is an option to store data in a object-relational (structured) format. This makes me think that there should be an almost seamless link between XML DB and JPA. Maybe I'm missing something, or maybe it just doesn't exist?
Can they work together? Are there other options than JDBC? Or can I just do JPA for the queries, and the JDBC for XML?
Edit: Is Oracle XML DB even worth using it? Since it doesn't look like anyone uses it (according to the views and responses so far).

You can use Oracle XDB features from JPA. If you have a column of an XMLType you can map it into a JPA Entity using a #Basic mapping to a String.
In EclipseLink you could use a Converter to map it to another data-type, or use the DirectToXMLTypeMapping to map the DOM.
If you want to map the XML to objects, you could use a Converter that uses JAXB.

Related

Embeded H2 Database for dynamic files

In our application, we need to load large CSV files and fetch some data out of it. For example, getting the distinct values from the CSV file. For this, we decided to go with in-memory DB's like H2, as there is no need to store the data in persistent storage.
However, the file is so dynamic that the columns may not be the same. I need to load the file to the H2 database to a table that is temporary for that session.
Tech Stack is Spring boot and H2.
The examples I see on forums is using a standard entity that knows what fields the table has. However my case the table columns will be dynamic
I tried the below in spring boot
public interface ImportCSVRepository extends JpaRepository<Object, String>
with
#Query(value = "CREATE TABLE TEST AS SELECT * FROM CSVREAD('test.csv');", nativeQuery = true)
But this gives unmanaged entity error. I understand why the error is thrown. However I am not sure how to achieve this. Also please clarify if I should use Spring-batch ?
You can use JdbcTemplate to manually create tables and query/update the data in them.
An example of how to create a table with JdbcTemplate
Dynamically creating tables and defining new entities (or modifying existing ones) is hardly possible with spring-data repositories and #Entity-ies. You probably should also check some NoSQL dbs like MongoDb - it's easier to define documents (or key-value objects - Redis) with dynamic structures in them.

Data migration among multiple databases with spring-boot

I am trying to make an application where data will be migrated from one database to another database (Multiple dbs will be used). User can select the table at runtime & push it to target db. I am using spring-boot, spring data JPA & trying with Flyway.
My issue is how to read the complete schema from source db as user can select the source db at runtime?
Sumit
You can obtain a MetaData object from a JDBC connection and use it to obtain all kinds of information about the database, e.g. the list of tables.
See the following example which I took from a tutorial.
databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"});
System.out.println("Printing TABLE_TYPE \"TABLE\" ");
System.out.println("----------------------------------");
while(resultSet.next())
{
System.out.println(resultSet.getString("TABLE_NAME"));
}
Note: JPA is most likely not the right tool for the job. Consider using Springs JdbcTemplate instead.

Map Oracle timestamp type to java.sql.Timestamp or java.util.Date using Hibernate?

I have an existing database with hundreds of tables using TIMESTAMP(6) as the data type for some columns.
When I reverse engineered this database using Hibernate, the Java type Serializable is used to map these columns.
How can I get Hibernate to automatically map columns in these hundreds of tables to java.sql.Timestamp or java.util.Date?
This question is related to How to map oracle timestamp to appropriate java type in hibernate? However, the answer to that question was a work around involving an ant task modifying the generated classes. I don't want to modify the generated classes, I want to get the right type in the first place.
I see there's a solution like
<sql-type jdbc-type="OTHER" hibernate-type="java.sql.Timestamp" />
I wouldn't mind if the solution was more like
<sql-type jdbc-type="TIMESTAMP" hibernate-type="java.sql.Timestamp" />
But I don't want all unmapped types (OTHER) to go to java.sql.Timestamp. I would like to simply tell Hibernate to map these Oracle timestamps to Java's timestamp class.
When I use Netbeans to do the mapping, it detects the timestamp in Oracle and properly converts it. I cannot use Netbeans though because I would like to use an automated tool in my build script so I'm using Hibernate.

BLOB and CLOB data type same programs

is it possible to develop a database application to demonstrate storing and retrieving of BLOB (Binary Large Object) and CLOB (Character Large Object) objects using
Front - end : Java,
Back-end : Oracle
I have installed Netbeans7.3.1 and Oracle 11g
The question is vague- but yes. There are many ways; my preference is to use something like JPA (I use EclipseLink) with your Oracle database. Once you have JPA setup for the database, create an entity an use the #Lob annotation on the field to mark it as a Lob type. Then you can retrieve it from the database and work on it as an object. In the case of Blob you get a byte array back, and Clob comes back as a String.
For sure it is possible. As an entry point you might start here Oracle JDBC tutorial
or maybe here (depending what you want to achieve)
Oracle Java EE 6 tutorial

Is there a generic way of getting columns in ResultsSet of MapRow

I am using SimpleJdbcTemplate and for example I have something like this:
#Override
public Variant mapRow(ResultSet rs, int rowNum) throws SQLException
then I am getting the values from this result set with lines of code like this:
variant.setName(rs.getString("variant_name"));
so I have to look at my table, see what type should I use for each column, - getString for String in this example - ...so I will have getString, getLong, getInt,...
I was wondering if there a more generic way of getting these values from result set without the need to specify the correct type and hope that Spring JDBC takes care of some boxing/unboxing on these generic types
If you want to map JDBC results to your object model, then you're going to have to live with doing that. That's the deal when you use JDBC.
If you want something more high level, including column-to-property mapping, then you need a better tool. You could go the whole hog and use Hibernate, but that carries a whole load of baggage, and presents 10 new problems for every one it solves.
Have a look at MyBatis (formerly known as iBatis). This is a pretty basic framework for mapping JDBC result sets to javabeans, with connection/statement management backed in. Spring provides support for iBatis 2, but iBatis 2 itself is no longer supported. The new MyBatis 3.x isn't supported by Spring out-of-the-box, but the MyBatis project does provide it's own Spring integration.

Resources