Skip to content

Commit

Permalink
Added Jpa new features.
Browse files Browse the repository at this point in the history
  • Loading branch information
C5258401 authored and C5258401 committed Jun 28, 2017
1 parent adbd3b5 commit bda3a31
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 109 deletions.
7 changes: 7 additions & 0 deletions espm-cloud-jpa/src/main/java/com/sap/espm/model/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "ESPM_CUSTOMER")
@NamedQueries({
@NamedQuery(name = "Customer.getCustomerByEmailAddress", query = "SELECT c FROM Customer c WHERE c.emailAddress = :emailAddress"),
@NamedQuery(name = "Customer.getCustomerByCustomerId", query = "SELECT cid FROM Customer cid WHERE cid.customerId= :customerId"),
@NamedQuery(name = "Customer.getAllCustomers", query = "SELECT c FROM Customer c")
})
public class Customer {

/* Customer ids are generated within a number range starting with 1 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
Expand All @@ -18,6 +19,7 @@

@Entity
@Table(name = "ESPM_EXTENSION_CUSTOMER_REVIEW")
@NamedQuery(name = "CustomerReview.getAllCustomerReviews", query = "SELECT cr FROM CustomerReview cr")
public class CustomerReview implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down
7 changes: 7 additions & 0 deletions espm-cloud-jpa/src/main/java/com/sap/espm/model/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import javax.persistence.EntityManagerFactory;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
Expand All @@ -26,6 +28,11 @@

@Entity
@Table(name = "ESPM_PRODUCT")
@NamedQueries({
@NamedQuery(name = "Product.getProductByProductId", query = "SELECT p FROM Product p where p.productId = :productId"),
@NamedQuery(name = "Product.getAllProducts", query = "SELECT p FROM Product p"),
@NamedQuery(name = "Product.getProductByCategory", query = "SELECT p FROM Product p WHERE p.category = :category")
})
public class Product {

transient private static Map<String, BigDecimal> prices = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "ESPM_PRODUCT_CATEGORY")
@NamedQueries({
@NamedQuery(name = "ProductCategory.getAllProductCategories", query = "SELECT pc FROM ProductCategory pc"),
@NamedQuery(name = "ProductCategory.getProductCategoryByCategory", query = "SELECT pc FROM ProductCategory pc WHERE pc.category= :category"),
@NamedQuery(name = "ProductCategory.getProductCategoryByCategoryName", query = "SELECT pc FROM ProductCategory pc WHERE pc.categoryName= :categoryName")
})
public class ProductCategory {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
Expand All @@ -23,6 +25,10 @@

@Entity
@Table(name = "ESPM_SALES_ORDER_HEADER")
@NamedQueries({
@NamedQuery(name = "SalesOrderHeader.getSOHBySaledOrderId", query = "SELECT soh FROM SalesOrderHeader soh WHERE soh.salesOrderId= :salesOrderId"),
@NamedQuery(name = "SalesOrderHeader.getSOHByCustomerId", query = "SELECT soh FROM SalesOrderHeader soh WHERE soh.customerId= :customerId")
})
public class SalesOrderHeader {

/* Sales order ids are generated within a number range starting with 5 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "ESPM_SALES_ORDER_ITEM")
@NamedQueries({
@NamedQuery(name = "SalesOrderItem.getSOIBySalesOrderItemId", query = "SELECT soi FROM SalesOrderItem soi WHERE soi.id.salesOrderId= :id"),
@NamedQuery(name = "SalesOrderItem.getSOIByCurrencyCode", query = "SELECT soi FROM SalesOrderItem soi WHERE soi.currencyCode = :currencyCode")
})
public class SalesOrderItem {

private static final BigDecimal TAX_AMOUNT_FACTOR = new BigDecimal("0.19");
Expand Down
6 changes: 6 additions & 0 deletions espm-cloud-jpa/src/main/java/com/sap/espm/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
Expand All @@ -16,6 +18,10 @@

@Entity
@Table(name = "ESPM_STOCK")
@NamedQueries({
@NamedQuery(name = "Stock.getAllStocks", query = "SELECT st FROM Stock st"),
@NamedQuery(name = "Stock.getStockByProductId", query = "SELECT s FROM Stock s WHERE s.productId= :productId")
})
public class Stock {

@Id
Expand Down
6 changes: 6 additions & 0 deletions espm-cloud-jpa/src/main/java/com/sap/espm/model/Supplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name = "ESPM_SUPPLIER")
@NamedQueries({
@NamedQuery(name = "Supplier.getAllSuppliers", query = "SELECT s FROM Supplier s"),
@NamedQuery(name = "Supplier.getSupplierBySupplierId", query = "SELECT s FROM Supplier s WHERE s.supplierId= :supplierId")
})
public class Supplier {
/* Supplier ids are generated within a number range starting with 2 */
@TableGenerator(name = "SupplierGenerator", table = "ESPM_ID_GENERATOR", pkColumnName = "GENERATOR_NAME", valueColumnName = "GENERATOR_VALUE", pkColumnValue = "Customer", initialValue = 100000000, allocationSize = 100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public List<Product> loadProducts(List<Supplier> suppliers) {
List<Product> resProd = null;
try {
em.getTransaction().begin();
queryProd = em.createQuery("SELECT p FROM Product p", Product.class);
queryProd = em.createNamedQuery("Product.getAllProducts", Product.class);
resProd = queryProd.getResultList();
if (resProd.size() > 5) {
logger.info(resProd.size() + " Products already available in the db");
} else {
new XMLParser().readProduct(em, "com/sap/espm/model/data/Products.xml", suppliers);
em.getTransaction().commit();
queryProd = em.createQuery("SELECT p FROM Product p", Product.class);
queryProd = em.createNamedQuery("Product.getAllProducts", Product.class);
resProd = queryProd.getResultList();
logger.info(resProd.size() + " Products loaded into the db");
}
Expand All @@ -96,14 +96,14 @@ public List<CustomerReview> loadCustomerReviews(List<Product> products) {
List<CustomerReview> resReview = null;
try {
em.getTransaction().begin();
queryReviews = em.createQuery("SELECT p FROM CustomerReview p", CustomerReview.class);
queryReviews = em.createNamedQuery("CustomerReview.getAllCustomerReviews", CustomerReview.class);
resReview = queryReviews.getResultList();
if (resReview.size() > 5) {
logger.info(resReview.size() + " Customer Reviews already available in the db");
} else {
new XMLParser().readCustomerReview(em, "com/sap/espm/model/data/CustomerReviews.xml", products);
em.getTransaction().commit();
queryReviews = em.createQuery("SELECT p FROM CustomerReview p", CustomerReview.class);
queryReviews = em.createNamedQuery("CustomerReview.getAllCustomerReviews", CustomerReview.class);
resReview = queryReviews.getResultList();
logger.info(resReview.size() + " Products loaded into the db");
}
Expand All @@ -126,14 +126,14 @@ public void loadCustomers() {
List<Customer> resBP;
try {
em.getTransaction().begin();
queryBP = em.createQuery("SELECT c FROM Customer c", Customer.class);
queryBP = em.createNamedQuery("Customer.getAllCustomers",Customer.class);
resBP = queryBP.getResultList();
if (resBP.size() > 5) {
logger.info(resBP.size() + " Customers already available in the db");
} else {
new XMLParser().readCustomers(em, "com/sap/espm/model/data/Business_Partners.xml");
em.getTransaction().commit();
queryBP = em.createQuery("SELECT c FROM Customer c", Customer.class);
queryBP = em.createNamedQuery("Customer.getAllCustomers",Customer.class);
resBP = queryBP.getResultList();
logger.info(resBP.size() + " customers loaded into the db");
}
Expand All @@ -153,14 +153,14 @@ public List<Supplier> loadSuppliers() {
List<Supplier> resBP = null;
try {
em.getTransaction().begin();
queryBP = em.createQuery("SELECT s FROM Supplier s", Supplier.class);
queryBP = em.createNamedQuery("Supplier.getAllSuppliers", Supplier.class);
resBP = queryBP.getResultList();
if (resBP.size() > 5) {
logger.info(resBP.size() + " Suppliers already available in the db");
} else {
new XMLParser().readSuppliers(em, "com/sap/espm/model/data/Business_Partners.xml");
em.getTransaction().commit();
queryBP = em.createQuery("SELECT s FROM Supplier s", Supplier.class);
queryBP = em.createNamedQuery("Supplier.getAllSuppliers", Supplier.class);
resBP = queryBP.getResultList();
logger.info(resBP.size() + " suppliers loaded into the db");
}
Expand All @@ -186,14 +186,14 @@ public void loadProductCategories(List<Product> products) {
List<ProductCategory> resPC;
try {
em.getTransaction().begin();
queryPC = em.createQuery("SELECT pc FROM ProductCategory pc", ProductCategory.class);
queryPC = em.createNamedQuery("ProductCategory.getAllProductCategories", ProductCategory.class);
resPC = queryPC.getResultList();
if (resPC.size() > 5) {
logger.info(resPC.size() + " Product Categories already available in the db");
} else {
new XMLParser().readProductCategory(em, "com/sap/espm/model/data/Product_Categories.xml", products);
em.getTransaction().commit();
queryPC = em.createQuery("SELECT pc FROM ProductCategory pc", ProductCategory.class);
queryPC = em.createNamedQuery("ProductCategory.getAllProductCategories", ProductCategory.class);
resPC = queryPC.getResultList();
logger.info(resPC.size() + " Product Categories loaded into the db");
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public void loadStock(List<Product> products) {
int lenProdName;
try {
em.getTransaction().begin();
queryStock = em.createQuery("SELECT st FROM Stock st", Stock.class);
queryStock = em.createNamedQuery("Stock.getAllStocks", Stock.class);
resStock = queryStock.getResultList();
if (resStock.size() > 5) {
logger.info(resStock.size() + " Stock already available in the db");
Expand All @@ -248,7 +248,7 @@ public void loadStock(List<Product> products) {
}

em.getTransaction().commit();
queryStock = em.createQuery("SELECT st FROM Stock st", Stock.class);
queryStock = em.createNamedQuery("Stock.getAllStocks", Stock.class);
resStock = queryStock.getResultList();
logger.info(resStock.size() + " Stocks loaded into the db");
}
Expand Down
12 changes: 1 addition & 11 deletions espm-cloud-jpa/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="com.sap.espm.model" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.sap.espm.model.ProductCategory</class>
<class>com.sap.espm.model.Product</class>
<class>com.sap.espm.model.ProductText</class>
<class>com.sap.espm.model.Customer</class>
<class>com.sap.espm.model.SalesOrderHeader</class>
<class>com.sap.espm.model.SalesOrderItem</class>
<class>com.sap.espm.model.SalesOrderItemId</class>
<class>com.sap.espm.model.CustomerReview</class>
<class>com.sap.espm.model.Supplier</class>
<class>com.sap.espm.model.Stock</class>

<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
Expand Down
15 changes: 4 additions & 11 deletions espm-cloud-jpa/src/test/java/com/sap/espm/model/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ public void testExistingCustomerSearchTyped() {
assertTrue("Business Partner not created",
tf.createCustomer(em, bupaId));
// Search for Business Partner
TypedQuery<Customer> query = em.createQuery(
"SELECT bp FROM Customer bp WHERE bp.customerId=:id",
Customer.class);

bupaAct = query.setParameter("id", bupaId).getSingleResult();
TypedQuery<Customer> query = em.createNamedQuery("Customer.getCustomerByCustomerId", Customer.class);
bupaAct = query.setParameter("customerId", bupaId).getSingleResult();
assertEquals(
"Search via typed query for existing Business Partner: Added Business Partner not persisted in the database",
bupaId, bupaAct.getCustomerId());
Expand Down Expand Up @@ -157,8 +154,7 @@ public void testExistingCustomersSearchTyped() {
DataLoader dl = new DataLoader(emf);
dl.loadCustomers();
// Search for mutiple Business Partners.
TypedQuery<Customer> query = em.createQuery(
"SELECT bp FROM Customer bp", Customer.class);
TypedQuery<Customer> query = em.createNamedQuery("Customer.getAllCustomers", Customer.class);
List<Customer> result = query.getResultList();
assertTrue(
"Search for mutiple existing Business Partners: Multiple Business Partners not added",
Expand All @@ -178,10 +174,7 @@ public void testNotExistingCustomersSearchTyped() {
em.getTransaction().begin();
try {
// Search for mutiple Customers.
TypedQuery<Customer> query = em
.createQuery(
"SELECT bp FROM Customer bp WHERE bp.customerId = :customerId",
Customer.class);
TypedQuery<Customer> query = em.createNamedQuery("Customer.getCustomerByCustomerId", Customer.class);
result = query.setParameter("customerId", "104").getResultList();
assertEquals(
"Search via typed query for not existing mutiple Business Partners: Business Partners exists in database",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ public void testExistingProductcategorySearchTyped() {
em.getTransaction().begin();

// Search for Product category
TypedQuery<ProductCategory> query = em
.createQuery(
"SELECT pc FROM ProductCategory pc WHERE pc.category=:category",
ProductCategory.class);

TypedQuery<ProductCategory> query = em.createNamedQuery("ProductCategory.getProductCategoryByCategory",
ProductCategory.class);
prodCatResult = query.setParameter("category", prodCategory)
.getSingleResult();

Expand Down Expand Up @@ -167,8 +164,8 @@ public void testExistingProductsSearchTyped() {
DataLoader dl = new DataLoader(emf);
dl.loadProductCategories(null);
// Search for mutiple Product Categories.
TypedQuery<ProductCategory> query = em.createQuery(
"SELECT pc FROM ProductCategory pc", ProductCategory.class);
TypedQuery<ProductCategory> query = em.createNamedQuery("ProductCategory.getAllProductCategories",
ProductCategory.class);
List<ProductCategory> result = query.getResultList();

assertTrue(
Expand All @@ -189,10 +186,8 @@ public void testNotExistingProductsSearchTyped() {
em.getTransaction().begin();
try {
// Search for mutiple Product Category.
TypedQuery<ProductCategory> query = em
.createQuery(
"SELECT pc FROM ProductCategory pc WHERE pc.categoryName = :categoryName",
ProductCategory.class);
TypedQuery<ProductCategory> query = em.createNamedQuery("ProductCategory.getProductCategoryByCategoryName",
ProductCategory.class);
result = query.setParameter("categoryName", "Trackpad")
.getResultList();
assertEquals(
Expand Down
13 changes: 4 additions & 9 deletions espm-cloud-jpa/src/test/java/com/sap/espm/model/ProductTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ public void testExistingProductSearchTyped() {
em.getTransaction().begin();

// Search for Product
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p WHERE p.productId=:id",
Product.class);
TypedQuery<Product> query = em.createNamedQuery("Product.getProductByProductId", Product.class);

prodResult = query.setParameter("id", prodId).getSingleResult();
prodResult = query.setParameter("productId", prodId).getSingleResult();

assertEquals(
"Search via typed query for existing product: Added Product not persisted in the database",
Expand Down Expand Up @@ -155,8 +153,7 @@ public void testExistingProductsSearchTyped() {
DataLoader dl = new DataLoader(emf);
dl.loadProducts(null);
// Search for mutiple Products.
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p", Product.class);
TypedQuery<Product> query = em.createNamedQuery("Product.getAllProducts", Product.class);
List<Product> result = query.getResultList();

assertTrue(
Expand All @@ -177,9 +174,7 @@ public void testNotExistingProductsSearchTyped() {
em.getTransaction().begin();
try {
// Search for mutiple Products.
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p WHERE p.category = :category",
Product.class);
TypedQuery<Product> query = em.createNamedQuery("Product.getProductByCategory", Product.class);
result = query.setParameter("category", "Trackpad").getResultList();
assertEquals(
"Search via typed query for not existing mutiple products: Product exists in database",
Expand Down
Loading

0 comments on commit bda3a31

Please sign in to comment.