Wednesday, 3 July 2019

JPA: Supported Data Types


As per JPA 2.2, the persistent fields of an entity should be of below type.
a.   Java primitive types (byte, short, int, long, float, double, boolean, char)
b.   Java.lang.String type
c.    All the other serializable types, including all the wrapper classes of primitive types, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], java.time.LocalDate, java.time.LocalTime, java.time.LocalDateTime, java.time.OffsetTime, java.time.OffsetDateTime etc.,
d.   Any user defined types that implement the Serializable interface
e.   enums, entity types, collections of entity types, embeddable classes, collections of basic and embeddable types.


Find the below working application.

Address.java
package com.sample.myApp.entities;

import java.io.Serializable;

public class Address implements Serializable {

 private static final long serialVersionUID = 1L;

 private String houseNum;
 private String streetName;
 private String city;
 private String state;
 private String country;

 public String getHouseNum() {
  return houseNum;
 }

 public void setHouseNum(String houseNum) {
  this.houseNum = houseNum;
 }

 public String getStreetName() {
  return streetName;
 }

 public void setStreetName(String streetName) {
  this.streetName = streetName;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getState() {
  return state;
 }

 public void setState(String state) {
  this.state = state;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  return "Address [houseNum=" + houseNum + ", streetName=" + streetName + ", city=" + city
    + ", state=" + state + ", country=" + country + "]";
 }

}


Employee.java
package com.sample.myApp.entities;

import javax.persistence.Entity;
import javax.persistence.Id;

import com.sample.myApp.entities.Address;

@Entity(name = "MY_EMPLOYEE")
public class Employee {

 @Id
 private int id;
 private String firstName;
 private String lastName;

 private Address tempAddress;
 private Address permAddress;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public Address getTempAddress() {
  return tempAddress;
 }

 public void setTempAddress(Address tempAddress) {
  this.tempAddress = tempAddress;
 }

 public Address getPermAddress() {
  return permAddress;
 }

 public void setPermAddress(Address permAddress) {
  this.permAddress = permAddress;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", tempAddress="
    + tempAddress + ", permAddress=" + permAddress + "]";
 }

}


persistence.xml
<?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="myPersistenceUnit"
  transaction-type="RESOURCE_LOCAL">
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

  <class>com.sample.myApp.entities.Employee</class>

  <properties>
   <property name="eclipselink.target-database" value="Derby" />
   <property name="javax.persistence.jdbc.driver"
    value="org.apache.derby.jdbc.ClientDriver" />
   <property name="javax.persistence.jdbc.url"
    value="jdbc:derby://localhost:1527/demoDatabase;create=true" />
   <property name="javax.persistence.jdbc.user" value="APP" />
   <property name="javax.persistence.jdbc.password" value="APP" />
   <property name="eclipselink.logging.level" value="FINE" />
   

   <property name="eclipselink.ddl-generation"
    value="drop-and-create-tables" />

   <property name="eclipselink.ddl-generation.output-mode"
    value="database" />
  </properties>

 </persistence-unit>
</persistence>


TestApp.java
package com.sample.myApp;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.sample.myApp.entities.Address;
import com.sample.myApp.entities.Employee;

public class TestApp {

 public static void main(String args[]) {
  EntityManagerFactory emf = null;
  EntityManager em = null;

  try {
   emf = Persistence.createEntityManagerFactory("myPersistenceUnit");

   em = emf.createEntityManager();

   Address tempAddress = new Address();
   Address permAddress = new Address();

   tempAddress.setCity("temp_city");
   tempAddress.setCountry("temp_country");
   tempAddress.setHouseNum("temp_house_num");
   tempAddress.setState("temp_state");
   tempAddress.setStreetName("temp_street");

   permAddress.setCity("perm_city");
   permAddress.setCountry("perm_country");
   permAddress.setHouseNum("perm_house_num");
   permAddress.setState("perm_state");
   permAddress.setStreetName("perm_street");

   Employee emp = new Employee();
   emp.setFirstName("Krishna");
   emp.setLastName("Gurram");
   emp.setTempAddress(tempAddress);
   emp.setPermAddress(permAddress);
   
   em.getTransaction().begin();
   em.persist(emp);
   em.getTransaction().commit();

   Query q = em.createQuery("select e from MY_EMPLOYEE e",
     Employee.class);
   List<Employee> emps = q.getResultList();
   for (Employee emp1 : emps) {
    System.out.println(emp1);
   }
  } finally {
   if (em != null && em.isOpen()) {
    em.close();
   }

   if (emf != null && emf.isOpen()) {
    emf.close();
   }
  }

 }

}


Project structure looks like below.


Run TestApp.java, you can able to see below messages in the console.

Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.7.1.v20171221-bd47e8f): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error.  Class [org.apache.derby.jdbc.ClientDriver] not found.
 at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:89)
 at org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267)
 at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:85)
 at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:170)
 at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:346)
 at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:313)
 at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connect(DatasourceAccessor.java:434)
 at org.eclipse.persistence.sessions.server.ConnectionPool.buildConnection(ConnectionPool.java:217)
 at org.eclipse.persistence.sessions.server.ConnectionPool.startUp(ConnectionPool.java:505)
 at org.eclipse.persistence.sessions.server.ServerSession.connect(ServerSession.java:519)
 at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:819)
 at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:256)
 at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:759)
 at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:216)
 at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:324)
 at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:348)
 at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:311)
 at com.sample.app.Test.main(Test.java:16)


Previous                                                    Next                                                    Home

No comments:

Post a Comment