You can specify length for string type columns. If you do
not specify any length, then default column length is 255 characters.
For example, for the above Employee entity, columns are
defined like below.
As you see above image, 255 is the colums size for
EMPLOYEE_EMIAL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME columns.
But in some cases, you may not require 255 characters,
for example, you can restrict the firstname, lastName and emails to maximum 30
characters. You can do this by using length attribute.
Example
@Column(name = "EMPLOYEE_FIRST_NAME", nullable
= false, length = 30)
private String firstName;
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, length = 30) private String firstName; @Column(name = "EMPLOYEE_LAST_NAME", nullable = true, length = 30) private String lastName; @Column(name = "EMPLOYEE_EMAIL", unique = true, length = 30) 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="INFO" /> <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(); } } } }
When you ran TestApp.java, you can able to see below
messages in console.
[EL Info]: 2018-06-29 09:38:19.934--ServerSession(1978869058)--EclipseLink, version: Eclipse Persistence Services - 2.7.1.v20171221-bd47e8f [EL Info]: connection: 2018-06-29 09:38:20.194--ServerSession(1978869058)--/file:/C:/Users/krishna/java9/jpaDemo/target/classes/_myPersistenceUnit login successful ************************************** 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......] ************************************** [EL Info]: connection: 2018-06-29 09:38:20.703--ServerSession(1978869058)--/file:/C:/Users/krishna/java9/jpaDemo/target/classes/_myPersistenceUnit logout successful
From the above image, you can confirm that the column
sizes for EMPLOYEE_EMIAL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME are set to 30
characters.
No comments:
Post a Comment