I am working on a JavaEE7/CDI project that makes heavy use of pure JDBC code.
Is there a library that could help me eliminate the boilerplate code, much like what JDBCTemplate does for Spring. Obviously adding Spring as dependency is not an option.
I would suggest the Apache commons-dbutils, which is a light library for JDBC operations. It provides the QueryRunner class that seems very similar to Spring's JDBCTemplate.
I would look into MyBatis (formerly iBatis).
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
Related
I'm new with Spring and I would like to use Spring Data with Jpa, Maven and Mysql in Eclipse environment. Then I'm going to use Tomcat as application server
I read a lot of tutorial but all of them are different from each other therefore don't understand what I have really to do. At the moment I'm confusing between these technology and DAO/DAL structure
Must I use Hibernate?
I have to use it with java JPA and then I have to add Spring data?
If anyone has an example or a tutorial I thank him.
Official spring data link
For spring-data-jpa,maven,mysql,hibernate,eclipse example tutorial
Hibernate is optional, it depends on your use case what you want to do. Its a ORM for mapping your db tables into java objects and have lot of other features like session caching, second level caching, criteria queries, validation, search/filters etc.
If you are just testing queries to load data you can use spring jpa queries directly, you will find examples in spring-data-jpa link
I would like to be able to write linq-style specific queries in java using jinq (http://www.jinq.org/index.html) and the java 8 streams. However, in the set-up section it says you need to use the JPA entityManagerFactory (http://www.jinq.org/docs/queries.html#N65755). In my project I am using hibernate and the sessionFactory. Does that mean that I cannot use jinq at all or there's some workaround?
Simple viewing of the javadoc for "JINQ" shows very clearly that they take in a JPA "EMF", so you have to use JPA to use it.
I see no downside of using JPA. You can easily enough dip in to vendor specifics when you really need to and still use JPA for the majority
In the screen where you can add the Hibernate library to a project, there are two options, Hibernate and Hibernate JPA.
What is the difference between the 2? Googling did not provide an explanation.
I found this to provide a good explanation.
http://elope.wordpress.com/2007/09/06/difference-between-jpa-and-hibernate/
From the above blog:
So if i need to put in Concise words:
a) JPA is Persistence Api which your code should use.
b) JPA Api will pass on the call to actual peristence provider (ex:Hibernate/TopLink) to do the actual work.
c) If you are looking from Performance prespective ,it will be dependent on actual peristence provider (Hibernate/TopLink) and not on JPA as its just a wrapper layer.
d) If you are looking from code dependency prespective ,JPA makes more sense as your code is dependent on standard Java Api.
e) If you have used Hibernate then you will find that certain features are missing in JPA like criteria queries etc.This does not mean that you can’t write criteria query in your application, you need to get Session object from JPA Entity manager and now you are as good as in hibernate project.
But now your code is dependent on Specific impl (Hibernate),going forward you can see more things getting added in JPA (2.0)
f) Should you use JPA: My take is you should ,API is clean and although not everthing you need is their but is a good step forward.
I don't know what "screen" you mean, but in general you can use Hibernate directly (Hibernate API) or as a JPA provider. As JPA is a standard API one can code against this API and switch between implementations (Hibernate, EclipseLink, OpenJPA, ...). When using Hibernate API you are tied to this but you can utilize features that are not standardized by JPA.
I just start reading Sun's JDBC tutorial. I installed MySQL Connector/J and was just skimming through its menu, then came across this statement:
"Although JDBC is useful by itself, we would hope that if you are not familiar with JDBC that after reading the first few sections of this
manual, that you would avoid using naked JDBC for all but the most trivial problems and consider using one of the popular persistence
frameworks such as Hibernate, Spring's JDBC templates or Ibatis SQL Maps to do the majority of repetitive work and heavier lifting that is sometimes required with JDBC"
So what are the relationships between Spring, Hibernate, JDBC? What does the statement mean by saying "avoid using naked JDBC"?
Thanks,
Sarah
If you use Hibernate, Spring's JDBC templates or Ibatis SQL Maps, you're still using JDBC, but you don't have to deal with it directly. They're doing it for you, and to a degree, insulate you from some difficulties in the use of JDBC.
Hibernate is an object-relational-mapping framework.
MyBatis, formerly known as iBatis, is a data mapping framework.
Spring is a wide-ranging set of web framework components, and includes templating subsystems that allow integration with JDBC, Hibernate, or iBatis and abstract away some of the details of dealing with any of them.
You should indeed learn JDBC, but also (eventually) learn some of these others and try to avoid using JDBC directly for anything very complex.
These ideas are also (especially Hibernate) closely related to the Java Persistence API (JPA), which is also certainly worth learning.
You might also want to look at Java Data Objects (JDO).
Don't try to learn it all at once, though.
Starting with JDBC is a good idea. Staying with it is not.
Alphabets > Words > Sentences.
That is how we learn (natural) language.
Similarly, I see:
JDBC API > JDBC API with Connection Pooling > Spring JDBC Template > ORM/JPA/JDO
"avoid using naked JDBC" - Don't use the JDBC API directly.
Hibernate - A database persistence framework. It lets you store Java objects in databases. Pretty nifty.
Spring - It's actually a web development framework. The JDBC templates are an abstraction to JDBC, I think.
I'm creating an application that has to work with different databases (Oracle, MSSQL, MySQL...) through JDBC. I have to work via JDBC because my application calls stored procedures in these databases.
What is the best aproach for building such applications? Are there any frameworks for this?
Important: The solution must nicely deal with Spring Framework.
I am thinking about Hibernate, since it is robust ORM solution and it has a buildin support for stored procedures: http://docs.jboss.org/hibernate/stable/core/reference/en/html_single/#sp_query
Please, provide me with your oppinions about my current choise.
Best regards,
Max
I would give myBatis a good look. It handles all the pain associated with JDBC and transactions and mapping resultsets to Java objects or hashes.
It also plays nice with SQL and stored procedure by separating them from the Java code and configuring them in XML configuration files. This works in practice a lot better because it is easier to copy queries from XML to an interactive SQL browser and vice versa.
To connect to multiple datasets you need to create an SqlSessionFactory for each datasource.
Hibernate is usually the standard option (and the one I'd choose). I prefer using JPA over Hibernate, but that's not an option if you need Stored Procedures. But regarding the comment about iBatis:
While I have no experience with iBatis myself, it seems the Spring Support for iBatis is not bad:
From the Spring Reference, chapter 13.6: iBATIS SQL Maps:
The iBATIS support in the Spring
Framework much resembles the JDBC
support in that it supports the same
template style programming, and as
with JDBC and other ORM technologies,
the iBATIS support works with Spring's
exception hierarchy and lets you enjoy
Spring's IoC features.
Transaction management can be handled
through Spring's standard facilities.
No special transaction strategies are
necessary for iBATIS, because no
special transactional resource
involved other than a JDBC Connection.
Hence, Spring's standard JDBC
DataSourceTransactionManager or
JtaTransactionManager are perfectly
sufficient.