Skip to content

Instantly share code, notes, and snippets.

@luiswolff
Last active April 9, 2017 16:04
Show Gist options
  • Save luiswolff/2221538da0dc167138d872dd0a2d7710 to your computer and use it in GitHub Desktop.
Save luiswolff/2221538da0dc167138d872dd0a2d7710 to your computer and use it in GitHub Desktop.
Using JPA an Construtor-Result to call a stored procedure and map the result to an intern domain model
package de.luiswolff.test;
import javax.persistence.*;
import java.util.List;
/**
* This program uses the MySQL sample database "world" and a stored procedure from the MySQL-Tutorial
* <a href="https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-stored-procedures.html">
* Connector/Net 4.1.5 Working with Stored Procedures</a>.
*/
public class CallEuropeanHOSWithSQLMapping {
public static void main(String[] args){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test-jpa");
EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
try {
et.begin();
List results = em.createStoredProcedureQuery("country_hos", "CountryHosResult")
.registerStoredProcedureParameter(1, String.class, ParameterMode.IN)
.setParameter(1, "Europe")
.getResultList();
for (Object o : results){
if (o instanceof CountryHos){
CountryHos countryHos = (CountryHos) o;
System.out.println("Country name: " + countryHos.getName() + " --> Head of state: " + countryHos.getHeadOfState());
} else {
System.err.println("received unknown type " + o.getClass().getName());
}
}
et.commit();
} catch (Exception e){
e.printStackTrace();
et.rollback();
}
em.close();
emf.close();
}
public static class CountryHos {
private final String name;
private final String headOfState;
public CountryHos(String name, String headOfState){
this.name = name;
this.headOfState = headOfState;
}
String getName() {
return name;
}
String getHeadOfState() {
return headOfState;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm
http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
<sql-result-set-mapping name="CountryHosResult">
<constructor-result target-class="de.luiswolff.test.CallEuropeanHOSWithSQLMapping$CountryHos">
<column name="Name" />
<column name="HeadOfState" />
</constructor-result>
</sql-result-set-mapping>
</entity-mappings>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="test-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tutorial?noAccessToProcedureBodies=true"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
<?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>de.luiswolff.test</groupId>
<artifactId>TestStoredProceduresJPA</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.9.Final</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment