Monday 9 September 2013

Deployment ADF Applacition On GlassFish Server

1- Download Glass Fish Server
http://www.oracle.com/technetwork/java/javaee/downloads/index.html
go to "Oracle GlassFish Server" and download it
2- Download ADF Essential
http://www.oracle.com/technetwork/developer-tools/adf/downloads/index.html
got to "Oracle ADF Essentials" and download it ("adf-essentials.zip)
3- Installing Glass Fish. Create Domain.
4- Unzip adf-essentials.zip in domain (unzip.exe –j adf-essentials.zip)
5- Start-domain. http//localhost:4848.
6- server-config. Change JVM setting.
                        -Doracle.mds.cache=simple
                        -Duser.timezone=Etc/GMT-2 (write your timezone).          
                        -XX:PermSize=256m (Edit Java momery).          
                        -XX:MaxPermSize=512m (Edit Java momery).
7- Create JDBC Connection Pools. javax.sql.XADataSource.
8- Create JDBC resource. jdbo/hr
9- Edit ADF Application AppModule. Project  properties.  Application properties.
10- Edit Java EE web application.
11- Develop ADF application.
12- http://localhost:9090/context-root/faces/yourPage.jspx

Wednesday 28 August 2013

import sun.misc.BASE64Encoder got error in Eclipse

Problem:
while importing sun.misc.BASE64Encoder got error in Eclipse; For this two imports;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

I got this error:
Access restriction: The type BASE64Decoder is not accessible due to restriction on required library xxx.jar

Cause:
That error is caused by your Eclipse configuration. You can reduce it to a warning. Better still, use a Base64 encoder that isn't part of a non-public API. Apache Commons has one.

Solution:
  1. Go to Window-->Preferences-->Java-->Compiler-->Error/Warnings.
  2. Select Deprecated and Restricted API. Change it to warning.
  3. Change forbidden and Discouraged Reference and change it to warning. (or as your need.)

Thursday 22 August 2013

JPQL/Custom data retrieve from JPQL SELECT Statment

To retrieve specific columns from table and mapped it into new Class not the JPA contains the entire Table columns, to reduce the memory allocation for the entire JPA Entity Class.
The result of this query is a list of AnimalInfo objects that have been instantiated with the new operator and initialized with the animal ID, and Type of the animals.


TABLE CREATION:

CREATE TABLE ANIMAL 
(
ANIMAL_ID NUMBER NOT NULL
, TYPE VARCHAR2(45 BYTE)
, TOTAL_NO NUMBER
, CATEGORY_ID NUMBER
, CONSTRAINT ANIMAL_PK PRIMARY KEY
(
ANIMAL_ID
)
ENABLE
);

SEQUENCES AND TRIGGERS CREATION:

CREATE SEQUENCE ANIMAL_SEQ NOCACHE;

create or replace TRIGGER ANIMAL_TRG
BEFORE INSERT ON ANIMAL
FOR EACH ROW
BEGIN
IF :NEW.ANIMAL_ID IS NULL THEN
SELECT ANIMAL_SEQ.NEXTVAL INTO :NEW.ANIMAL_ID FROM DUAL;
END IF;
END;

INSERT TEST DATA:

REM INSERTING into ANIMAL
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (1,'Elephant',4,1);
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (2,'Turtle',33,3);
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (3,'Snake',3,3);
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (4,'Pelican',6,3);
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (5,'Lion',2,1);
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (6,'Zebra',4,1);
Insert into ANIMAL (ANIMAL_ID,TYPE,TOTAL_NO,CATEGORY_ID) values (7,'Owl',2,2);

CLASS CREATION:


Animal JPA Entity Class
@Entity
@Table(name = "ANIMAL")
public class Animal implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ANIMAL_ID")
private Integer animalId;
@Column(name = "TYPE")
private String type;
@Column(name = "TOTAL_NO")
private Integer totalNo;
@Column(name = "CATEGORY_ID")
private Integer categoryId;
//generate getters, setters, toString(), hashCode(),equals()
}

Custom AnimalInfo Class
public class AnimalInfo {

private Integer animalId;
private String type;
private Integer totalNo;

public AnimalInfo() {
}

public AnimalInfo(Integer animalId, String type) {
this.animalId = animalId;
this.type = type;
}

public AnimalInfo(Integer animalId, String type, Integer totalNo) {
this.animalId = animalId;
this.type = type;
this.totalNo = totalNo;
}
//generate getters, setters, toString(), hashCode(),equals()
}

JUNIT TEST CASE:

public class InheritanceJUnit {

static EntityManagerFactory emf;
static EntityManager em;

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

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

@Test
@Ignore
public void testTypedQueryReturnInCustomClass() {

TypedQuery typedQuery;
typedQuery = em.createQuery("select NEW com.jpa.entity.info.AnimalInfo( a.animalId,a.type) from Animal a", AnimalInfo.class);
List animalInfoList = typedQuery.getResultList();
for (AnimalInfo animalInfo : animalInfoList) {
assertNotNull(animalInfo);
System.out.println(animalInfo);
}

}
}

Wednesday 21 August 2013

JPA @SecondaryTables

Up to now, I have assumed that an entity gets mapped to a single table, also known as a  primary table. But sometimes when you have an existing data model, you need to spread the data across multiple tables, or secondary tables. To do this, you need to use the annotation @SecondaryTable to associate a secondary table to an entity or @SecondaryTables (with an “s”) for several secondary tables. You can distribute the data of an entity across columns in both the primary table and the secondary tables simply by defining the secondary tables with anno-tations and then specifying for each attribute which table it is in (with the @Column annotation, which I’ll describe in the “Attributes” section in more detail).

Example below shows an Address entity mapping its attributes in one primary table and two secondary tables.




TABLE CREATION:


T_ADDRESS Table
CREATE TABLE T_ADDRESS 
(
ID NUMBER NOT NULL
, STREET1 VARCHAR2(245)
, STREET2 VARCHAR2(245)
, CONSTRAINT T_ADDRESS_PK PRIMARY KEY
(
ID
)
ENABLE
);

T_CITY Table
CREATE TABLE T_CITY 
(
ID NUMBER NOT NULL
, CITY VARCHAR2(45)
, STATE VARCHAR2(50)
, ZIPCODE VARCHAR2(10)
, CONSTRAINT T_CITY_PK PRIMARY KEY
(
ID
)
ENABLE
);

T_COUNTRY Table
CREATE TABLE T_COUNTRY 
(
ID NUMBER NOT NULL
, COUNTRY VARCHAR2(50)
, CONSTRAINT T_COUNTRY_PK PRIMARY KEY
(
ID
)
ENABLE
);

SEQUENCES AND TRIGGERS CREATION:

CREATE SEQUENCE T_ADDRESS_SEQ;

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

CONSTRAINT:


T_CITY Table
ALTER TABLE T_CITY
ADD CONSTRAINT T_CITY_FK FOREIGN KEY
(
ID
)
REFERENCES T_ADDRESS
(
ID
)
ENABLE;

T_COUNTRY Table
ALTER TABLE T_COUNTRY
ADD CONSTRAINT T_COUNTRY_FK FOREIGN KEY
(
ID
)
REFERENCES T_ADDRESS
(
ID
)
ENABLE;

INSERT TEST DATA:


T_ADDRESS Table

REM INSERTING into T_ADDRESS
Insert into T_ADDRESS (ID,STREET1,STREET2) values (1,'STREET_1 1','STREET_2 1');
Insert into T_ADDRESS (ID,STREET1,STREET2) values (2,'STREET_1 2','STREET_2 2');
Insert into T_ADDRESS (ID,STREET1,STREET2) values (3,'STREET_1 3','STREET_2 3');

T_CITY Table

REM INSERTING into T_CITY
Insert into T_CITY (ID,CITY,STATE,ZIPCODE) values (1,'CITY 1','STATE 1','111');
Insert into T_CITY (ID,CITY,STATE,ZIPCODE) values (2,'CITY 2','STATE 2','222');
Insert into T_CITY (ID,CITY,STATE,ZIPCODE) values (3,'CITY 3','STATE 3','333');

T_COUNTRY Table

REM INSERTING into T_COUNTRY
Insert into T_COUNTRY (ID,COUNTRY) values (1,'COUNTRY 1');
Insert into T_COUNTRY (ID,COUNTRY) values (2,'COUNTRY 2');
Insert into T_COUNTRY (ID,COUNTRY) values (3,'COUNTRY 3');

CLASS CREATION:


TAddress Class
@Entity
@Table(name = "T_ADDRESS")
@SecondaryTables(
{
@SecondaryTable(name = "T_COUNTRY"),
@SecondaryTable(name = "T_CITY")
})
public class TAddress implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Column(name = "STREET1")
private String street1;
@Column(name = "STREET2")
private String street2;

@Column(table = "T_COUNTRY", name = "COUNTRY")
private String country;

@Column(table = "T_CITY", name = "CITY")
private String city;
@Column(table = "T_CITY", name = "STATE")
private String state;
@Column(table = "T_CITY", name = "ZIPCODE")
private String zipcode;

//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 testSecondaryTableInsert() {

TAddress tAddress = new TAddress();
tAddress.setId(10);
tAddress.setStreet1("Street 1 10");
tAddress.setStreet2("Street 2 10");

tAddress.setCountry("Country 1 10");

tAddress.setCity("City 10");
tAddress.setState("State 10");
tAddress.setZipcode("1010");
assertNotNull(tAddress);
em.persist(tAddress);
System.out.println("TAddress : " + tAddress);

TAddress tAddress2 = new TAddress();
tAddress2.setId(11);
tAddress2.setStreet1("Street 1 11");
tAddress2.setStreet2("Street 2 11");

tAddress2.setCountry("Country 1 11");


assertNotNull(tAddress2);
em.persist(tAddress2);
System.out.println("TAddress2 : " + tAddress2);

}

@Test
@Ignore
public void testSecondaryTableSelect() {

TAddress tAddress = em.find(TAddress.class, 10);
assertNotNull(tAddress);

System.out.println("TAddress : " + tAddress);

}

@Test
@Ignore
public void testSecondaryTableUpdate() {

TAddress tAddress = em.find(TAddress.class, 10);
assertNotNull(tAddress);

tAddress.setStreet1("Street 1 10 edited");
tAddress.setStreet2("Street 2 10 edited");

tAddress.setCountry("Country 1 10 edited");

tAddress.setCity("City 10 edited");
tAddress.setState(null);
tAddress.setZipcode("1010");
em.merge(tAddress);
System.out.println("TAddress : " + tAddress);

TAddress tAddress2 = em.find(TAddress.class, 11);
assertNotNull(tAddress2);

tAddress2.setStreet1("Street 1 11 edited");
tAddress2.setStreet2("");

tAddress2.setCountry("Country 1 11 edited");

tAddress2.setCity("City 10 edited");
tAddress2.setState("State 10 edited");
tAddress2.setZipcode(null);

em.merge(tAddress2);
System.out.println("TAddress2 : " + tAddress2);
}

@Test
@Ignore
public void testSecondaryTableDelete() {

TAddress tAddress = em.find(TAddress.class, 10);
assertNotNull(tAddress);

em.detach(tAddress);
System.out.println("TAddress : " + tAddress);

TAddress tAddress2 = em.find(TAddress.class, 11);
assertNotNull(tAddress2);

em.detach(tAddress2);
System.out.println("TAddress2 : " + tAddress2);

}
}

Monday 19 August 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);

}
}

Inheritance Mapping Stategies in JPA

In JPA, an entity class may inherit from another entity class its behavior and state. The behavior and state become shared between entity classes enabling the inheritance of existing mappings. There are several types of inheritance which are  Joined Strategy, Single-Table Strategy, Table-per-Concrete-Class Strategy. Two of these types; Single table inheritance and Joined table inheritance have many similarities:

When it comes to mapping inheritance, JPA supports three different strategies. When an entity hierarchy exists, it always has an entity as its root. The root entity class can define the inheritance strategy by using the @Inheritance annotation. If it doesn’t, the default single-table-per-class strategy will be applied.

Their object model is the same.
  • They can be configured using either annotations or XML.
  • A discriminator column (a single column used for distinguishing to which class type a database row belongs) is required on the database.
  • The underlying database structure of the single table and joined table inheritance is, however, slightly different.
please refer to each type link for further discussion, example and code snippts.

Joined Strategy

In the joined table inheritance, each class shares data from the root table. In addition, each subclass defines its own table that adds its extended state. The following example shows two child tables, EXTERNAT_VET and IN_HOUSE_VET, as well as parent table VET.
Click here for mode details about Joined Strategy

Single-Table Strategy

In the single table inheritance, the entire class hierarchy is represented by a single table. As the following example shows, the Three classes map to the same VET_ALL table.
Click here for mode details about Single Table Strategy


Table-per-Concrete-Class Strategy

In the table-per-concrete class, each Table contains the shared data as its specific data. In addition, each subclass defines its own table that adds its extended state. The following example shows two child tables, VET_IN and VET_OUT.
Click here for mode details about Table-per-Concrete Class Strategy

JPA Table-Per-Concrete Strategy

In the table-per-class (or table-per-concrete-class) strategy, each entity is mapped to its own dedicated table like the joined strategy. The difference is that all attributes of the root entity will also be mapped to columns of the child entity table. From a database point of view, this strategy de-normalizes the model and causes all root entity attributes to be redefined in the tables of all leaf entities that inherit from it. With the table-per-class strategy, there is no shared table, no shared columns, and no discriminator column. The only requirement is that all tables must share a common primary key that matches across all tables in the hierarchy.

In the table-per-concrete class, each Table contains the shared data as its specific data. In addition, each subclass defines its own table that adds its extended state. The following example shows two child tables, VET_IN and VET_OUT

TABLE CREATION:


VET_IN Table
CREATE TABLE VET_IN 
(
VET_ID NUMBER NOT NULL
, NAME VARCHAR2(45 BYTE)
, QUALIFICATION VARCHAR2(45 BYTE)
, SALARY NUMBER
, CONSTRAINT VET_IN_PK PRIMARY KEY
(
VET_ID
)
ENABLE
);

VET_OUT Table
CREATE TABLE VET_OUT 
(
VET_ID NUMBER NOT NULL
, NAME VARCHAR2(45 BYTE)
, COUNTRY VARCHAR2(45 BYTE)
, VISITING_FEES NUMBER
, QUALIFICATION VARCHAR2(45 BYTE)
, CONSTRAINT VET_OUT_PK PRIMARY KEY
(
VET_ID
)
ENABLE
);

SEQUENCES AND TRIGGERS CREATION:


Table VET_IN Sequence and Trigger
CREATE SEQUENCE VET_IN_SEQ NOCACHE;

create or replace TRIGGER VET_IN_TRG
BEFORE INSERT ON VET_IN
FOR EACH ROW
BEGIN
IF :NEW.VET_ID IS NULL THEN
SELECT VET_IN_SEQ.NEXTVAL INTO :NEW.VET_ID FROM DUAL;
END IF;
END;

Table VET_OUT Sequence and Trigger
CREATE SEQUENCE VET_OUT_SEQ NOCACHE;

create or replace TRIGGER VET_OUT_TRG
BEFORE INSERT ON VET_OUT
FOR EACH ROW
BEGIN
IF :NEW.VET_ID IS NULL THEN
SELECT VET_OUT_SEQ.NEXTVAL INTO :NEW.VET_ID FROM DUAL;
END IF;
end;

INSERT TEST DATA:


inserting into VET_IN
REM INSERTING into VET_IN
Insert into VET_IN (VET_ID,NAME,QUALIFICATION,SALARY) values (1,'Ashitraj more','mvsc',35000);
Insert into VET_IN (VET_ID,NAME,QUALIFICATION,SALARY) values (2,'Raj','bvsc',30000);
Insert into VET_IN (VET_ID,NAME,QUALIFICATION,SALARY) values (4,'Rakesh','mvsc',29000);

inserting into VET_OUT
REM INSERTING into VET_OUT
Insert into VET_OUT (VET_ID,NAME,COUNTRY,VISITING_FEES,QUALIFICATION) values (3,'Steven','UK',500,'mvsc');
Insert into VET_OUT (VET_ID,NAME,COUNTRY,VISITING_FEES,QUALIFICATION) values (5,'John','US',450,'mvsc');

CLASS CREATION:


ConcreteVet Class
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ConcreteVet implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Basic(optional = false)
@Column(name = "VET_ID")
private Integer vetId;
@Column(name = "NAME")
private String name;
@Column(name = "QUALIFICATION")
private String qualification;
//generate getters, setters, toString(), hashCode(),equals()
}

ConcreteInVet Class
@Entity
@Table(name = "VET_IN")
public class ConcreteInVet extends ConcreteVet{

@Column(name = "SALARY")
private Integer salary;
//generate getters, setters, toString(), hashCode(),equals()
}

