Search This Blog

Monday, August 19, 2013

Java Persistence/Embeddables

An embeddable object can be shared between multiple classes. Consider a Address object, that both a Customer and an Orders contain. Both Customer  and Orders have their own tables.


TABLE CREATION:


ORDERS Table
CREATE TABLE ORDERS 
(
  ID NUMBER NOT NULL 
, ORDER_DATE Date
, BENF_NAME VARCHAR2(20) 
, STREET1 VARCHAR2(255) 
, STREET2 VARCHAR2(225) 
, ZIPCODE VARCHAR2(6) 
, STATE VARCHAR2(20) 
, COUNTRY VARCHAR2(20) 
, CITY VARCHAR2(50) 
, CONSTRAINT ORDERS_PK PRIMARY KEY 
  (
    ID 
  )
  ENABLE 
);

CUSTOMER Table
CREATE TABLE CUSTOMER 
(
  ID NUMBER NOT NULL 
, FIRST_NAME VARCHAR2(20) 
, LAST_NAME VARCHAR2(45) 
, PHONE_NUMBER VARCHAR2(15) 
, EMAIL VARCHAR2(50) 
, STREET1 VARCHAR2(255) 
, STREET2 VARCHAR2(255) 
, ZIPCODE VARCHAR2(6) 
, STATE VARCHAR2(20) 
, COUNTRY VARCHAR2(50) 
, CITY VARCHAR2(50) 
, CONSTRAINT CUSTOMER_PK PRIMARY KEY 
  (
    ID 
  )
  ENABLE 
);

SEQUENCES AND TRIGGERS CREATION:


ORDERS Table
CREATE SEQUENCE ORDERS_SEQ NOCACHE;

CREATE TRIGGER ORDERS_TRG 
BEFORE INSERT ON ORDERS 
FOR EACH ROW 
BEGIN
    IF :NEW.ID IS NULL THEN
      SELECT ORDERS_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
    END IF;
END;
/

CUSTOMER Table
CREATE SEQUENCE CUSTOMER_SEQ NOCACHE;

CREATE TRIGGER CUSTOMER_TRG 
BEFORE INSERT ON CUSTOMER 
FOR EACH ROW 
BEGIN
    IF :NEW.ID IS NULL THEN
      SELECT CUSTOMER_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
    END IF;
END;
/

INSERT TEST DATA:


ORDERS Table
REM INSERTING into ORDERS
Insert into ORDERS (ID,ORDER_DATE,BENF_NAME,STREET1,STREET2,ZIPCADE,STATE,COUNTRY,CITY) values (1,to_timestamp('17-AUG-13','DD-MON-RR HH.MI.SSXFF AM'),'benf 1','street 1','street 2','11','state 11','country 11','city 11');
Insert into ORDERS (ID,ORDER_DATE,BENF_NAME,STREET1,STREET2,ZIPCADE,STATE,COUNTRY,CITY) values (2,to_timestamp('18-AUG-13','DD-MON-RR HH.MI.SSXFF AM'),'benf 2','street 1 -2','street 2 -2','22','state 22','country 22','city 22');

CUSTOMER Table
REM INSERTING into CUSTOMER
Insert into CUSTOMER (ID,FIRST_NAME,LAST_NAME,PHONE_NUMBER,EMAIL,STREET1,STREET2,ZIPCODE,STATE,COUNTRY,CITY) values (1,'cust f name 1','cust l name 1',null,'custfname1@mail.com','cust f name 1 street 1','cust f name 1 street 2','11','state1','cust f name 1 counntry 1','cust f name 1 city 1');
Insert into CUSTOMER (ID,FIRST_NAME,LAST_NAME,PHONE_NUMBER,EMAIL,STREET1,STREET2,ZIPCODE,STATE,COUNTRY,CITY) values (2,'cust f name 2','cust 2 name',null,'custfname2@mail.com','cust f name 2 street 1','cust f name 2 street 2','22','state2','cust f name 2 counntry','cust f name 2 city ');

CLASS CREATION:


Address Class
@Embeddable
public class Address {

