Tuesday, 2 July 2019

JPA: Change the table name


In my previous post, I explained about the entities.

For example, I defined Employee entity like below.

@Entity
public class Employee {

         .....
         .....
        
}

One thing to note in the above definition is, table is created with the same name of the entity class.


You can customize the name of the table. @Entity annotation provides 'name' argument, by using this, you can customize the table name.

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

         .....
         .....

}

In the above example, I am informing JPA, to use table name as 'MY_EMPLOYEE' for the entity class Employee.

Find the below working applicaition.

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

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

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

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

 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;
 }

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

}


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

  <class>com.smaple.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="ALL" />

   <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.Employee;

public class TestApp {

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

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

   em = emf.createEntityManager();

   Employee emp = new Employee();
   emp.setFirstName("Krishna");
   emp.setLastName("Gurram");
   emp.setId(1);

   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();
   }
  }

 }

}


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

As you observe above image, table is created with name 'MY_EMPLOYEE'.

Points to note
a. To run any queries, you should use the entity name, not the class name.

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);
}

b. You can even perform conditional queries using JPA

Query q = em.createQuery("select e from MY_EMPLOYEE e WHERE e.firstName='Krishna' AND e.lastName='Gurram'", Employee.class);

Previous                                                    Next                                                    Home

No comments:

Post a Comment