ConcreteOutVet Class
@Entity
@Table(name = "VET_OUT")
public class ConcreteOutVet extends ConcreteVet{

@Column(name = "COUNTRY")
private String country;
@Column(name = "VISITING_FEES")
private Integer visitingFees;
//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 {
em.close();
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 testConcreteStrategyInsert() {

ConcreteInVet inVet = new ConcreteInVet();
inVet.setName("Invet name 10");
inVet.setQualification("invet Qualification 10");
inVet.setSalary(1010);
inVet.setVetId(10);
em.persist(inVet);
System.out.println("InHouseVet inserted");

ConcreteOutVet extVet = new ConcreteOutVet();
extVet.setName("extVet name 11");
extVet.setQualification("extVet Qualification 11");
extVet.setCountry("xy");
extVet.setVisitingFees(1111);
extVet.setVetId(11);
em.persist(extVet);
System.out.println("ExternatVet inserted");
}

@Test
@Ignore
public void testConcreteStrategySelect() {

ConcreteVet vet = em.find(ConcreteVet.class, 10);
assertNotNull(vet);

if (vet instanceof ConcreteInVet) {
ConcreteInVet concreteInVet = (ConcreteInVet) vet;
System.out.println(concreteInVet);
} else if (vet instanceof ConcreteOutVet) {
ConcreteOutVet concreteOutVet = (ConcreteOutVet) vet;
System.out.println(concreteOutVet);
} else {
System.out.println("ERROR in Type");
}

ConcreteVet vet2 = em.find(ConcreteVet.class, 11);
assertNotNull(vet2);

if (vet2 instanceof ConcreteInVet) {
ConcreteInVet concreteInVet = (ConcreteInVet) vet2;
System.out.println(concreteInVet);
} else if (vet2 instanceof ConcreteOutVet) {
ConcreteOutVet concreteOutVet = (ConcreteOutVet) vet2;
System.out.println(concreteOutVet);
} else {
System.out.println("ERROR in Type");
}

}

@Test
@Ignore
public void testConcreteStrategyUpdate() {

ConcreteVet vet = em.find(ConcreteVet.class, 10);
assertNotNull(vet);

if (vet instanceof ConcreteOutVet) {
ConcreteOutVet concreteOutVet = (ConcreteOutVet) vet;
concreteOutVet.setName("extVet Qualification 10 updated");
concreteOutVet.setVisitingFees(101010);
em.merge(concreteOutVet);
System.out.println(concreteOutVet);
} else if (vet instanceof ConcreteInVet) {
ConcreteInVet concreteInVet = (ConcreteInVet) vet;
concreteInVet.setName("Invet name 10 updated");
concreteInVet.setSalary(1111);
em.merge(concreteInVet);
System.out.println(concreteInVet);
} else {
System.out.println("ERROR in Type");
}

ConcreteVet vet2 = em.find(ConcreteVet.class, 11);
assertNotNull(vet2);

if (vet2 instanceof ConcreteOutVet) {
ConcreteOutVet concreteOutVet = (ConcreteOutVet) vet2;
concreteOutVet.setName("extVet Qualification 11 updated");
concreteOutVet.setVisitingFees(101010);
em.merge(concreteOutVet);
System.out.println(concreteOutVet);
} else if (vet2 instanceof ConcreteInVet) {
ConcreteInVet concreteInVet = (ConcreteInVet) vet2;
concreteInVet.setName("extVet name 11 updated");
concreteInVet.setSalary(1111);
em.merge(concreteInVet);
System.out.println(concreteInVet);
} else {
System.out.println("ERROR in Type");
}


}

@Test
@Ignore
public void testConcreteStrategyDelete() {

ConcreteVet vet = em.find(ConcreteVet.class, 10);
assertNotNull(vet);
em.remove(vet);
System.out.println("InHouseVet 10 : deleteds");

ConcreteVet vet2 = em.find(ConcreteVet.class, 11);
assertNotNull(vet2);

if (vet2 instanceof ConcreteOutVet) {
ConcreteOutVet concreteOutVet = (ConcreteOutVet) vet2;
em.remove(concreteOutVet);
System.out.println("ExternatVet 11 : deleted");
} else if (vet2 instanceof ConcreteInVet) {
ConcreteInVet concreteInVet = (ConcreteInVet) vet2;
em.remove(concreteInVet);
System.out.println("InHouseVet 11 : deleteds");
} else {
System.out.println("ERROR in Type");
}
}
}