Skip to content

Commit 42b9f67

Browse files
committed
Starting a new sample for @converter and @convert
1 parent 6b614f9 commit 42b9f67

11 files changed

Lines changed: 395 additions & 0 deletions

File tree

jpa/jpa-converter/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jpa</groupId>
6+
<artifactId>jpa-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>jpa-converter</artifactId>
12+
<packaging>war</packaging>
13+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.javaee7.jpa.converter;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Column;
5+
import javax.persistence.Convert;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.NamedQueries;
9+
import javax.persistence.NamedQuery;
10+
import javax.persistence.Table;
11+
12+
/**
13+
* @author Arun Gupta
14+
*/
15+
@Entity
16+
@Table(name="EMPLOYEE_SCHEMA_CONVERTER")
17+
@NamedQueries({
18+
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
19+
})
20+
public class Employee implements Serializable {
21+
private static final long serialVersionUID = 1L;
22+
@Id
23+
private int id;
24+
25+
@Column(length=50)
26+
private String name;
27+
28+
@Convert(converter = EmployeeDateConverter.class)
29+
private String dob;
30+
31+
public Employee() { }
32+
33+
public Employee(String name) {
34+
this.name = name;
35+
}
36+
37+
public Employee(int id, String name, String dob) {
38+
this.id = id;
39+
this.name = name;
40+
this.dob = dob;
41+
}
42+
43+
public int getId() {
44+
return id;
45+
}
46+
47+
public void setId(int id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public String getDob() {
60+
return dob;
61+
}
62+
63+
public void setDob(String dob) {
64+
this.dob = dob;
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return name + "(" + dob + ")";
70+
}
71+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javaee7.jpa.converter;
2+
3+
import java.util.List;
4+
import javax.ejb.Stateless;
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
8+
/**
9+
* @author Arun Gupta
10+
*/
11+
@Stateless
12+
public class EmployeeBean {
13+
14+
@PersistenceContext
15+
EntityManager em;
16+
17+
public void persist(Employee e) {
18+
em.persist(e);
19+
}
20+
21+
public List<Employee> get() {
22+
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package org.javaee7.jpa.converter;
8+
9+
import java.text.ParseException;
10+
import java.text.SimpleDateFormat;
11+
import java.util.Date;
12+
import javax.persistence.AttributeConverter;
13+
import javax.persistence.Converter;
14+
15+
/**
16+
* @author Arun Gupta
17+
*/
18+
@Converter
19+
public class EmployeeDateConverter implements AttributeConverter<MyDate, String> {
20+
final SimpleDateFormat format = new SimpleDateFormat("M/dd/yy");
21+
22+
@Override
23+
public String convertToDatabaseColumn(MyDate attribute) {
24+
return attribute.getDate().toString();
25+
}
26+
27+
@Override
28+
public MyDate convertToEntityAttribute(String date) {
29+
Date d = null;
30+
try {
31+
d = format.parse(date);
32+
} catch (ParseException ex) {
33+
ex.printStackTrace();
34+
}
35+
return new MyDate(d);
36+
}
37+
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package org.javaee7.jpa.converter;
8+
9+
import java.util.Date;
10+
11+
/**
12+
* @author Arun Gupta
13+
*/
14+
public class MyDate {
15+
Date date;
16+
17+
public MyDate() {
18+
}
19+
20+
public MyDate(Date date) {
21+
this.date = date;
22+
}
23+
24+
public Date getDate() {
25+
return date;
26+
}
27+
28+
public void setDate(Date date) {
29+
this.date = date;
30+
}
31+
32+
33+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jpa.converter;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.inject.Inject;
45+
import javax.servlet.ServletException;
46+
import javax.servlet.annotation.WebServlet;
47+
import javax.servlet.http.HttpServlet;
48+
import javax.servlet.http.HttpServletRequest;
49+
import javax.servlet.http.HttpServletResponse;
50+
51+
/**
52+
* @author Arun Gupta
53+
*/
54+
@WebServlet(urlPatterns = {"/TestServlet"})
55+
public class TestServlet extends HttpServlet {
56+
57+
@Inject
58+
EmployeeBean bean;
59+
60+
/**
61+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
62+
* methods.
63+
*
64+
* @param request servlet request
65+
* @param response servlet response
66+
* @throws ServletException if a servlet-specific error occurs
67+
* @throws IOException if an I/O error occurs
68+
*/
69+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
70+
throws ServletException, IOException {
71+
response.setContentType("text/html;charset=UTF-8");
72+
PrintWriter out = response.getWriter();
73+
out.println("<html>");
74+
out.println("<head>");
75+
out.println("<title>JPA 2.1 Converter</title>");
76+
out.println("</head>");
77+
out.println("<body>");
78+
out.println("<h1>JPA 2.1 Converter</h1>");
79+
out.println("Original list of employees");
80+
for (Employee e : bean.get()) {
81+
out.println(e + "<br>");
82+
}
83+
out.println("<h3>Adding a new employee</h3>");
84+
Employee emp = new Employee(8, "Lucy", "3/31/1980");
85+
bean.persist(emp);
86+
out.println("Updated list of employees");
87+
for (Employee e : bean.get()) {
88+
out.println(e + "<br>");
89+
}
90+
out.println("</body>");
91+
out.println("</html>");
92+
}
93+
94+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
95+
/**
96+
* Handles the HTTP <code>GET</code> method.
97+
*
98+
* @param request servlet request
99+
* @param response servlet response
100+
* @throws ServletException if a servlet-specific error occurs
101+
* @throws IOException if an I/O error occurs
102+
*/
103+
@Override
104+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
105+
throws ServletException, IOException {
106+
processRequest(request, response);
107+
}
108+
109+
/**
110+
* Handles the HTTP <code>POST</code> method.
111+
*
112+
* @param request servlet request
113+
* @param response servlet response
114+
* @throws ServletException if a servlet-specific error occurs
115+
* @throws IOException if an I/O error occurs
116+
*/
117+
@Override
118+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
119+
throws ServletException, IOException {
120+
processRequest(request, response);
121+
}
122+
123+
/**
124+
* Returns a short description of the servlet.
125+
*
126+
* @return a String containing servlet description
127+
*/
128+
@Override
129+
public String getServletInfo() {
130+
return "Short description";
131+
}// </editor-fold>
132+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE EMPLOYEE_SCHEMA_CONVERTER ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null, "DOB" VARCHAR(10) not null)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE EMPLOYEE_SCHEMA_CONVERTER
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (1, 'Leonard', '12/9/1980')
2+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (2, 'Sheldon', '3/24/1973')
3+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (3, 'Penny', '11/30/1985')
4+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (4, 'Raj', '4/30/1975')
5+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (5, 'Howard', '12/9/1980')
6+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (6, 'Bernadette', '6/23/1980')
7+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "DOB") VALUES (7, 'Amy', '12/12/1975')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence
3+
version="2.1"
4+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
7+
<persistence-unit name="MyPU" transaction-type="JTA">
8+
<properties>
9+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
10+
<property name="javax.persistence.schema-generation.create-source" value="script"/>
11+
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
12+
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/>
13+
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop.sql"/>
14+
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
15+
<property name="eclipselink.logging.level" value="FINE"/>
16+
</properties>
17+
</persistence-unit>
18+
</persistence>

0 commit comments

Comments
 (0)