JPA Stored Procedure

This commit is contained in:
giuseppe.bueti 2016-02-26 16:43:11 +01:00
parent e00f62fb03
commit 3b5c46bbbf
8 changed files with 312 additions and 0 deletions

81
persistence-jpa/pom.xml Normal file
View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>jpa-storedprocedure</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<jee.version>7.0</jee.version>
<oracle.version>11.2.0.4</oracle.version>
<hibernate.version>5.1.0.Final</hibernate.version>
</properties>
<build>
<finalName>JpaStoredProcedure</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!--archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass>
</manifest> </archive -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- core library -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${jee.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Oracle JDBC -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.version}</version>
</dependency>
<!-- Junit Library -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,59 @@
package com.baeldung.jpa.model;
import javax.persistence.*;
/**
* Created by Giuseppe Bueti on 22/02/2016.
*/
@Entity
@Table(name = "CAR")
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(name = "findByModelProcedure", procedureName = "FIND_CAR_BY_MODEL", resultClasses = { Car.class }, parameters = { @StoredProcedureParameter(name = "p_model", type = String.class, mode = ParameterMode.IN),
@StoredProcedureParameter(name = "data", type = Car.class, mode = ParameterMode.REF_CURSOR) }),
@NamedStoredProcedureQuery(name = "findByYearProcedure", procedureName = "FIND_CAR_BY_YEAR", resultClasses = { Car.class }, parameters = { @StoredProcedureParameter(name = "p_year", type = Integer.class, mode = ParameterMode.IN),
@StoredProcedureParameter(name = "data", type = Car.class, mode = ParameterMode.REF_CURSOR) }) })
public class Car {
private long id;
private String model;
private Integer year;
public Car(String model, Integer year) {
this.model = model;
this.year = year;
}
public Car() {
}
@Id
@SequenceGenerator(name = "CarIdSequence", sequenceName = "SEQ_CAR_ID", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CarIdSequence")
@Column(name = "ID", unique = true, nullable = false, scale = 0)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "MODEL")
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Column(name = "YEAR")
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.jpa.model;
import java.util.HashMap;
import java.util.Map;
public class QueryParameter {
private Map<String, Object> parameters = null;
private QueryParameter(String name, Object value) {
this.parameters = new HashMap<>();
this.parameters.put(name, value);
}
public static QueryParameter with(String name, Object value) {
return new QueryParameter(name, value);
}
public QueryParameter and(String name, Object value) {
this.parameters.put(name, value);
return this;
}
public Map<String, Object> parameters() {
return this.parameters;
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/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_1_0.xsd"
version="1.0">
<persistence-unit name="jpa-db">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.model.Car</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@//127.0.0.1:1521/xe" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.connection.username" value="JPA" />
<property name="hibernate.connection.password" value="JPA" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,34 @@
CREATE TABLESPACE JPA DATAFILE 'C:\oraclexe\app\oracle\oradata\XE\JPA.DBF' SIZE 100M AUTOEXTEND ON MAXSIZE 2048M;
--DROP USER JPA CASCADE;
CREATE USER JPA IDENTIFIED BY JPA DEFAULT TABLESPACE JPA;
ALTER USER JPA QUOTA UNLIMITED ON "JPA" ACCOUNT UNLOCK;
GRANT CREATE SESSION TO "JPA";
GRANT ALTER SESSION TO "JPA";
GRANT CREATE TABLE TO "JPA";
GRANT CREATE TRIGGER TO "JPA";
GRANT CREATE VIEW TO "JPA";
GRANT CREATE DIMENSION TO "JPA";
GRANT CREATE CLUSTER TO "JPA";
GRANT CREATE INDEXTYPE TO "JPA";
GRANT CREATE ROLE TO "JPA";
GRANT CREATE SEQUENCE TO "JPA";
GRANT CREATE TYPE TO "JPA";
GRANT CREATE MATERIALIZED VIEW TO "JPA";
GRANT CREATE PROCEDURE TO "JPA";
GRANT CREATE SYNONYM TO "JPA";
CREATE SEQUENCE SEQ_CAR_ID INCREMENT BY 1 START WITH 1 MAXVALUE 999999999999999 MINVALUE 1;
CREATE TABLE CAR
(
ID NUMBER NOT NULL,
MODEL VARCHAR2(50) NOT NULL,
YEAR NUMBER(4) NOT NULL
);
ALTER TABLE CAR ADD CONSTRAINT CAR_PK PRIMARY KEY ( ID );
commit;

View File

@ -0,0 +1,4 @@
INSERT INTO CAR (ID, MODEL, YEAR) VALUES ('123456', 'Camaro', '2012');
INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('12112', 'Fiat Panda', '2000')
INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('111000', 'Fiat Punto', '2007')
INSERT INTO "JPA"."CAR" (ID, MODEL, YEAR) VALUES ('3382', 'Citroen C3', '2009')

View File

@ -0,0 +1,66 @@
package com.baeldung.jpa.storedprocedure;
import com.baeldung.jpa.model.Car;
import org.junit.*;
import javax.persistence.*;
/**
* Created by Giuseppe Bueti on 23/02/2016.
*/
public class StoredProcedureTest {
private static EntityManagerFactory factory = null;
private static EntityManager entityManager = null;
@BeforeClass
public static void init() {
factory = Persistence.createEntityManagerFactory("jpa-db");
entityManager = factory.createEntityManager();
}
@Before
public void setup() {
}
@Test
public void createCarTest() {
EntityTransaction transaction = entityManager.getTransaction();
try {
transaction.begin();
Car car = new Car("Ford Mustang", 2015);
entityManager.persist(car);
transaction.commit();
} catch (Exception e) {
System.out.println(e.getCause());
if (transaction.isActive()) {
transaction.rollback();
}
}
}
@Test
public void findCarsByYear() {
final StoredProcedureQuery findByYearProcedure = entityManager.createNamedStoredProcedureQuery("findByYearProcedure");
StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015);
storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
}
@Test
public void findCarsByModel() {
final StoredProcedureQuery findByModelProcedure = entityManager.createNamedStoredProcedureQuery("findByModelProcedure");
StoredProcedureQuery storedProcedure = findByModelProcedure.setParameter("p_model", "Camaro");
storedProcedure.getResultList().forEach(c -> Assert.assertEquals("Camaro", ((Car) c).getModel()));
}
@AfterClass
public static void destroy() {
if (entityManager != null) {
entityManager.close();
}
if (factory != null) {
factory.close();
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/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_1_0.xsd"
version="1.0">
<persistence-unit name="jpa-db">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.model.Car</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:@//127.0.0.1:1521/xe" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.connection.username" value="JPA" />
<property name="hibernate.connection.password" value="JPA" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>