mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-03 00:23:24 +00:00
#827 - ENH: Add support for AttributeConverter from JPA 2.1
This commit is contained in:
1 parent
2ab98a922d
commit
7f31104439
6 files changed
+162
-12
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package com.avaje.tests.model.ivo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class ESomeConvertType {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
Rate rate;
|
||||
|
||||
public ESomeConvertType(String name, Rate rate) {
|
||||
this.name = name;
|
||||
this.rate = rate;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Rate getRate() {
|
||||
return rate;
|
||||
}
|
||||
|
||||
public void setRate(Rate rate) {
|
||||
this.rate = rate;
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,23 @@ package com.avaje.tests.model.ivo.converter;
|
||||
|
||||
import com.avaje.tests.model.ivo.Rate;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Converts between Rate and BigDecimal.
|
||||
*/
|
||||
public class RateTypeConverter {//implements ScalarTypeConverter<Rate,BigDecimal>{
|
||||
@Converter
|
||||
public class RateTypeConverter implements AttributeConverter<Rate,BigDecimal> {
|
||||
|
||||
public BigDecimal unwrapValue(Rate beanType) {
|
||||
return beanType.getValue();
|
||||
@Override
|
||||
public BigDecimal convertToDatabaseColumn(Rate attribute) {
|
||||
return attribute.getValue();
|
||||
}
|
||||
|
||||
public Rate wrapValue(BigDecimal scalarType) {
|
||||
return new Rate(scalarType);
|
||||
@Override
|
||||
public Rate convertToEntityAttribute(BigDecimal dbData) {
|
||||
return new Rate(dbData);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.avaje.tests.model.ivo.test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.ivo.ESomeConvertType;
|
||||
import com.avaje.tests.model.ivo.Rate;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestAttributeConverter extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insertUpdateDelete() {
|
||||
|
||||
ESomeConvertType bean = new ESomeConvertType("one", new Rate(20.1));
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
ESomeConvertType found = Ebean.find(ESomeConvertType.class, bean.getId());
|
||||
found.setRate(new Rate("40"));
|
||||
Ebean.save(found);
|
||||
|
||||
Ebean.delete(found);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user