EclipseLink JPA With SQL Server

EclipseLink JPA With SQL Server explains step by step details of setting / configuring Java Persistence JPA With EclipseLink And SQL Server

How To Configure EclipseLink JPA With SQL Server?

Java Persistence API, is a standard interface which wraps different ORM tools such as  Hibernate, EclipseLink, OpenJPA etc.

ie; you can able to change Hibernate implementation to EclipseLink implementation without changing the code base.

On this standalone JPA Example, we are using EclipseLink With SQL Server

If you want to configure EclipseLink JPA With Oracle, you can follow JPA Tutorial With EclipseLink

EclipseLink is popular open source ORM (Object Relation Mapping) tool for Java platform, for mapping an entity to a traditional relational like Oracle, MySQL, SQL Server etc

I am going to re-use JPA Tutorial With EclipseLink

Note

You need to remove classes12.jar (Oracle driver) and add sqljdbc4.jar (SQL Server driver) into classpath

You can check below different database with Hibernate and EclipseLink examples

Database Driver class Jar name Examples
MySQL com.mysql.jdbc.Driver mysql-connector-java-5.1.25-bin.jar (exact name depends on version)
http://www.mysql.com/downloads/connector/j/
EclipseLink JPA With MySql
Hibernate JPA With MySql
HSQLDB org.hsqldb.jdbcDriver hsqldb.jar
http://hsqldb.sourceforge.net
Hibernate JPA With HSQL
EclipseLink JPA With HSQL
Sybase com.sybase.jdbc3.jdbc.SybDriver jconnect.jar
http://www.sybase.com/products/allproductsa-z/softwaredeveloperkit/jconnect
Hibernate JPA With Sybase
EclipseLink JPA With Sybase
Apache Derby org.apache.derby.jdbc.EmbeddedDriver derby.jar
http://db.apache.org/derby/
Hibernate JPA With Derby
EclipseLink JPA With Derby
IBM DB2 com.ibm.db2.jcc.DB2Driver db2jcc4.jar
http://www-01.ibm.com/software/data/db2/linux-unix-windows/download.html
Hibernate JPA With DB2
EclipseLink JPA With DB2
PostgreSQL org.postgresql.Driver postgresql-8.4-701.jdbc4.jar (exact name depends on PostgreSQL version)
http://jdbc.postgresql.org
Hibernate JPA With PostgreSQL
EclipseLink JPA With PostgreSQL
SQL Server (Microsoft driver) com.microsoft.sqlserver.jdbc.SQLServerDriver sqljdbc4.jar
http://msdn.microsoft.com/en-gb/data/aa937724%28en-us%29.aspx
Hibernate JPA With SQL Server
EclipseLink JPA With SQL Server
Informix com.informix.jdbc.IfxDriver ifxjdbc.jar
http://www14.software.ibm.com/webapp/download/search.jsp?go=y&rs=ifxjdbc
Hibernate JPA With Informix
EclipseLink JPA With Informix
H2 org.h2.Driver h2.jar
http://www.h2database.com/h2-2014-10-17.zip
Hibernate JPA With H2
EclipseLink JPA With H2

Modify persistence.xml

persistence.xml file must be under src/META-INF (Please check below screenshot(project structure)

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <class>com.test.jpa.Student</class>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
      <property name="javax.persistence.jdbc.url"    value="jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName>" />
      <property name="javax.persistence.jdbc.user" value="username" />
      <property name="javax.persistence.jdbc.password" value="password" />
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
  </persistence-unit>
</persistence>

Package Structure

Now package structure looks like following

EclipseLink JPA With SQL Server Structure

Testing (JPAExample.java)

package com.test.jpa;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;

//EclipseLink JPA With SQL Server Example

public class JPAExample {

 
private EntityManager entityManager = EntityManagerUtil.getEntityManager();

 
public static void main(String[] args) {
   
JPAExample example = new JPAExample();
    System.out.println
("After Sucessfully insertion ");
    Student student1 = example.saveStudent
("Sumith");
    Student student2 = example.saveStudent
("Anoop");
    example.listStudent
();
    System.out.println
("After Sucessfully modification ");
    example.updateStudent
(student1.getStudentId(), "Sumith Honai");
    example.updateStudent
(student2.getStudentId(), "Anoop Pavanai");
    example.listStudent
();
    System.out.println
("After Sucessfully deletion ");
    example.deleteStudent
(student2.getStudentId());
    example.listStudent
();
   

 
}

 
public Student saveStudent(String studentName) {
   
Student student = new Student();
   
try {
     
entityManager.getTransaction().begin();
      student.setStudentName
(studentName);
      student = entityManager.merge
(student);
      entityManager.getTransaction
().commit();
   
} catch (Exception e) {
     
entityManager.getTransaction().rollback();
   
}
   
return student;
 
}

 
public void listStudent() {
   
try {
     
entityManager.getTransaction().begin();
     
@SuppressWarnings("unchecked")
     
List<Student> Students = entityManager.createQuery("from Student").getResultList();
     
for (Iterator<Student> iterator = Students.iterator(); iterator.hasNext();) {
       
Student student = (Student) iterator.next();
        System.out.println
(student.getStudentName());
     
}
     
entityManager.getTransaction().commit();
   
} catch (Exception e) {
     
entityManager.getTransaction().rollback();
   
}
  }

 
public void updateStudent(Long studentId, String studentName) {
   
try {
     
entityManager.getTransaction().begin();
      Student student =
(Student) entityManager.find(Student.class, studentId);
      student.setStudentName
(studentName);
      entityManager.getTransaction
().commit();
   
} catch (Exception e) {
     
entityManager.getTransaction().rollback();
   
}
  }

 
public void deleteStudent(Long studentId) {
   
try {
     
entityManager.getTransaction().begin();
      Student student =
(Student) entityManager.find(Student.class, studentId);
      entityManager.remove
(student);
      entityManager.getTransaction
().commit();
   
} catch (Exception e) {
     
entityManager.getTransaction().rollback();
   
}
  }
}

Output

After Sucessfully insertion 
Sumith
Anoop
After Sucessfully modification 
Sumith Honai
Anoop Pavanai
After Sucessfully deletion 
Sumith Honai

 











Your email address will not be published. Required fields are marked *