This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.craigew.service; | |
import com.craigew.entity.Address; | |
import com.craigew.entity.Customer; | |
import junit.framework.Assert; | |
import org.junit.Test; | |
import javax.persistence.EntityNotFoundException; | |
import java.util.List; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Customer> returnAllCustomers() { | |
return queryString("SELECT e FROM Customer e"); | |
} | |
public List<Customer> returnCustomersForACountry(String country) { | |
return queryString("SELECT e FROM Customer e WHERE e.residentialAddress.country='" + country + "'"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected List<T> queryString(String sql){ | |
EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPAIntroduction"); | |
EntityManager em = factory.createEntityManager(); | |
List<T> results= em.createQuery(sql).getResultList(); | |
em.close(); | |
factory.close(); | |
return results; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OneToOne(cascade = CascadeType.ALL, optional=false) | |
@JoinColumn(name="RESIDENTIAL_ADDRESS_ID") | |
private Address residentialAddress; | |
@OneToOne(cascade = CascadeType.ALL, optional=true) | |
@JoinColumn(name="POSTAL_ADDRESS_ID") | |
private Address postalAddress; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<class>com.craigew.entity.Address</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.craigew.entity; | |
import javax.persistence.Entity; | |
@Entity | |
public class Address extends BaseEntity{ | |
private String addressLine1; | |
private String addressLine2; | |
private String suburb; | |
private String postalCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerManagementService extends DataAccess<Customer> { | |
public long createCustomer(Customer customer) { | |
return add(customer); | |
} | |
public boolean updateCustomer(Customer customer) { | |
update(customer); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataAccess<T extends BaseEntity> { | |
private Class<T> entityClass; | |
protected long add(T object){ | |
EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPAIntroduction"); | |
EntityManager em = factory.createEntityManager(); | |
em.getTransaction().begin(); | |
em.persist(object); | |
em.getTransaction().commit(); | |
long id=object.getId(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<persistence xmlns="http://java.sun.com/xml/ns/persistence" | |
version="1.0"> | |
<persistence-unit name="JPAIntroduction" transaction-type="RESOURCE_LOCAL"> | |
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> | |
<class>com.craigew.entity.Customer</class> | |
<class>com.craigew.entity.BaseEntity</class> | |
<properties> | |
<property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@192.168.0.9:1521/xe"/> | |
<property name="openjpa.ConnectionDriverName" value="oracle.jdbc.driver.OracleDriver"/> | |
<property name="openjpa.ConnectionUserName" value="dev"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MappedSuperclass | |
public class BaseEntity implements Identifier{ | |
@Id | |
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="id_sequence_gen") | |
@SequenceGenerator(name="id_sequence_gen", sequenceName="id_sequence") | |
private long id; | |
public long getId() { |
NewerOlder