Added setup and code for MySQL DB
This commit is contained in:
parent
2478619202
commit
d7524f9501
|
@ -62,6 +62,21 @@
|
|||
<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>
|
||||
|
|
|
@ -9,10 +9,10 @@ import javax.persistence.*;
|
|||
@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) }) })
|
||||
@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;
|
||||
|
@ -30,6 +30,7 @@ public class 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;
|
||||
|
|
|
@ -16,5 +16,15 @@
|
|||
<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,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;
|
|
@ -28,7 +28,7 @@ public class StoredProcedureTest {
|
|||
EntityTransaction transaction = entityManager.getTransaction();
|
||||
try {
|
||||
transaction.begin();
|
||||
Car car = new Car("Ford Mustang", 2015);
|
||||
Car car = new Car("Fiat Punto", 2015);
|
||||
entityManager.persist(car);
|
||||
transaction.commit();
|
||||
} catch (Exception e) {
|
||||
|
@ -57,12 +57,21 @@ public class StoredProcedureTest {
|
|||
public void findCarsByYearNoNamedStored() {
|
||||
StoredProcedureQuery findByYearProcedure =
|
||||
entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class)
|
||||
.registerStoredProcedureParameter("p_year", Integer.class, ParameterMode.IN)
|
||||
.registerStoredProcedureParameter("data", Void.class, ParameterMode.REF_CURSOR).setParameter("p_year", 2015);
|
||||
.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() {
|
||||
|
||||
|
|
|
@ -16,6 +16,16 @@
|
|||
<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