Merge pull request #481 from GuenHamza/master
BAEL-63 - Add Stored Procedures with Hibernate Tests
This commit is contained in:
commit
5733676887
@ -8,7 +8,7 @@
|
|||||||
- [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination)
|
- [Hibernate Pagination](http://www.baeldung.com/hibernate-pagination)
|
||||||
- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort)
|
- [Sorting with Hibernate](http://www.baeldung.com/hibernate-sort)
|
||||||
- [Auditing with JPA, Hibernate, and Spring Data JPA](http://www.baeldung.com/database-auditing-jpa)
|
- [Auditing with JPA, Hibernate, and Spring Data JPA](http://www.baeldung.com/database-auditing-jpa)
|
||||||
|
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
|
||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
|
@ -11,93 +11,97 @@ import javax.persistence.GenerationType;
|
|||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.NamedNativeQueries;
|
||||||
|
import javax.persistence.NamedNativeQuery;
|
||||||
|
|
||||||
import org.hibernate.envers.Audited;
|
import org.hibernate.envers.Audited;
|
||||||
|
|
||||||
|
@NamedNativeQueries({
|
||||||
|
@NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class),
|
||||||
|
@NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) })
|
||||||
@Entity
|
@Entity
|
||||||
@Audited
|
@Audited
|
||||||
// @Proxy(lazy = false)
|
// @Proxy(lazy = false)
|
||||||
public class Foo implements Serializable {
|
public class Foo implements Serializable {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
@Column(name = "id")
|
@Column(name = "id")
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
@JoinColumn(name = "BAR_ID")
|
@JoinColumn(name = "BAR_ID")
|
||||||
private Bar bar = new Bar();
|
private Bar bar = new Bar();
|
||||||
|
|
||||||
public Foo() {
|
public Foo() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Foo(final String name) {
|
public Foo(final String name) {
|
||||||
super();
|
super();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
public Bar getBar() {
|
public Bar getBar() {
|
||||||
return bar;
|
return bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBar(final Bar bar) {
|
public void setBar(final Bar bar) {
|
||||||
this.bar = bar;
|
this.bar = bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(final long id) {
|
public void setId(final long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(final String name) {
|
public void setName(final String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object obj) {
|
public boolean equals(final Object obj) {
|
||||||
if (this == obj)
|
if (this == obj)
|
||||||
return true;
|
return true;
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
return false;
|
return false;
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
final Foo other = (Foo) obj;
|
final Foo other = (Foo) obj;
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
if (other.name != null)
|
if (other.name != null)
|
||||||
return false;
|
return false;
|
||||||
} else if (!name.equals(other.name))
|
} else if (!name.equals(other.name))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
final StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append("Foo [name=").append(name).append("]");
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("Foo [name=").append(name).append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
20
spring-hibernate4/src/main/resources/stored_procedure.sql
Normal file
20
spring-hibernate4/src/main/resources/stored_procedure.sql
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
DELIMITER //
|
||||||
|
CREATE PROCEDURE GetFoosByName(IN fooName VARCHAR(255))
|
||||||
|
LANGUAGE SQL
|
||||||
|
DETERMINISTIC
|
||||||
|
SQL SECURITY DEFINER
|
||||||
|
BEGIN
|
||||||
|
SELECT * FROM foo WHERE name = fooName;
|
||||||
|
END //
|
||||||
|
DELIMITER ;
|
||||||
|
|
||||||
|
|
||||||
|
DELIMITER //
|
||||||
|
CREATE PROCEDURE GetAllFoos()
|
||||||
|
LANGUAGE SQL
|
||||||
|
DETERMINISTIC
|
||||||
|
SQL SECURITY DEFINER
|
||||||
|
BEGIN
|
||||||
|
SELECT * FROM foo;
|
||||||
|
END //
|
||||||
|
DELIMITER ;
|
@ -0,0 +1,128 @@
|
|||||||
|
package com.baeldung.persistence.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.hibernate.Query;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.exception.SQLGrammarException;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Assume;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
import com.baeldung.persistence.model.Foo;
|
||||||
|
import com.baeldung.spring.PersistenceConfig;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class FooStoredProceduresIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IFooService fooService;
|
||||||
|
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public final void before() {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
Assume.assumeTrue(getAllFoosExists());
|
||||||
|
Assume.assumeTrue(getFoosByNameExists());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean getFoosByNameExists() {
|
||||||
|
try {
|
||||||
|
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
|
||||||
|
.addEntity(Foo.class);
|
||||||
|
sqlQuery.list();
|
||||||
|
return true;
|
||||||
|
} catch (SQLGrammarException e) {
|
||||||
|
System.out
|
||||||
|
.println("WARNING : GetFoosByName() Procedure is may be missing ");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean getAllFoosExists() {
|
||||||
|
try {
|
||||||
|
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()")
|
||||||
|
.addEntity(Foo.class);
|
||||||
|
sqlQuery.list();
|
||||||
|
return true;
|
||||||
|
} catch (SQLGrammarException e) {
|
||||||
|
System.out
|
||||||
|
.println("WARNING : GetAllFoos() Procedure is may be missing ");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public final void after() {
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void getAllFoosUsingStoredProcedures() {
|
||||||
|
|
||||||
|
fooService.create(new Foo(randomAlphabetic(6)));
|
||||||
|
|
||||||
|
// Stored procedure getAllFoos using createSQLQuery
|
||||||
|
Query sqlQuery = session.createSQLQuery("CALL GetAllFoos()").addEntity(
|
||||||
|
Foo.class);
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Foo> allFoos = sqlQuery.list();
|
||||||
|
for (Foo foo : allFoos) {
|
||||||
|
System.out.println("getAllFoos() SQL Query result : "
|
||||||
|
+ foo.getName());
|
||||||
|
}
|
||||||
|
assertEquals(allFoos.size(), fooService.findAll().size());
|
||||||
|
|
||||||
|
// Stored procedure getAllFoos using a Named Query
|
||||||
|
Query namedQuery = session.getNamedQuery("callGetAllFoos");
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Foo> allFoos2 = namedQuery.list();
|
||||||
|
for (Foo foo : allFoos2) {
|
||||||
|
System.out.println("getAllFoos() NamedQuery result : "
|
||||||
|
+ foo.getName());
|
||||||
|
}
|
||||||
|
assertEquals(allFoos2.size(), fooService.findAll().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void getFoosByNameUsingStoredProcedures() {
|
||||||
|
|
||||||
|
fooService.create(new Foo("NewFooName"));
|
||||||
|
|
||||||
|
// Stored procedure getFoosByName using createSQLQuery()
|
||||||
|
Query sqlQuery = session.createSQLQuery("CALL GetFoosByName(:fooName)")
|
||||||
|
.addEntity(Foo.class).setParameter("fooName", "NewFooName");
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Foo> allFoosByName = sqlQuery.list();
|
||||||
|
for (Foo foo : allFoosByName) {
|
||||||
|
System.out.println("getFoosByName() using SQL Query : found => "
|
||||||
|
+ foo.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stored procedure getFoosByName using getNamedQuery()
|
||||||
|
Query namedQuery = session.getNamedQuery("callGetFoosByName")
|
||||||
|
.setParameter("fooName", "NewFooName");
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Foo> allFoosByName2 = namedQuery.list();
|
||||||
|
for (Foo foo : allFoosByName2) {
|
||||||
|
System.out.println("getFoosByName() using Native Query : found => "
|
||||||
|
+ foo.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user