* BAEL-2806

Article companion code.

* BAEL-2806-Rev1

Added integration tests, improved examples and fixed an error.
This commit is contained in:
rodolforfq 2019-03-31 10:48:49 -04:00 committed by maibin
parent 1c82623201
commit 0b9a5b3d04
5 changed files with 216 additions and 19 deletions

View File

@ -0,0 +1,69 @@
package com.baeldung.jpa.querytypes;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
/**
* JPA Query Types examples. All using the UserEntity class.
*
* @author Rodolfo Felipe
*/
public class QueryTypesExamples {
EntityManagerFactory emf;
public QueryTypesExamples() {
Map properties = new HashMap();
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
emf = Persistence.createEntityManagerFactory("jpa-query-types", properties);
}
private EntityManager getEntityManager() {
return emf.createEntityManager();
}
public UserEntity getUserByIdWithPlainQuery(Long id) {
Query jpqlQuery = getEntityManager().createQuery("SELECT u FROM UserEntity u WHERE u.id=:id");
jpqlQuery.setParameter("id", id);
return (UserEntity) jpqlQuery.getSingleResult();
}
public UserEntity getUserByIdWithTypedQuery(Long id) {
TypedQuery<UserEntity> typedQuery = getEntityManager().createQuery("SELECT u FROM UserEntity u WHERE u.id=:id", UserEntity.class);
typedQuery.setParameter("id", id);
return typedQuery.getSingleResult();
}
public UserEntity getUserByIdWithNamedQuery(Long id) {
Query namedQuery = getEntityManager().createNamedQuery("UserEntity.findByUserId");
namedQuery.setParameter("userId", id);
return (UserEntity) namedQuery.getSingleResult();
}
public UserEntity getUserByIdWithNativeQuery(Long id) {
Query nativeQuery = getEntityManager().createNativeQuery("SELECT * FROM users WHERE id=:userId", UserEntity.class);
nativeQuery.setParameter("userId", id);
return (UserEntity) nativeQuery.getSingleResult();
}
public UserEntity getUserByIdWithCriteriaQuery(Long id) {
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<UserEntity> criteriaQuery = criteriaBuilder.createQuery(UserEntity.class);
Root<UserEntity> userRoot = criteriaQuery.from(UserEntity.class);
UserEntity queryResult = getEntityManager().createQuery(criteriaQuery.select(userRoot)
.where(criteriaBuilder.equal(userRoot.get("id"), id)))
.getSingleResult();
return queryResult;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.jpa.querytypes;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* User entity class. Used as an asset for JPA Query Types examples.
*
* @author Rodolfo Felipe
*/
@Table(name = "users")
@Entity
@NamedQuery(name = "UserEntity.findByUserId", query = "SELECT u FROM UserEntity u WHERE u.id=:userId")
public class UserEntity {
@Id
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -89,24 +89,45 @@
</persistence-unit>
<persistence-unit name="jpa-h2-criteria">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.criteria.entity.Item</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source" value="item.sql"/>
</properties>
</persistence-unit>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.criteria.entity.Item</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source" value="item.sql"/>
</properties>
</persistence-unit>
<persistence-unit name="jpa-query-types">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.querytypes.UserEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
<property name="javax.persistence.sql-load-script-source" value="users.sql"/>
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,65 @@
package com.baeldung.jpa.querytypes;
import org.junit.Assert;
import org.junit.Test;
/**
* QueryTypesExamples class integration tests.
*
* @author Rodolfo Felipe
*/
public class QueryTypesExamplesIntegrationTest {
QueryTypesExamples userDao = new QueryTypesExamples();
@Test
public void givenUserId_whenCallingPlaingQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithPlainQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithPlainQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingTypedQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithTypedQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithTypedQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingNamedQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithNamedQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithNamedQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingNativeQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithNativeQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithNativeQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingCriteriaApiMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithCriteriaQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithCriteriaQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
}

View File

@ -0,0 +1,4 @@
INSERT INTO users(id,name) VALUES(1,'baeldung');
INSERT INTO users(id,name) VALUES(2,'john doe');
INSERT INTO users(id,name) VALUES(3,'jane doe');
INSERT INTO users(id,name) VALUES(4,'batman');