Skip to content

Commit 49a7d9a

Browse files
committed
Temporal attributes cannot be used with @convert and so changing to CreditCard based upon Antonio's slide deck
1 parent 42b9f67 commit 49a7d9a

7 files changed

Lines changed: 84 additions & 89 deletions

File tree

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.util.Date;
10+
11+
/**
12+
* @author Arun Gupta
13+
*/
14+
public class CreditCard {
15+
String cardNumber;
16+
17+
public CreditCard() {
18+
}
19+
20+
public CreditCard(String cardNumber) {
21+
this.cardNumber = cardNumber;
22+
}
23+
24+
public String getCardNumber() {
25+
return cardNumber;
26+
}
27+
28+
public void setCardNumber(String cardNumber) {
29+
this.cardNumber = cardNumber;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return cardNumber;
35+
}
36+
37+
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 javax.persistence.AttributeConverter;
10+
import javax.persistence.Converter;
11+
12+
/**
13+
* @author Arun Gupta
14+
*/
15+
@Converter
16+
public class CreditCardConverter implements AttributeConverter<CreditCard, String> {
17+
18+
@Override
19+
public String convertToDatabaseColumn(CreditCard attribute) {
20+
return attribute.toString();
21+
}
22+
23+
@Override
24+
public CreditCard convertToEntityAttribute(String card) {
25+
return new CreditCard(card);
26+
}
27+
28+
}

jpa/jpa-converter/src/main/java/org/javaee7/jpa/converter/Employee.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.javaee7.jpa.converter;
22

33
import java.io.Serializable;
4+
import java.util.Date;
45
import javax.persistence.Column;
56
import javax.persistence.Convert;
67
import javax.persistence.Entity;
@@ -25,19 +26,19 @@ public class Employee implements Serializable {
2526
@Column(length=50)
2627
private String name;
2728

28-
@Convert(converter = EmployeeDateConverter.class)
29-
private String dob;
29+
@Convert(converter = CreditCardConverter.class)
30+
private String card;
3031

3132
public Employee() { }
3233

3334
public Employee(String name) {
3435
this.name = name;
3536
}
3637

37-
public Employee(int id, String name, String dob) {
38+
public Employee(int id, String name, String card) {
3839
this.id = id;
3940
this.name = name;
40-
this.dob = dob;
41+
this.card = card;
4142
}
4243

4344
public int getId() {
@@ -56,16 +57,16 @@ public void setName(String name) {
5657
this.name = name;
5758
}
5859

59-
public String getDob() {
60-
return dob;
60+
public String getCard() {
61+
return card;
6162
}
6263

63-
public void setDob(String dob) {
64-
this.dob = dob;
64+
public void setCard(String card) {
65+
this.card = card;
6566
}
66-
67+
6768
@Override
6869
public String toString() {
69-
return name + "(" + dob + ")";
70+
return name + "(" + card + ")";
7071
}
7172
}

jpa/jpa-converter/src/main/java/org/javaee7/jpa/converter/EmployeeDateConverter.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

jpa/jpa-converter/src/main/java/org/javaee7/jpa/converter/MyDate.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CREATE TABLE EMPLOYEE_SCHEMA_CONVERTER ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null, "DOB" VARCHAR(10) not null)
1+
CREATE TABLE EMPLOYEE_SCHEMA_CONVERTER ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null, "CARD" VARCHAR(15) not null)
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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')
1+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "CARD") VALUES (1, 'Leonard', '11-22-33-44')
2+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "CARD") VALUES (2, 'Sheldon', '22-33-44-55')
3+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "CARD") VALUES (3, 'Penny', '33-44-55-66')
4+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "CARD") VALUES (4, 'Raj', '44-55-66-77')
5+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "CARD") VALUES (5, 'Howard', '55-66-77-88')
6+
INSERT INTO EMPLOYEE_SCHEMA_CONVERTER("ID", "NAME", "CARD") VALUES (6, 'Bernadette', '66-77-88-99')

0 commit comments

Comments
 (0)