minor cleanup work
This commit is contained in:
parent
9e3d7a3b22
commit
c11c10b10c
|
@ -1,23 +1,27 @@
|
|||
package com.baeldung.jpa.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* Created by Giuseppe Bueti on 22/02/2016.
|
||||
*/
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedStoredProcedureQueries;
|
||||
import javax.persistence.NamedStoredProcedureQuery;
|
||||
import javax.persistence.ParameterMode;
|
||||
import javax.persistence.StoredProcedureParameter;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "CAR")
|
||||
@NamedStoredProcedureQueries({
|
||||
@NamedStoredProcedureQuery(name = "findByYearProcedure", procedureName = "FIND_CAR_BY_YEAR", resultClasses = { Car.class },
|
||||
parameters = { @StoredProcedureParameter(name = "p_year", type = Integer.class, mode = ParameterMode.IN) }) })
|
||||
@NamedStoredProcedureQuery(name = "findByYearProcedure", procedureName = "FIND_CAR_BY_YEAR", resultClasses = { Car.class }, parameters = { @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) {
|
||||
public Car(final String model, final Integer year) {
|
||||
this.model = model;
|
||||
this.year = year;
|
||||
}
|
||||
|
@ -32,7 +36,7 @@ public class Car {
|
|||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -41,7 +45,7 @@ public class Car {
|
|||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
public void setModel(final String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
|
@ -50,7 +54,7 @@ public class Car {
|
|||
return year;
|
||||
}
|
||||
|
||||
public void setYear(Integer year) {
|
||||
public void setYear(final Integer year) {
|
||||
this.year = year;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,16 +7,16 @@ public class QueryParameter {
|
|||
|
||||
private Map<String, Object> parameters = null;
|
||||
|
||||
private QueryParameter(String name, Object value) {
|
||||
private QueryParameter(final String name, final Object value) {
|
||||
this.parameters = new HashMap<>();
|
||||
this.parameters.put(name, value);
|
||||
}
|
||||
|
||||
public static QueryParameter with(String name, Object value) {
|
||||
public static QueryParameter with(final String name, final Object value) {
|
||||
return new QueryParameter(name, value);
|
||||
}
|
||||
|
||||
public QueryParameter and(String name, Object value) {
|
||||
public QueryParameter and(final String name, final Object value) {
|
||||
this.parameters.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
@ -24,4 +24,5 @@ public class QueryParameter {
|
|||
public Map<String, Object> parameters() {
|
||||
return this.parameters;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,20 @@
|
|||
package com.baeldung.jpa.storedprocedure;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.ParameterMode;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.StoredProcedureQuery;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
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;
|
||||
|
@ -25,13 +32,13 @@ public class StoredProcedureTest {
|
|||
|
||||
@Test
|
||||
public void createCarTest() {
|
||||
EntityTransaction transaction = entityManager.getTransaction();
|
||||
final EntityTransaction transaction = entityManager.getTransaction();
|
||||
try {
|
||||
transaction.begin();
|
||||
Car car = new Car("Fiat Marea", 2015);
|
||||
final Car car = new Car("Fiat Marea", 2015);
|
||||
entityManager.persist(car);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
} catch (final Exception e) {
|
||||
System.out.println(e.getCause());
|
||||
if (transaction.isActive()) {
|
||||
transaction.rollback();
|
||||
|
@ -42,13 +49,13 @@ public class StoredProcedureTest {
|
|||
@Test
|
||||
public void findCarsByYearNamedProcedure() {
|
||||
final StoredProcedureQuery findByYearProcedure = entityManager.createNamedStoredProcedureQuery("findByYearProcedure");
|
||||
StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015);
|
||||
final StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015);
|
||||
storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findCarsByYearNoNamed() {
|
||||
StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class).registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN).setParameter(1, 2015);
|
||||
final 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()));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue