@Column annotation specifies 'scale' attribute, by using
this you can specify the number of digits after decimal point.
Example
@Column(name = "EMPLOYEE_SALARY", precision =
10, scale = 2)
private BigDecimal salary;
Precision and Scale
Precision specifies total number of digits in a number
scale specifies number of digits after the decimal point.
Example 1
precision 6
scale 2
1234.54, 5432.12 are the valid numbers.
Example 2
precision 5
scale 0
12345, 65432, 98765 are the valid numbers
Find the below working application
Employee.java
package com.sample.myApp.entities; import java.math.BigDecimal; 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; @Column(name = "EMPLOYEE_SALARY", precision = 10, scale = 2) private BigDecimal salary; 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; } public BigDecimal getSalary() { return salary; } public void setSalary(BigDecimal salary) { this.salary = salary; } }
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.math.BigDecimal; 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......"); emp.setSalary(new BigDecimal("12345.87")); 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-29 09:52:03.038--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(30) UNIQUE, EMPLOYEE_FIRST_NAME VARCHAR(30) NOT NULL, EMPLOYEE_LAST_NAME VARCHAR(30), EMPLOYEE_SALARY DECIMAL(10,2), PRIMARY KEY (EMPLOYEE_ID))") [EL Fine]: sql: 2018-06-29 09:52:03.224--ClientSession(1527953000)--Connection(365181913)--Thread(Thread[main,5,main])--INSERT INTO MY_EMPLOYEE (EMPLOYEE_ID, ABOUT_ME, EMPLOYEE_EMAIL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME, EMPLOYEE_SALARY) VALUES (?, ?, ?, ?, ?, ?) bind => [1, The will to win, the desire to succeed, the urge to reach your full potential......, abc@abc.com, Siva, Majety, 12345.87] [EL Finer]: transaction: 2018-06-29 09:52:03.241--ClientSession(1527953000)--Connection(365181913)--Thread(Thread[main,5,main])--commit transaction ************************************** [EL Finest]: query: 2018-06-29 09:52:03.448--UnitOfWork(2110756088)--Thread(Thread[main,5,main])--Execute query ReadAllQuery(referenceClass=Employee sql="SELECT EMPLOYEE_ID, ABOUT_ME, EMPLOYEE_EMAIL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME, EMPLOYEE_SALARY FROM MY_EMPLOYEE") [EL Finest]: connection: 2018-06-29 09:52:03.449--ServerSession(575335780)--Connection(365181913)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default]. [EL Fine]: sql: 2018-06-29 09:52:03.449--ServerSession(575335780)--Connection(365181913)--Thread(Thread[main,5,main])--SELECT EMPLOYEE_ID, ABOUT_ME, EMPLOYEE_EMAIL, EMPLOYEE_FIRST_NAME, EMPLOYEE_LAST_NAME, EMPLOYEE_SALARY FROM MY_EMPLOYEE [EL Finest]: connection: 2018-06-29 09:52:03.473--ServerSession(575335780)--Connection(365181913)--Thread(Thread[main,5,main])--Connection released to connection pool [default]. com.sample.myApp.entities.Employee@229f66ed **************************************
From the above image, you can confirm the precision and
scale values.
Good afternoon.
ReplyDeletePlease tell me how to change the precision and scale values that are used by default by JPA to create fields without using the @Column annotation?