Skip to content

Instantly share code, notes, and snippets.

@Takeno
Last active August 29, 2015 13:57
Show Gist options
  • Save Takeno/9888315 to your computer and use it in GitHub Desktop.
Save Takeno/9888315 to your computer and use it in GitHub Desktop.
SIW-Products
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="products-unit" transaction-type="RESOURCE_LOCAL">
<class>it.uniroma3.db.products.Product</class>
<properties>
<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
<property name="openjpa.ConnectionUserName" value="postgres" />
<property name="openjpa.ConnectionPassword" value="siw" />
<property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost/products" />
<property name="openjpa.jdbc.DBDictionary" value="postgres" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction=add,ForeignKeys=true)" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>
package it.uniroma3.db.products;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Column;
@Entity
@NamedQuery(name = "findAllProducts", query = "SELECT p FROM Product p")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
private Float price;
@Column(length = 2000)
private String description;
@Column(nullable = false)
private String code;
public Product() {
}
public Product(String name, Float price, String description, String code) {
this.name = name;
this.price = price;
this.description = description;
this.code = code;
}
// Getters & Setters
public Long getId() {
return id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription () {
return this.description;
}
public void setDescription(String desc) {
this.description = desc;
}
public boolean equals(Object obj) {
Product product = (Product) obj;
return this.getCode().equals(product.getCode());
}
public int hashCode() {
return this.code.hashCode();
}
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Product");
sb.append("{id=").append(id);
sb.append(", name='").append(name);
sb.append(", price=").append(price);
sb.append(", description='").append(description);
sb.append(", code='").append(code);
sb.append("'}\n");
return sb.toString();
}
}
package it.uniroma3.db.products;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class ProductMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("products-unit");
EntityManager em = emf.createEntityManager();
Product product = new Product();
product.setName("KRIDDIG");
product.setPrice(3.5F);
product.setDescription("A wonderful");
product.setCode("9781853262715-AA");
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(product);
tx.commit();
em.close();
emf.close();
}
}
package it.uniroma3.db.products;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.*;
import java.sql.*;
import java.util.List;
import javax.persistence.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class ProductTest {
private static EntityManagerFactory emf;
private static EntityManager em;
private static EntityTransaction tx;
@BeforeClass
public static void initEntityManager() throws Exception {
emf = Persistence.createEntityManagerFactory("products-unit");
em = emf.createEntityManager();
}
@AfterClass
public static void closeEntityManager() throws SQLException {
if (em != null) em.close();
if (emf != null) emf.close();
}
@Before
public void initTransaction() {
tx = em.getTransaction();
}
@Test
public void shouldCreateAproduct() throws Exception {
// Creates an instance of Product
Product product = new Product();
product.setName("SLANGAN");
product.setCode("ABC");
// Persists the book to the database
tx.begin();
em.persist(product);
tx.commit();
assertNotNull("ID should not be null", product.getId());
// Retrieves all the products from the database
List<Product> products = em.createNamedQuery("findAllProducts").getResultList();
assertEquals(1, products.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment