Skip to content

Commit 8ada19d

Browse files
authored
Merge pull request eugenp#3576 from asturcon/tutorials/jpaAttributeConverters
BAEL-1509 - JPA Attribute Converters
2 parents 18f77c0 + 796277b commit 8ada19d

5 files changed

Lines changed: 179 additions & 0 deletions

File tree

‎hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
8181
metadataSources.addAnnotatedClass(Bag.class);
8282
metadataSources.addAnnotatedClass(PointEntity.class);
8383
metadataSources.addAnnotatedClass(PolygonEntity.class);
84+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
8485

8586
Metadata metadata = metadataSources.buildMetadata();
8687
return metadata.getSessionFactoryBuilder()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.hibernate.converters;
2+
3+
import javax.persistence.AttributeConverter;
4+
import javax.persistence.Converter;
5+
6+
import com.baeldung.hibernate.pojo.PersonName;
7+
8+
@Converter
9+
public class PersonNameConverter implements AttributeConverter<PersonName, String> {
10+
11+
private static final String SEPARATOR = ", ";
12+
13+
@Override
14+
public String convertToDatabaseColumn(PersonName person) {
15+
StringBuilder sb = new StringBuilder();
16+
if (person.getSurname() != null) {
17+
sb.append(person.getSurname());
18+
sb.append(SEPARATOR);
19+
}
20+
21+
if (person.getName() != null) {
22+
sb.append(person.getName());
23+
}
24+
25+
return sb.toString();
26+
}
27+
28+
@Override
29+
public PersonName convertToEntityAttribute(String dbPerson) {
30+
String[] pieces = dbPerson.split(SEPARATOR);
31+
32+
if (pieces == null || pieces.length != 2) {
33+
return null;
34+
}
35+
36+
PersonName personName = new PersonName();
37+
personName.setSurname(pieces[0]);
38+
personName.setName(pieces[1]);
39+
40+
return personName;
41+
}
42+
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Convert;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
8+
import com.baeldung.hibernate.converters.PersonNameConverter;
9+
10+
@Entity(name = "PersonTable")
11+
public class Person {
12+
13+
@Id
14+
@GeneratedValue
15+
private Long id;
16+
17+
@Convert(converter = PersonNameConverter.class)
18+
private PersonName personName;
19+
20+
public PersonName getPersonName() {
21+
return personName;
22+
}
23+
24+
public void setPersonName(PersonName personName) {
25+
this.personName = personName;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import java.io.Serializable;
4+
5+
public class PersonName implements Serializable {
6+
7+
private static final long serialVersionUID = 7883094644631050150L;
8+
9+
private String name;
10+
11+
private String surname;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public String getSurname() {
22+
return surname;
23+
}
24+
25+
public void setSurname(String surname) {
26+
this.surname = surname;
27+
}
28+
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.baeldung.hibernate.converter;
2+
3+
import java.io.IOException;
4+
5+
import org.hibernate.Session;
6+
import org.hibernate.Transaction;
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import com.baeldung.hibernate.HibernateUtil;
12+
import com.baeldung.hibernate.pojo.Person;
13+
import com.baeldung.hibernate.pojo.PersonName;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
public class PersonNameConverterTest {
18+
19+
private Session session;
20+
private Transaction transaction;
21+
22+
@Before
23+
public void setUp() throws IOException {
24+
session = HibernateUtil.getSessionFactory()
25+
.openSession();
26+
transaction = session.beginTransaction();
27+
28+
session.createNativeQuery("delete from personTable")
29+
.executeUpdate();
30+
31+
transaction.commit();
32+
transaction = session.beginTransaction();
33+
}
34+
35+
@After
36+
public void tearDown() {
37+
transaction.rollback();
38+
session.close();
39+
}
40+
41+
@Test
42+
public void givenPersonName_WhenSaving_ThenNameAndSurnameConcat() {
43+
final String name = "name";
44+
final String surname = "surname";
45+
46+
PersonName personName = new PersonName();
47+
personName.setName(name);
48+
personName.setSurname(surname);
49+
50+
Person person = new Person();
51+
person.setPersonName(personName);
52+
53+
Long id = (Long) session.save(person);
54+
55+
session.flush();
56+
session.clear();
57+
58+
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
59+
.setParameter("id", id)
60+
.getSingleResult();
61+
62+
assertEquals(surname + ", " + name, dbPersonName);
63+
64+
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
65+
.setParameter("id", id)
66+
.getSingleResult();
67+
68+
assertEquals(dbPerson.getPersonName()
69+
.getName(), name);
70+
assertEquals(dbPerson.getPersonName()
71+
.getSurname(), surname);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)