Saturday 6 July 2019

JPA: @Column: columnDefinition attribute


By using 'columnDefinition' attribute, you can specify the SQL fragment that is used when generating the DDL for the column.

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

         @Id
         @GeneratedValue
         @Column(name = "EMPLOYEE_ID")
         private int id;

         @Column(name = "EMPLOYEE_FIRST_NAME", nullable = false)
         private String firstName;

         @Column(name = "EMPLOYEE_LAST_NAME", nullable = true)
         private String lastName;

         @Column(name = "EMPLOYEE_EMAIL", unique = true)
         private String email;

         @Column(name = "ABOUT_ME", columnDefinition = "CLOB NOT NULL")
         private String aboutMe;
        
         .....
         .....
}

As you see above example. I specified columnDefinition attribute to the property aboutMe. JPA adds the value "CLOB NOT NULL" at the time of table creation like below.

CREATE TABLE MY_EMPLOYEE (EMPLOYEE_ID INTEGER NOT NULL, ABOUT_ME CLOB NOT NULL, EMPLOYEE_EMAIL VARCHAR(255) UNIQUE, EMPLOYEE_FIRST_NAME VARCHAR(255) NOT NULL, EMPLOYEE_LAST_NAME VARCHAR(255), PRIMARY KEY (EMPLOYEE_ID))

Find the below working example.

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

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

 @Id
 @GeneratedValue
 @Column(name = "EMPLOYEE_ID")
 private int id;

 @Column(name = "EMPLOYEE_FIRST_NAME", nullable = false)
 private String firstName;

 @Column(name = "EMPLOYEE_LAST_NAME", nullable = true)
 private String lastName;

 @Column(name = "EMPLOYEE_EMAIL", unique = true)
 private String email;

 @Column(name = "ABOUT_ME", columnDefinition = "CLOB NOT NULL")
 private String aboutMe;

 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 String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getAboutMe() {
  return aboutMe;
 }

 public void setAboutMe(String aboutMe) {
  this.aboutMe = aboutMe;
 }

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

}


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="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 {

 private static void printEmployees(EntityManager em) {
  System.out.println("**************************************");
  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);
  }
  System.out.println("**************************************\n");
 }

 private static void addEmployee(Employee emp, EntityManager em) {
  em.getTransaction().begin();
  em.persist(emp);
  em.getTransaction().commit();
 }

 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("Siva");
   emp.setLastName("Majety");
   emp.setEmail("abc@abc.com");
   emp.setAboutMe("The will to win, the desire to succeed, the urge to reach your full potential......");

   addEmployee(emp, em);
   printEmployees(em);

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

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

 }

}


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

[EL Finest]: query: 2018-06-28 09:19:07.023--ServerSession(575335780)--Thread(Thread[main,5,main])--Execute query DataModifyQuery(sql="CREATE TABLE MY_EMPLOYEE (EMPLOYEE_ID INTEGER NOT NULL, ABOUT_ME CLOB NOT NULL, EMPLOYEE_EMAIL VARCHAR(255) UNIQUE, EMPLOYEE_FIRST_NAME VARCHAR(255) NOT NULL, EMPLOYEE_LAST_NAME VARCHAR(255), PRIMARY KEY (EMPLOYEE_ID))")

[EL Fine]: sql: 2018-06-28 09:19:07.148--ClientSession(757004314)--Connection(1984990929)--Thread(Thread[main,5,main])--INSERT INTO MY_EMPLOYEE (EMPLOYEE_ID, ABOUT_ME, EMPLOYEE_EMAIL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME) VALUES (?, ?, ?, ?, ?)
 bind => [1, The will to win, the desire to succeed, the urge to reach your full potential......, abc@abc.com, Siva, Majety]

**************************************
[EL Finest]: query: 2018-06-28 09:19:07.345--UnitOfWork(328827614)--Thread(Thread[main,5,main])--Execute query ReadAllQuery(referenceClass=Employee sql="SELECT EMPLOYEE_ID, ABOUT_ME, EMPLOYEE_EMAIL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME FROM MY_EMPLOYEE")
[EL Finest]: connection: 2018-06-28 09:19:07.346--ServerSession(575335780)--Connection(1984990929)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: sql: 2018-06-28 09:19:07.346--ServerSession(575335780)--Connection(1984990929)--Thread(Thread[main,5,main])--SELECT EMPLOYEE_ID, ABOUT_ME, EMPLOYEE_EMAIL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME FROM MY_EMPLOYEE
[EL Finest]: connection: 2018-06-28 09:19:07.385--ServerSession(575335780)--Connection(1984990929)--Thread(Thread[main,5,main])--Connection released to connection pool [default].


Employee [id=1, firstName=Siva, lastName=Majety, email=abc@abc.com, aboutMe=The will to win, the desire to succeed, the urge to reach your full potential......]
**************************************



Previous                                                    Next                                                    Home

No comments:

Post a Comment