Right way to configure JPA to work with any JPA implementation - java

I have built a small application. Now I’m looking for the right way to deal with the JPA configuration.
My app use JPA 2.0 to connect to a database. Right now the configuration is hardcoded in my persistence.xml file. When I built the jar file, my persistence.xml is included in the jar.
So my questions are:
How do I configure my app/JPA to work with any JPA implementation.
And how can I use the same jar as a standalone application and as a component to deploy on a server (eg. Glassfish)

First of all JPA is a specification, which is being implemented by the ORM framework providers such as Hibernate, Eclipselink etc.
To make your application compatible with any implementation of JPA, you should strictly follow JPA specifications, you should not be using any ORM vendor specific features, this will allow your application to work with any ORM framework, however you have to specify the dialect class by your self to interact with a particular database.
One thing you can do here is write a properties file which will have all the database name with corrosponding dialect class for that particular database, you can ask user to choose the database and can fetch the related class name(Dialect class name) from the properties file.
About using the same application as standalone and using it as a component to deploy on a server, I am sorry but I am not getting your point.

Related

How can I generate entities classes from database using Spring Boot and IntelliJ Idea?

I trying to make a project using the Spring Initializr wizard and I already have a database, so I want to generate entities classes using Spring Boot and IntelliJ Idea.
Prerequisites are:
You have your Spring Boot project initialized correctly in IDEA
JPA persistence.xml file or similar has been generated correctly
Then you have to do these things:
Create a DataSource - here you will add a simple DataSource that will connect to your database. The setup should be intuitive - you only provide connection details and add DB drivers (IDEA can download them for you)
Add JPA/Hibernate facet. You can do it like this or this.
Now you should be able to generate entities using IDEA. What you want to do here is choose Generate by Database Schema. The dialog will let you select the tables you want to use, the rest should be up to you.

Share Grails database with standalone Java application?

In Grails I am creating "domains", i.e. classes with some fields, which are automatically mapped onto database.
Is it possible to achieve the same (or similar) functionality in separate Java or Groovy project?
I wish it to be compatible with separate Grails-controlled database, i.e. for example, to write desktop application to populate database.
Grails mappings are based on Hibernate ORM. hibernate is independent ORM rom map objects to DB. I think it will help you. See Hibernate docs

Undestanding how to use JPA to abstract the ORM

I am working on a Java project for the university and I am trying to learn about framework, technologies and best practices for creating a well structured software that follows software engineering principles. I decided to use Spring (Spring-data-jpa), and Hibernate for the standalone software and now I am having some trouble in understanding how to use the Java Persistence Api to create an abstraction layer above the Hibernate implementation provider. What I am trying to figure out is in which part (configuration file or java class) I can switch from an ORM tool to another one. I saw that I have to use the persistence.xml file to specify the persistence unit and persistence context and also the DB parameters, but it's not clear how the EntityManager bind itself with the underlying ORM tool and in which properties this bind is set. Is the "provider" properties inside persistence.xml file that create this binding? Any link/references/examples or guide will be appreciated, thanks in advance and excuse me for my english ;)
Each persistence unit in he persistence.xml file is associated to a provider. For example, with Hibernate:
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
If you want to use another provider, you'll specify another value for this element.
Note that this is usually unnecessary, because you typically use only one provider in an application. If a single provider is available in the classpath, JPA will use that one. So switching from Hibernate to EclipseLink for example just consists in having the EclipseLink jars in the classpath rather than the Hibernate jars.
Hibernate works on the JPA API so you don't have to explicitly configure JPA if you are using hibernate. The ORM integration happens in the DAO "Data Access Object" layer of your application.

How to use existing java data bindings in grails?

I need to add a new datasource to my grails project wich uses the musicbrainz postgresql database. http://musicbrainz.org/doc/MusicBrainz_Database
I found a project on github where the data bindings are ready to use for a spring project:
https://github.com/lastfm/musicbrainz-data
Am i able to use these data bindings in a grails 2.2.3 project? If yes, how can i do this? (because there is no hibernate xml as needed by grails (regarding to the grails documentation: Hibernate Mapped Domain Classes))
I don't think it will be. Just setup the additional datasource and model out the tables or objects you need.
How do you access two databases in Grails
Once you do that, you can use all of the GORM methods and dynamic finders to get your data. Plus validation criteria, transactions, etc. Unless there is some very specialized criteria that make it necessary to bypass GORM, I would suggest leveraging it.

Do HibernateDaoSupport in spring require hibernate jar files for support

I am using spring 2.5 with HibernateDaoSupport. My doubt is, do I still require Hibernate jar files for support of HibernateDaoSupport.
I did a sample example in netbeans. I unchecked the hibernate jar library and the application is still working.
But is it correctly working with the HibernateDaoSupport class without using Hibernate jar files..
HibernateDaoSupport is present in Spring jars. It is used to provide support to Hibernate-based data access objects. If you plan in using Hibernate for read/write into DB, you will require the hibernate jar.
Please check out the following link for more details:
http://www.vaannila.com/spring/spring-hibernate-integration-1.html
you need to put org.springframework.orm_x.x.x.RELEASE.jar file in your project

Categories

Resources