    @Column(name = "STREET1")
    private String street1;
    @Column(name = "STREET2")
    private String street2;
    @Column(name = "ZIPCODE")
    private String zipcode;
    @Column(name = "STATE")
    private String state;
    @Column(name = "COUNTRY")
    private String country;
    @Column(name = "CITY")
    private String city;
    //generate getters, setters, toString(), hashCode(),equals()
}


Orders Class
@Entity
@Table(name = "ORDERS")
public class Orders implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "ORDER_DATE")
    @Temporal(TemporalType.DATE)
    private Date orderDate;
    @Column(name = "BENF_NAME")
    private String benfName;
    @Embedded
    private Address address;
    //generate getters, setters, toString(), hashCode(),equals()
}

Customer Class
@Entity
@Table(name = "CUSTOMER")
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;
    @Column(name = "PHONE_NUMBER")
    private String phoneNumber;
    @Column(name = "EMAIL")
    private String email;
    @Embedded
    private Address address;
    //generate getters, setters, toString(), hashCode(),equals()
}

JUNIT TEST CASE:


public class InheritanceJUnit {

    static EntityManagerFactory emf;
    static EntityManager em;
    static EntityTransaction trx;

    @BeforeClass
    public static void initEntityManager() throws Exception {
        emf = Persistence.createEntityManagerFactory("JavaApplicationJPAPU");
        em = emf.createEntityManager();
        trx = em.getTransaction();
    }

    @AfterClass
    public static void closeEntityManager() throws Exception {
        if (em != null) {
            em.close();
        }
        if (emf != null) {
            emf.close();
        }
    }

    @Before
    public void initTransaction() throws Exception {
        trx.begin();
    }

    @After
    public void endTransaction() throws Exception {
        if (!trx.getRollbackOnly()) {
            trx.commit();
        }
    }
    @Test
    @Ignore
    public void testEmbeddableStrategyInsert() {

        Address address = new Address();
        address.setCity("Cust City");
        address.setCountry("Cust Country");
        address.setState("Cust State");
        address.setStreet1("Cust Street1");
        address.setStreet2("Cust Street2");
        address.setZipcode("Zcode");

        Customer customer = new Customer();
        customer.setId(10);
        customer.setFirstName("Cust FirstName");
        customer.setLastName("Cust LastName");
        customer.setPhoneNumber("Cust Phone");
        customer.setEmail("CustMail@host.com");
        customer.setAddress(address);
        em.persist(customer);
        System.out.println("Customer : " + customer);

        Orders orders = new Orders();
        orders.setId(10);
        orders.setBenfName("Benf Name");
        orders.setOrderDate(new Date());
        //orders.setAddress(null);
        em.persist(orders);
        System.out.println("Orders : " + orders);
    }

    @Test
    @Ignore
    public void testEmbeddableStrategySelect() {

        Customer customer = em.find(Customer.class, 10);
        assertNotNull(customer);
        System.out.println("Customer : " + customer);

        Orders orders = em.find(Orders.class, 10);
        assertNotNull(orders);
        System.out.println("Orders : " + orders);

    }

    @Test
    @Ignore
    public void testEmbeddableStrategyUpdate() {
        Address address = new Address();
        address.setCity("Cust City1");
        address.setCountry("Cust Country");
        address.setState("Cust State");
        address.setStreet1("Cust Street1");
        address.setZipcode("Zcode");

        Customer customer = em.find(Customer.class, 10);
        assertNotNull(customer);
        customer.setEmail("new Mail");
        customer.setAddress(null);
        em.merge(customer);
        System.out.println("Customer : " + customer);

        Orders orders = em.find(Orders.class, 10);
        assertNotNull(orders);
        orders.setBenfName("New Benf Name");
        orders.setAddress(address);
        em.merge(orders);
        System.out.println("Orders : " + orders);


    }

    @Test
    @Ignore
    public void testEmbeddableStrategyDelete() {

        Customer customer = em.find(Customer.class, 10);
        assertNotNull(customer);
        em.remove(customer);

        Orders orders = em.find(Orders.class, 10);
        assertNotNull(orders);
        em.remove(orders);

    }
}

Post a Comment