In this paper we will try to write a mavenized java application that will connect to an Oracle database and perform some data manipulation.
First of all, why maven ?
If you have the right jar dependencies whithout maven that's great ! no need to use maven... Personally i've faced a big problem to download and put the correct dependencies (no only packages but versions also!) together into my project.
Then let's talk maven !
Run this command line :
mvn archetype:generate -DgroupId=com.itillusion.migrators -DartifactId=loader -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
And that's all, no need to other dependencies ...
So our new pom.xml become :
Note that here we use hibernate 5.2.11
3- Build the HibernateUtils class
This class is recommended, it is a Singleton design pattern implementation, and will facilitate our work
Note in the fragment
We have added our annotated classes !
I'm sure that there is a more elegant methods to do this, (iteration on packages using java reflection api) but in my case i just need two tabled to map.
Note also that we conserved our classic hibernate.cfg.xml file. (Surely we can avoid that and use100% java code to confgiure hibernate, but i don't recommand that and it is more logic to store database informations in a separate faile that can change independently from our code)
Here is a simple cfg file:
to be continued
First of all, why maven ?
If you have the right jar dependencies whithout maven that's great ! no need to use maven... Personally i've faced a big problem to download and put the correct dependencies (no only packages but versions also!) together into my project.
Then let's talk maven !
1- Create the application with maven
Run this command line :
mvn archetype:generate -DgroupId=com.itillusion.migrators -DartifactId=loader -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2- Add dependencies to POM
1 2 3 4 5 | < dependency > < groupid >org.hibernate</ groupid > < artifactid >hibernate-core</ artifactid > < version >5.2.11.Final</ version > </ dependency > |
1 2 3 4 5 | < dependency > < groupid >org.hibernate</ groupid > < artifactid >hibernate-tools</ artifactid > < version >3.2.0.ga</ version > </ dependency > |
And that's all, no need to other dependencies ...
So our new pom.xml become :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | < project xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://maven.apache.org/POM/4.0.0" xsi:schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > < modelversion >4.0.0</ modelversion > < groupid >com.itillusion.migrators</ groupid > < artifactid >loader</ artifactid > < packaging >jar</ packaging > < version >1.0-SNAPSHOT</ version > < name >loader</ name > < dependencies > < dependency > < groupid >junit</ groupid > < artifactid >junit</ artifactid > < version >3.8.1</ version > < scope >test</ scope > </ dependency > < dependency > < groupid >org.hibernate</ groupid > < artifactid >hibernate-core</ artifactid > < version >5.2.11.Final</ version > </ dependency > < dependency > < groupid >org.hibernate</ groupid > < artifactid >hibernate-tools</ artifactid > < version >3.2.0.ga</ version > </ dependency > </ dependencies > </ project > |
3- Build the HibernateUtils class
This class is recommended, it is a Singleton design pattern implementation, and will facilitate our work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package com.itillusion.migrators.utils; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import tn.cnss.referentiel.models.Assure; import tn.cnss.referentiel.models.Beneficiaire; public class HibernateUtils { private static final SessionFactory sessionFactory; static { try { StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure( "hibernate.cfg.xml" ).build(); //Annotated classes to be scanned List< class >> classes = new ArrayList< class >>(); classes.add(Assure. class ); classes.add(Beneficiaire. class ); //Attach classes MetadataSources metadataSources = new MetadataSources(standardRegistry); for (Class annotatedClass : classes) { metadataSources.addAnnotatedClass(annotatedClass); } sessionFactory = metadataSources.buildMetadata() .buildSessionFactory(); } catch (Throwable th) { System.err.println( "Enitial SessionFactory creation failed" + th); throw new ExceptionInInitializerError(th); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Session getSession() throws org.hibernate.HibernateException { SessionFactory sessionFactory = HibernateUtils.getSessionFactory(); Session sess = null ; try { sess = sessionFactory.getCurrentSession(); } catch (org.hibernate.HibernateException he) { sess = sessionFactory.openSession(); } return sess; } } </ class ></ class > |
1 2 | classes.add(Assure. class ); classes.add(Beneficiaire. class ); |
We have added our annotated classes !
I'm sure that there is a more elegant methods to do this, (iteration on packages using java reflection api) but in my case i just need two tabled to map.
Note also that we conserved our classic hibernate.cfg.xml file. (Surely we can avoid that and use100% java code to confgiure hibernate, but i don't recommand that and it is more logic to store database informations in a separate faile that can change independently from our code)
Here is a simple cfg file:
1 2 3 4 5 6 7 8 9 10 | < hibernate-configuration > < session-factory name = "mainSession" > < property name = "hibernate.connection.driver_class" >oracle.jdbc.driver.OracleDriver</ property > < property name = "hibernate.connection.password" >PASSWORD</ property > < property name = "hibernate.connection.url" >jdbc:oracle:thin:@xxx.yy.zz.tt:SID</ property > < property name = "hibernate.connection.username" >USERNAME</ property > < property name = "hibernate.default_schema" >DEFAULT_SCHEMA </ property > <!--USERNAME--> < property name = "hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</ property > </ session-factory > </ hibernate-configuration > |
No comments:
Post a Comment