Merge branch 'master' into BAEL-17506
This commit is contained in:
@@ -4,18 +4,14 @@ This module contains articles about the Java Persistence API (JPA) in Java.
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [A Guide to SqlResultSetMapping](http://www.baeldung.com/jpa-sql-resultset-mapping)
|
||||
- [A Guide to Stored Procedures with JPA](http://www.baeldung.com/jpa-stored-procedures)
|
||||
- [A Guide to SqlResultSetMapping](https://www.baeldung.com/jpa-sql-resultset-mapping)
|
||||
- [A Guide to Stored Procedures with JPA](https://www.baeldung.com/jpa-stored-procedures)
|
||||
- [Fixing the JPA error “java.lang.String cannot be cast to Ljava.lang.String;”](https://www.baeldung.com/jpa-error-java-lang-string-cannot-be-cast)
|
||||
- [JPA Entity Graph](https://www.baeldung.com/jpa-entity-graph)
|
||||
- [JPA 2.2 Support for Java 8 Date/Time Types](https://www.baeldung.com/jpa-java-time)
|
||||
- [Converting Between LocalDate and SQL Date](https://www.baeldung.com/java-convert-localdate-sql-date)
|
||||
- [Combining JPA And/Or Criteria Predicates](https://www.baeldung.com/jpa-and-or-criteria-predicates)
|
||||
- [Types of JPA Queries](https://www.baeldung.com/jpa-queries)
|
||||
- [JPA/Hibernate Projections](https://www.baeldung.com/jpa-hibernate-projections)
|
||||
- [Composite Primary Keys in JPA](https://www.baeldung.com/jpa-composite-primary-keys)
|
||||
- [Defining JPA Entities](https://www.baeldung.com/jpa-entities)
|
||||
- [JPA @Basic Annotation](https://www.baeldung.com/jpa-basic-annotation)
|
||||
- [Default Column Values in JPA](https://www.baeldung.com/jpa-default-column-values)
|
||||
- [Persisting Enums in JPA](https://www.baeldung.com/jpa-persisting-enums-in-jpa)
|
||||
- More articles: [[next -->]](/java-jpa-2)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.util;
|
||||
package com.baeldung.jpa.convertdates;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.jpa.criteria.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Table(name = "item")
|
||||
@Entity
|
||||
public class Item {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String color;
|
||||
private String grade;
|
||||
private String name;
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public String getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public void setGrade(String grade) {
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.jpa.criteria.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.jpa.criteria.entity.Item;
|
||||
|
||||
public interface CustomItemRepository {
|
||||
|
||||
List<Item> findItemsByColorAndGrade();
|
||||
|
||||
List<Item> findItemByColorOrGrade();
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package com.baeldung.jpa.criteria.repository.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import com.baeldung.jpa.criteria.entity.Item;
|
||||
import com.baeldung.jpa.criteria.repository.CustomItemRepository;
|
||||
|
||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
|
||||
private EntityManager entityManager;
|
||||
|
||||
public CustomItemRepositoryImpl() {
|
||||
super();
|
||||
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-criteria");
|
||||
entityManager = factory.createEntityManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemsByColorAndGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForColor = criteriaBuilder.or(predicateForBlueColor, predicateForRedColor);
|
||||
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "A");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForGrade = criteriaBuilder.or(predicateForGradeA, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.and(predicateForColor, predicateForGrade);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemByColorOrGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "D");
|
||||
Predicate predicateForBlueColorAndGradeA = criteriaBuilder.and(predicateForBlueColor, predicateForGradeA);
|
||||
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForRedColorAndGradeB = criteriaBuilder.and(predicateForRedColor, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.or(predicateForBlueColorAndGradeA, predicateForRedColorAndGradeB);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.jpa.defaultvalues;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Column(columnDefinition = "varchar(255) default 'John Snow'")
|
||||
private String name = "John Snow";
|
||||
|
||||
@Column(columnDefinition = "integer default 25")
|
||||
private Integer age = 25;
|
||||
|
||||
@Column(columnDefinition = "boolean default false")
|
||||
private Boolean locked = false;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Boolean getLocked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
public void setLocked(Boolean locked) {
|
||||
this.locked = locked;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.jpa.defaultvalues;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class UserRepository {
|
||||
|
||||
private EntityManagerFactory emf = null;
|
||||
|
||||
public UserRepository() {
|
||||
emf = Persistence.createEntityManagerFactory("entity-default-values");
|
||||
}
|
||||
|
||||
public User find(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
User user = entityManager.find(User.class, id);
|
||||
entityManager.close();
|
||||
return user;
|
||||
}
|
||||
|
||||
public void save(User user, Long id) {
|
||||
user.setId(id);
|
||||
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(user);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
emf.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.util;
|
||||
package com.baeldung.jpa.entity;
|
||||
|
||||
public enum Gender {
|
||||
MALE,
|
||||
@@ -14,8 +14,6 @@ import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import com.baeldung.util.Gender;
|
||||
|
||||
@Entity
|
||||
@Table(name="STUDENT")
|
||||
public class Student {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
package com.baeldung.jpa.primarykeys;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
package com.baeldung.jpa.primarykeys;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
package com.baeldung.jpa.primarykeys;
|
||||
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
package com.baeldung.jpa.primarykeys;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.jpa.projections;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
@Id
|
||||
private long id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String category;
|
||||
private BigDecimal unitPrice;
|
||||
|
||||
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;
|
||||
}
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
public BigDecimal getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
public void setUnitPrice(BigDecimal unitPrice) {
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package com.baeldung.jpa.projections;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.Tuple;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
public class ProductRepository {
|
||||
private EntityManager entityManager;
|
||||
|
||||
public ProductRepository() {
|
||||
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-projections");
|
||||
entityManager = factory.createEntityManager();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> findAllNamesUsingJPQL() {
|
||||
Query query = entityManager.createQuery("select name from Product");
|
||||
List<Object> resultList = query.getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> findAllIdsUsingJPQL() {
|
||||
Query query = entityManager.createQuery("select id from Product");
|
||||
List<Object> resultList = query.getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public List<String> findAllNamesUsingCriteriaBuilder() {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<String> query = builder.createQuery(String.class);
|
||||
Root<Product> product = query.from(Product.class);
|
||||
query.select(product.get("name"));
|
||||
List<String> resultList = entityManager.createQuery(query).getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object[]> findAllIdAndNamesUsingJPQL() {
|
||||
Query query = entityManager.createQuery("select id, name from Product");
|
||||
List<Object[]> resultList = query.getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public List<Object[]> findAllIdAndNamesUsingCriteriaBuilderArray() {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
|
||||
Root<Product> product = query.from(Product.class);
|
||||
query.select(builder.array(product.get("id"), product.get("name")));
|
||||
List<Object[]> resultList = entityManager.createQuery(query).getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public List<Object[]> findAllIdNameUnitPriceUsingCriteriaQueryMultiselect() {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
|
||||
Root<Product> product = query.from(Product.class);
|
||||
query.multiselect(product.get("id"), product.get("name"), product.get("unitPrice"));
|
||||
List<Object[]> resultList = entityManager.createQuery(query).getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public List<Tuple> findAllIdAndNamesUsingCriteriaBuilderTuple() {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Tuple> query = builder.createQuery(Tuple.class);
|
||||
Root<Product> product = query.from(Product.class);
|
||||
query.select(builder.tuple(product.get("id"), product.get("name")));
|
||||
List<Tuple> resultList = entityManager.createQuery(query).getResultList();
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public List<Object[]> findCountByCategoryUsingJPQL() {
|
||||
Query query = entityManager.createQuery("select p.category, count(p) from Product p group by p.category");
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Object[]> findCountByCategoryUsingCriteriaBuilder() {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
|
||||
Root<Product> product = query.from(Product.class);
|
||||
query.multiselect(product.get("category"), builder.count(product));
|
||||
query.groupBy(product.get("category"));
|
||||
List<Object[]> resultList = entityManager.createQuery(query).getResultList();
|
||||
return resultList;
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sqlresultsetmapping;
|
||||
package com.baeldung.jpa.sqlresultsetmapping;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@@ -7,7 +7,7 @@ import javax.persistence.*;
|
||||
name="EmployeeResult",
|
||||
entities={
|
||||
@EntityResult(
|
||||
entityClass = com.baeldung.sqlresultsetmapping.Employee.class,
|
||||
entityClass = com.baeldung.jpa.sqlresultsetmapping.Employee.class,
|
||||
fields={@FieldResult(name="id",column="employeeNumber"),
|
||||
@FieldResult(name="name", column="name")}
|
||||
)
|
||||
@@ -1,18 +1,18 @@
|
||||
package com.baeldung.sqlresultsetmapping;
|
||||
package com.baeldung.jpa.sqlresultsetmapping;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@SqlResultSetMappings(value = {
|
||||
@SqlResultSetMapping(name = "ScheduleResult",
|
||||
classes = { @ConstructorResult(targetClass = com.baeldung.sqlresultsetmapping.ScheduledDay.class,
|
||||
classes = { @ConstructorResult(targetClass = com.baeldung.jpa.sqlresultsetmapping.ScheduledDay.class,
|
||||
columns = { @ColumnResult(name = "id", type = Long.class),
|
||||
@ColumnResult(name = "employeeId", type = Long.class),
|
||||
@ColumnResult(name = "dayOfWeek") }) }),
|
||||
@SqlResultSetMapping(name = "FridayEmployeeResult",
|
||||
columns = { @ColumnResult(name = "employeeId") }),
|
||||
@SqlResultSetMapping(name = "EmployeeScheduleResults",
|
||||
entities = { @EntityResult(entityClass = com.baeldung.sqlresultsetmapping.Employee.class),
|
||||
@EntityResult(entityClass = com.baeldung.sqlresultsetmapping.ScheduledDay.class)
|
||||
entities = { @EntityResult(entityClass = com.baeldung.jpa.sqlresultsetmapping.Employee.class),
|
||||
@EntityResult(entityClass = com.baeldung.jpa.sqlresultsetmapping.ScheduledDay.class)
|
||||
}) })
|
||||
@NamedNativeQuery(name = "FridayEmployees",
|
||||
query = "SELECT employeeId FROM schedule_days WHERE dayOfWeek = 'FRIDAY'",
|
||||
@@ -7,8 +7,8 @@
|
||||
|
||||
<persistence-unit name="java-jpa-scheduled-day">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.sqlresultsetmapping.ScheduledDay</class>
|
||||
<class>com.baeldung.sqlresultsetmapping.Employee</class>
|
||||
<class>com.baeldung.jpa.sqlresultsetmapping.ScheduledDay</class>
|
||||
<class>com.baeldung.jpa.sqlresultsetmapping.Employee</class>
|
||||
<class>com.baeldung.jpa.basicannotation.Course</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
@@ -115,77 +115,13 @@
|
||||
</properties>
|
||||
</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>
|
||||
|
||||
<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-unit name="entity-default-values">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.defaultvalues.User</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" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="jpa-entity-definition">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.entity.Student</class>
|
||||
<class>com.baeldung.jpa.entity.Book</class>
|
||||
<class>com.baeldung.jpa.entity.BookId</class>
|
||||
<class>com.baeldung.jpa.entity.Account</class>
|
||||
<class>com.baeldung.jpa.entity.AccountId</class>
|
||||
<class>com.baeldung.jpa.primarykeys.Book</class>
|
||||
<class>com.baeldung.jpa.primarykeys.BookId</class>
|
||||
<class>com.baeldung.jpa.primarykeys.Account</class>
|
||||
<class>com.baeldung.jpa.primarykeys.AccountId</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver"
|
||||
@@ -203,25 +139,4 @@
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="jpa-projections">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.projections.Product</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="products_jpa.sql" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
@@ -1,4 +0,0 @@
|
||||
insert into product(id, name, description, category) values (1,'Product Name 1','This is Product 1', 'category1');
|
||||
insert into product(id, name, description, category) values (2,'Product Name 2','This is Product 2', 'category1');
|
||||
insert into product(id, name, description, category) values (3,'Product Name 3','This is Product 3', 'category2');
|
||||
insert into product(id, name, description, category) values (4,'Product Name 4','This is Product 4', 'category3');
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.jpa.criteria.repository.impl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jpa.criteria.entity.Item;
|
||||
import com.baeldung.jpa.criteria.repository.CustomItemRepository;
|
||||
|
||||
public class CustomItemRepositoryIntegrationTest {
|
||||
|
||||
CustomItemRepository customItemRepository = new CustomItemRepositoryImpl();
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemsByColorAndGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepository.findItemsByColorAndGrade();
|
||||
|
||||
assertFalse("No items found", items.isEmpty());
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have blue color", "blue", item.getColor());
|
||||
assertEquals("this item does not belong to A grade", "A", item.getGrade());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemByColorOrGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepository.findItemByColorOrGrade();
|
||||
|
||||
assertFalse("No items found", items.isEmpty());
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have red color", "red", item.getColor());
|
||||
assertEquals("this item does not belong to D grade", "D", item.getGrade());
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.jpa.defaultvalues;
|
||||
|
||||
import com.baeldung.jpa.defaultvalues.User;
|
||||
import com.baeldung.jpa.defaultvalues.UserRepository;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class UserDefaultValuesUnitTest {
|
||||
|
||||
private static UserRepository userRepository = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void once() {
|
||||
userRepository = new UserRepository();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // SQL default values are also defined
|
||||
public void saveUser_shouldSaveWithDefaultFieldValues() {
|
||||
User user = new User();
|
||||
userRepository.save(user, 1L);
|
||||
|
||||
user = userRepository.find(1L);
|
||||
assertEquals(user.getName(), "John Snow");
|
||||
assertEquals(25, (int) user.getAge());
|
||||
assertFalse(user.getLocked());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // SQL default values are also defined
|
||||
public void saveUser_shouldSaveWithNullName() {
|
||||
User user = new User();
|
||||
user.setName(null);
|
||||
userRepository.save(user, 2L);
|
||||
|
||||
user = userRepository.find(2L);
|
||||
assertNull(user.getName());
|
||||
assertEquals(25, (int) user.getAge());
|
||||
assertFalse(user.getLocked());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveUser_shouldSaveWithDefaultSqlValues() {
|
||||
User user = new User();
|
||||
userRepository.save(user, 3L);
|
||||
|
||||
user = userRepository.find(3L);
|
||||
assertEquals(user.getName(), "John Snow");
|
||||
assertEquals(25, (int) user.getAge());
|
||||
assertFalse(user.getLocked());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
userRepository.clean();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,8 +16,6 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.util.Gender;
|
||||
|
||||
public class StudentEntityIntegrationTest {
|
||||
|
||||
private EntityManagerFactory emf;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
package com.baeldung.jpa.primarykeys;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -7,6 +7,10 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import com.baeldung.jpa.primarykeys.Account;
|
||||
import com.baeldung.jpa.primarykeys.AccountId;
|
||||
import com.baeldung.jpa.primarykeys.Book;
|
||||
import com.baeldung.jpa.primarykeys.BookId;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
@@ -1,133 +0,0 @@
|
||||
package com.baeldung.jpa.projections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.hibernate.criterion.Projections;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HibernateProjectionsIntegrationTest {
|
||||
private static Session session;
|
||||
private static SessionFactory sessionFactory;
|
||||
private Transaction transaction;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
Configuration configuration = getConfiguration();
|
||||
configuration.addAnnotatedClass(Product.class);
|
||||
sessionFactory = configuration.buildSessionFactory();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
session = sessionFactory.getCurrentSession();
|
||||
transaction = session.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
if(transaction.isActive()) {
|
||||
transaction.rollback();
|
||||
}
|
||||
}
|
||||
|
||||
private static Configuration getConfiguration() {
|
||||
Configuration cfg = new Configuration();
|
||||
cfg.setProperty(AvailableSettings.DIALECT,
|
||||
"org.hibernate.dialect.H2Dialect");
|
||||
cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "none");
|
||||
cfg.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
|
||||
cfg.setProperty(AvailableSettings.URL,
|
||||
"jdbc:h2:mem:myexceptiondb2;DB_CLOSE_DELAY=-1;;INIT=RUNSCRIPT FROM 'src/test/resources/products.sql'");
|
||||
cfg.setProperty(AvailableSettings.USER, "sa");
|
||||
cfg.setProperty(AvailableSettings.PASS, "");
|
||||
cfg.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
||||
return cfg;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void givenProductData_whenIdAndNameProjectionUsingCriteria_thenListOfObjectArrayReturned() {
|
||||
Criteria criteria = session.createCriteria(Product.class);
|
||||
criteria = criteria.setProjection(Projections.projectionList()
|
||||
.add(Projections.id())
|
||||
.add(Projections.property("name")));
|
||||
List<Object[]> resultList = criteria.list();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(4, resultList.size());
|
||||
assertEquals(1L, resultList.get(0)[0]);
|
||||
assertEquals("Product Name 1", resultList.get(0)[1]);
|
||||
assertEquals(2L, resultList.get(1)[0]);
|
||||
assertEquals("Product Name 2", resultList.get(1)[1]);
|
||||
assertEquals(3L, resultList.get(2)[0]);
|
||||
assertEquals("Product Name 3", resultList.get(2)[1]);
|
||||
assertEquals(4L, resultList.get(3)[0]);
|
||||
assertEquals("Product Name 4", resultList.get(3)[1]);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenNameProjectionUsingCriteria_thenListOfStringReturned() {
|
||||
Criteria criteria = session.createCriteria(Product.class);
|
||||
criteria = criteria.setProjection(Projections.property("name"));
|
||||
List resultList = criteria.list();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(4, resultList.size());
|
||||
assertEquals("Product Name 1", resultList.get(0));
|
||||
assertEquals("Product Name 2", resultList.get(1));
|
||||
assertEquals("Product Name 3", resultList.get(2));
|
||||
assertEquals("Product Name 4", resultList.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenCountByCategoryUsingCriteria_thenOK() {
|
||||
Criteria criteria = session.createCriteria(Product.class);
|
||||
criteria = criteria.setProjection(Projections.projectionList()
|
||||
.add(Projections.groupProperty("category"))
|
||||
.add(Projections.rowCount()));
|
||||
List<Object[]> resultList = criteria.list();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(3, resultList.size());
|
||||
assertEquals("category1", resultList.get(0)[0]);
|
||||
assertEquals(2L, resultList.get(0)[1]);
|
||||
assertEquals("category2", resultList.get(1)[0]);
|
||||
assertEquals(1L, resultList.get(1)[1]);
|
||||
assertEquals("category3", resultList.get(2)[0]);
|
||||
assertEquals(1L, resultList.get(2)[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenCountByCategoryWithAliasUsingCriteria_thenOK() {
|
||||
Criteria criteria = session.createCriteria(Product.class);
|
||||
criteria = criteria.setProjection(Projections.projectionList()
|
||||
.add(Projections.groupProperty("category"))
|
||||
.add(Projections.alias(Projections.rowCount(), "count")));
|
||||
criteria.addOrder(Order.asc("count"));
|
||||
List<Object[]> resultList = criteria.list();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(3, resultList.size());
|
||||
assertEquals("category2", resultList.get(0)[0]);
|
||||
assertEquals(1L, resultList.get(0)[1]);
|
||||
assertEquals("category3", resultList.get(1)[0]);
|
||||
assertEquals(1L, resultList.get(1)[1]);
|
||||
assertEquals("category1", resultList.get(2)[0]);
|
||||
assertEquals(2L, resultList.get(2)[1]);
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package com.baeldung.jpa.projections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ProductRepositoryIntegrationTest {
|
||||
private static ProductRepository productRepository;
|
||||
|
||||
@BeforeClass
|
||||
public static void once() throws IOException {
|
||||
productRepository = new ProductRepository();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenIdAndNameProjectionUsingJPQL_thenListOfObjectArrayReturned() {
|
||||
List<Object[]> resultList = productRepository.findAllIdAndNamesUsingJPQL();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(4, resultList.size());
|
||||
assertEquals(1L, resultList.get(0)[0]);
|
||||
assertEquals("Product Name 1", resultList.get(0)[1]);
|
||||
assertEquals(2L, resultList.get(1)[0]);
|
||||
assertEquals("Product Name 2", resultList.get(1)[1]);
|
||||
assertEquals(3L, resultList.get(2)[0]);
|
||||
assertEquals("Product Name 3", resultList.get(2)[1]);
|
||||
assertEquals(4L, resultList.get(3)[0]);
|
||||
assertEquals("Product Name 4", resultList.get(3)[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenIdAndNameProjectionUsingCriteriaBuilder_thenListOfObjectArrayReturned() {
|
||||
List<Object[]> resultList = productRepository.findAllIdAndNamesUsingCriteriaBuilderArray();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(4, resultList.size());
|
||||
assertEquals(1L, resultList.get(0)[0]);
|
||||
assertEquals("Product Name 1", resultList.get(0)[1]);
|
||||
assertEquals(2L, resultList.get(1)[0]);
|
||||
assertEquals("Product Name 2", resultList.get(1)[1]);
|
||||
assertEquals(3L, resultList.get(2)[0]);
|
||||
assertEquals("Product Name 3", resultList.get(2)[1]);
|
||||
assertEquals(4L, resultList.get(3)[0]);
|
||||
assertEquals("Product Name 4", resultList.get(3)[1]);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void givenProductData_whenNameProjectionUsingJPQL_thenListOfStringReturned() {
|
||||
List resultList = productRepository.findAllNamesUsingJPQL();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(4, resultList.size());
|
||||
assertEquals("Product Name 1", resultList.get(0));
|
||||
assertEquals("Product Name 2", resultList.get(1));
|
||||
assertEquals("Product Name 3", resultList.get(2));
|
||||
assertEquals("Product Name 4", resultList.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenNameProjectionUsingCriteriaBuilder_thenListOfStringReturned() {
|
||||
List<String> resultList = productRepository.findAllNamesUsingCriteriaBuilder();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(4, resultList.size());
|
||||
assertEquals("Product Name 1", resultList.get(0));
|
||||
assertEquals("Product Name 2", resultList.get(1));
|
||||
assertEquals("Product Name 3", resultList.get(2));
|
||||
assertEquals("Product Name 4", resultList.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenCountByCategoryUsingJPQL_thenOK() {
|
||||
List<Object[]> resultList = productRepository.findCountByCategoryUsingJPQL();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(3, resultList.size());
|
||||
assertEquals("category1", resultList.get(0)[0]);
|
||||
assertEquals(2L, resultList.get(0)[1]);
|
||||
assertEquals("category2", resultList.get(1)[0]);
|
||||
assertEquals(1L, resultList.get(1)[1]);
|
||||
assertEquals("category3", resultList.get(2)[0]);
|
||||
assertEquals(1L, resultList.get(2)[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductData_whenCountByCategoryUsingCriteriaBuider_thenOK() {
|
||||
List<Object[]> resultList = productRepository.findCountByCategoryUsingCriteriaBuilder();
|
||||
|
||||
assertNotNull(resultList);
|
||||
assertEquals(3, resultList.size());
|
||||
assertEquals("category1", resultList.get(0)[0]);
|
||||
assertEquals(2L, resultList.get(0)[1]);
|
||||
assertEquals("category2", resultList.get(1)[0]);
|
||||
assertEquals(1L, resultList.get(1)[1]);
|
||||
assertEquals("category3", resultList.get(2)[0]);
|
||||
assertEquals(1L, resultList.get(2)[1]);
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sqlresultsetmapping;
|
||||
package com.baeldung.jpa.sqlresultsetmapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
insert into item(id,grade,color) values (10,'C','blue');
|
||||
insert into item(id,grade,color) values (11,'C','red');
|
||||
insert into item(id,grade,color) values (12,'A','blue');
|
||||
insert into item(id,grade,color) values (13,'D','red');
|
||||
@@ -1,5 +0,0 @@
|
||||
create table Product (id bigint not null, category varchar(255), description varchar(255), name varchar(255), unitPrice decimal(19,2), primary key (id));
|
||||
insert into product(id, name, description, category) values (1,'Product Name 1','This is Product 1', 'category1');
|
||||
insert into product(id, name, description, category) values (2,'Product Name 2','This is Product 2', 'category1');
|
||||
insert into product(id, name, description, category) values (3,'Product Name 3','This is Product 3', 'category2');
|
||||
insert into product(id, name, description, category) values (4,'Product Name 4','This is Product 4', 'category3');
|
||||
@@ -1,4 +0,0 @@
|
||||
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');
|
||||
Reference in New Issue
Block a user