JPA Attribute Converters

This commit is contained in:
Marcos 2018-02-03 19:01:20 +01:00
parent 710c25fb01
commit ecf1cae3f7
5 changed files with 181 additions and 0 deletions

View File

@ -81,6 +81,7 @@ public class HibernateUtil {
metadataSources.addAnnotatedClass(Bag.class); metadataSources.addAnnotatedClass(Bag.class);
metadataSources.addAnnotatedClass(PointEntity.class); metadataSources.addAnnotatedClass(PointEntity.class);
metadataSources.addAnnotatedClass(PolygonEntity.class); metadataSources.addAnnotatedClass(PolygonEntity.class);
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
Metadata metadata = metadataSources.buildMetadata(); Metadata metadata = metadataSources.buildMetadata();
return metadata.getSessionFactoryBuilder() return metadata.getSessionFactoryBuilder()

View File

@ -0,0 +1,43 @@
package com.baeldung.hibernate.converters;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import com.baeldung.hibernate.pojo.PersonName;
@Converter
public class PersonNameConverter implements AttributeConverter<PersonName, String> {
private static final String SEPARATOR = ", ";
@Override
public String convertToDatabaseColumn(PersonName person) {
StringBuilder sb = new StringBuilder();
if (person.getSurname() != null) {
sb.append(person.getSurname());
sb.append(SEPARATOR);
}
if (person.getName() != null) {
sb.append(person.getName());
}
return sb.toString();
}
@Override
public PersonName convertToEntityAttribute(String dbPerson) {
String[] pieces = dbPerson.split(SEPARATOR);
if (pieces == null || pieces.length != 2) {
return null;
}
PersonName personName = new PersonName();
personName.setSurname(pieces[0]);
personName.setName(pieces[1]);
return personName;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.hibernate.pojo;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import com.baeldung.hibernate.converters.PersonNameConverter;
@Entity(name = "PersonTable")
public class Person {
@Id
@GeneratedValue
private Long id;
@Convert(converter = PersonNameConverter.class)
private PersonName personName;
public PersonName getPersonName() {
return personName;
}
public void setPersonName(PersonName personName) {
this.personName = personName;
}
public Long getId() {
return id;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.hibernate.pojo;
import java.io.Serializable;
import javax.persistence.Entity;
@Entity
public class PersonName implements Serializable {
private static final long serialVersionUID = 7883094644631050150L;
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}

View File

@ -0,0 +1,73 @@
package com.baeldung.hibernate.converter;
import java.io.IOException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.hibernate.HibernateUtil;
import com.baeldung.hibernate.pojo.Person;
import com.baeldung.hibernate.pojo.PersonName;
import com.vividsolutions.jts.util.Assert;
public class PersonNameConverterTest {
private Session session;
private Transaction transaction;
@Before
public void setUp() throws IOException {
session = HibernateUtil.getSessionFactory()
.openSession();
transaction = session.beginTransaction();
session.createNativeQuery("delete from personTable")
.executeUpdate();
transaction.commit();
transaction = session.beginTransaction();
}
@After
public void tearDown() {
transaction.rollback();
session.close();
}
@Test
public void givenPersonName_WhenSaving_ThenNameAndSurnameConcat() {
final String name = "name";
final String surname = "surname";
PersonName personName = new PersonName();
personName.setName(name);
personName.setSurname(surname);
Person person = new Person();
person.setPersonName(personName);
Long id = (Long) session.save(person);
session.flush();
session.clear();
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
.setParameter("id", id)
.getSingleResult();
Assert.equals(surname + ", " + name, dbPersonName);
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
.setParameter("id", id)
.getSingleResult();
Assert.equals(dbPerson.getPersonName()
.getName(), name);
Assert.equals(dbPerson.getPersonName()
.getSurname(), surname);
}
}