Compare commits
1 Commits
revert-146
...
hibernate-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e89c487555 |
@@ -34,7 +34,7 @@ public class OfficeEmployee {
|
||||
@AttributeOverride(name = "cityCode", column = @Column(name = "city_code")),
|
||||
@AttributeOverride(name = "number", column = @Column(name = "number"))
|
||||
})
|
||||
@Type(value = PhoneNumberType.class)
|
||||
@CompositeType(value = PhoneNumberType.class)
|
||||
private PhoneNumber employeeNumber;
|
||||
|
||||
@CompositeType(value = com.baeldung.hibernate.customtypes.AddressType.class)
|
||||
@@ -48,11 +48,7 @@ public class OfficeEmployee {
|
||||
private Address empAddress;
|
||||
|
||||
@Type(value = com.baeldung.hibernate.customtypes.SalaryType.class,
|
||||
parameters = {@Parameter(name = "currency", value = "USD")})
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "amount", column = @Column(name = "amount")),
|
||||
@AttributeOverride(name = "currency", column = @Column(name = "currency"))
|
||||
})
|
||||
parameters = {@Parameter(name = "currency", value = "USD")})
|
||||
private Salary salary;
|
||||
|
||||
public Salary getSalary() {
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.metamodel.spi.ValueAccess;
|
||||
import org.hibernate.type.CompositeType;
|
||||
import org.hibernate.usertype.CompositeUserType;
|
||||
import org.hibernate.usertype.UserType;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -10,7 +15,7 @@ import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PhoneNumberType implements UserType<PhoneNumber> {
|
||||
public class PhoneNumberType implements UserType<PhoneNumber>, CompositeUserType<PhoneNumber> {
|
||||
|
||||
@Override
|
||||
public int getSqlType() {
|
||||
@@ -90,4 +95,31 @@ public class PhoneNumberType implements UserType<PhoneNumber> {
|
||||
public PhoneNumber replace(PhoneNumber detached, PhoneNumber managed, Object owner) {
|
||||
return detached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getPropertyValue(PhoneNumber component, int property) throws HibernateException {
|
||||
switch (property) {
|
||||
case 0:
|
||||
return component.getCountryCode();
|
||||
case 1:
|
||||
return component.getCityCode();
|
||||
case 2:
|
||||
return component.getNumber();
|
||||
default:
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PhoneNumber instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> embeddable() {
|
||||
return PhoneNumber.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Salary {
|
||||
public class Salary implements Serializable {
|
||||
|
||||
private Long amount;
|
||||
private String currency;
|
||||
|
||||
@@ -16,38 +16,13 @@ import java.sql.Types;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SalaryType implements UserType<Salary>, CompositeUserType<Salary>, DynamicParameterizedType {
|
||||
public class SalaryType implements UserType<Salary>, DynamicParameterizedType {
|
||||
|
||||
private String localCurrency;
|
||||
|
||||
@Override
|
||||
public Object getPropertyValue(Salary component, int property) throws HibernateException {
|
||||
|
||||
switch (property) {
|
||||
case 0:
|
||||
return component.getAmount();
|
||||
case 1:
|
||||
return component.getCurrency();
|
||||
default:
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Salary instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> embeddable() {
|
||||
return Salary.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSqlType() {
|
||||
return Types.BIGINT;
|
||||
return Types.VARCHAR;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,13 +48,17 @@ public class SalaryType implements UserType<Salary>, CompositeUserType<Salary>,
|
||||
|
||||
@Override
|
||||
public Salary nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
|
||||
Salary salary = new Salary();
|
||||
salary.setAmount(rs.getLong(position));
|
||||
|
||||
|
||||
if (rs.wasNull())
|
||||
return null;
|
||||
|
||||
Salary salary = new Salary();
|
||||
|
||||
String salaryValue = rs.getString(position);
|
||||
|
||||
salary.setAmount(Long.parseLong(salaryValue.split(" ")[1]));
|
||||
|
||||
salary.setCurrency(rs.getString(position));
|
||||
salary.setCurrency(salaryValue.split(" ")[1]);
|
||||
|
||||
return salary;
|
||||
}
|
||||
@@ -87,13 +66,12 @@ public class SalaryType implements UserType<Salary>, CompositeUserType<Salary>,
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Salary value, int index, SharedSessionContractImplementor session) throws SQLException {
|
||||
if (Objects.isNull(value))
|
||||
st.setNull(index, Types.BIGINT);
|
||||
st.setNull(index, Types.VARCHAR);
|
||||
else {
|
||||
|
||||
st.setLong(index, SalaryCurrencyConvertor.convert(
|
||||
value.getAmount(),
|
||||
value.getCurrency(), localCurrency));
|
||||
st.setString(index + 1, value.getCurrency());
|
||||
Long salaryValue = SalaryCurrencyConvertor.convert(
|
||||
value.getAmount(),
|
||||
value.getCurrency(), localCurrency);
|
||||
st.setString(index, value.getCurrency()+ " " + salaryValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public class HibernateCustomTypesIntegrationTest {
|
||||
boolean contains = session.contains(e);
|
||||
assertTrue(contains);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user