Mixing jdbc with hibernate? - java

Background: I am working on a web application, which uses oracle jdbc and access database with java code and also sql queries which will be executed by our engine.
Now I am thinking about "replacing" jdbc over the time with hibernate t have -let's say - hibernate advantages.
My question is, can this be done step by step?
Can I start to create new tables with hibernate and work with hibernate without having risk with the existing database structure? can hibernate works in parallel with jdbc?

The answer is positive.
You can have both jdbc and hibernate data-sources in the same project, no problem.
Just be careful not to mix up those two data-sources, that's all.

Yes, JDBC and hibernate both can be used in parallel.
However, it depends on use cases. As per your question you just want to "replace" JDBC with Hibernate.
So, yes in this case you can start with :
creating new tables
creating hibernate mapping files
creating DAO layer
implementing hibernate code.

Related

Spring Application JPA and JDBC

I am starting with a new project and currently evaluating whether to use JPA or JDBC. Our operations mostly are going to be an bulk-insert and bulk-read and very rarely single insert/read.
I checked a prototype with JPA and JDBC and realized that both has its own merits and limitations.
Considering the current use case that for a fact I will only have always a bulk read and bulk write, which one will be a better option to go with ?
Spring JPA Repository gives a simple method save(Collection) which can take a collection and save as well.
Also Validations are also not be considered here, as the payload will already be validated in the layers above and the database layer should just do the read/write operations.
Does the JPA save(Collection<>) method in turn uses the jdbc templates or is it entirely a different implementation ?
Thanks in advance !
We use both JPA and JDBC in one application wiht no problem. Preferably we use JPA/JPQL and fall back to JDBC if needed. For JPA we use Spring Data JPA and for JDBC Spring JDBC Template.

Implementing database access using Spring or separate library / pattern?

I'm working on an app which uses hard coded sql statements to retrieve data from a database and then populate this data into pojo's. Spring jdbc template is being used so dont need to worry about opening/closing connections. Using hard-coded sql statements seems wrong ?
Is there a design pattern or library I can use to abstact the sql statements ?
Look at MyBatis (formly iBatis).
It let's you extract hardcoded SQLs into XML files (or even annotations),
It integrates with Spring container, and can use Spring Transaction.
and many more.
Using JdbcTemplate, your application code still has the responsibility to provide sql and the JdbcTemplate can then execute SQL query or updates, iterate over ResultSets and catch JDBC exceptions. If you want to get away with writing hard-coded sql statements, you need to look into an ORM tools like Hibernate, iBatisetc
Here's a good previous discussion of some of the issues surrounding the choice between using raw SQL or an ORM tool:
Hibernate, iBatis, Java EE or other Java ORM tool
There is nothing inherently wrong with having SQL inside Java classes (although it's a bit controversial). If you want to externalize SQL queries you can:
use MyBatis which is very mature
put SQL in your applicationContext.xml Spring configuration file and inject it to POJOs (poor man's MyBatis)
hide SQL behind DAO pattern (interface SomeDao and class SqlSomeDao having SQL encapsulated)
...going for full ORM like JPA if you already have SQLs is not the best idea
Also check out Spring Data JDBC generic DAO - small DAO implementation on top of JdbcTemplate and RowMapper<T>. Disclaimer: I'm an author of this library.

Distributed query with Hibernate multi-tenancy

I am using Hibernate's multi-tenancy feature via JPA, with a database per tenant strategy. One of my requirements is to be able to run a query against a table that exists in each database but obviously with different data. Is this possible?
Thanks in advance for your time.
Nope. this is not possible because when hibernate runs queries it is already initialized with a connection. MT support in Hibernate is basically done a little "outside of Hibernate" itself. It's kind of feeding hibernate with a proper connection and when it's fed :) it's bound to that connection.
If you need cross-tenant queries you might want to reconsider multitenancy or change JPA provider to the one that support "shared schema approach" e.g. EclipseLink. With shared shema approach you have two choices:
run native query agains table containing mt-aware entities
create additional entity - dont mark it as multitenant - map it to the table containing mt-ware entities and run JPQL query in standard manner

manage many database

I search a way in a spring 3.x application to manage many database.
Now we support mysql, but we need to support firebird and postgress sql.
we don't use orm.
what is the best way to support many database with spring?
You'll have multiple data sources - one for each JDBC driver you need. You'll have to find a way to select which one you want for a give case.
Once you define a data source per database, as duffymo said, define a transaction-manager for each, and you can use #Transactional("managerXname") for declarative transaction demarcation.

Use Hibernate and Ibatis in the same application

I need to write a new app but using some data acces logic from other two app. One uses Hibernate and the other uses iBATIS. Can I use in the same app both, Hibernate and iBATIS?. How?.
Update: Let me reformule my question. Let's forget I will rehuse some DAOs or domain clases. I need to use in the same app, Hibernate and iBATIS. How can I do that? Thanks for your time...
check appFuse project. it has skeleton implementations for both orms. just combine both of them into the same app. database usually doesn't care what's behind the jdbc driver that accesses it

Categories

Resources