commit
1657b40ff5
|
@ -0,0 +1,96 @@
|
|||
<?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>
|
||||
|
||||
<!-- Postgres JDBC -->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>9.4.1208</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MySql JDBC -->
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.38</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>
|
|
@ -0,0 +1,60 @@
|
|||
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 = "data", type = Car.class, mode = ParameterMode.REF_CURSOR),
|
||||
@StoredProcedureParameter(name = "p_model", type = String.class, mode = ParameterMode.IN) }),
|
||||
@NamedStoredProcedureQuery(name = "findByYearProcedure", procedureName = "FIND_CAR_BY_YEAR", resultClasses = { Car.class }, parameters = { @StoredProcedureParameter(name = "data", type = Car.class, mode = ParameterMode.REF_CURSOR),
|
||||
@StoredProcedureParameter(name = "p_year", type = Integer.class, mode = ParameterMode.IN) }) })
|
||||
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")
|
||||
//@GeneratedValue(strategy = GenerationType.IDENTITY) -- for MySQL
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
||||
version="2.1">
|
||||
|
||||
<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>
|
||||
|
||||
<!-- MySQL properties configuration -->
|
||||
<!--properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/jpa" />
|
||||
<property name="javax.persistence.jdbc.user" value="jpa" />
|
||||
<property name="javax.persistence.jdbc.password" value="bagnara.82" />
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
|
||||
<property name="hibernate.show_sql" value="true" />
|
||||
</properties-->
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -0,0 +1,8 @@
|
|||
DELIMITER $$
|
||||
CREATE DEFINER=`root`@`localhost` PROCEDURE `FIND_CAR_BY_YEAR`(in p_year int)
|
||||
begin
|
||||
SELECT ID, MODEL, YEAR
|
||||
FROM CAR
|
||||
WHERE YEAR = p_year;
|
||||
end$$
|
||||
DELIMITER ;
|
|
@ -0,0 +1,7 @@
|
|||
create or replace PROCEDURE FIND_CAR_BY_YEAR ( p_year IN NUMBER, data OUT SYS_REFCURSOR ) AS
|
||||
BEGIN
|
||||
OPEN data FOR
|
||||
SELECT ID, MODEL, YEAR
|
||||
FROM CAR
|
||||
WHERE YEAR = p_year;
|
||||
END FIND_CAR_BY_YEAR;
|
|
@ -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;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE `car` (
|
||||
`ID` int(10) NOT NULL AUTO_INCREMENT,
|
||||
`MODEL` varchar(50) NOT NULL,
|
||||
`YEAR` int(4) NOT NULL,
|
||||
PRIMARY KEY (`ID`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
|
|
@ -0,0 +1,5 @@
|
|||
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');
|
||||
commit;
|
|
@ -0,0 +1,85 @@
|
|||
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("Fiat Punto", 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()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCarsByYearNoNamedStored() {
|
||||
StoredProcedureQuery findByYearProcedure =
|
||||
entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class)
|
||||
.registerStoredProcedureParameter("data", Void.class, ParameterMode.REF_CURSOR)
|
||||
.registerStoredProcedureParameter("p_year", Integer.class, ParameterMode.IN)
|
||||
.setParameter("p_year", 2015);
|
||||
|
||||
findByYearProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
|
||||
}
|
||||
|
||||
@Test @Ignore
|
||||
public void findCarsByYearMySql() {
|
||||
StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR",Car.class)
|
||||
.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN)
|
||||
.setParameter(1, 2015);
|
||||
storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
|
||||
if (entityManager != null) {
|
||||
entityManager.close();
|
||||
}
|
||||
if (factory != null) {
|
||||
factory.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
||||
version="2.1">
|
||||
|
||||
<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>
|
||||
|
||||
<!-- MySQL properties configuration -->
|
||||
<!--properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/jpa" />
|
||||
<property name="javax.persistence.jdbc.user" value="jpa" />
|
||||
<property name="javax.persistence.jdbc.password" value="bagnara.82" />
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
|
||||
<property name="hibernate.show_sql" value="true" />
|
||||
</properties-->
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
Loading…
Reference in New Issue