如何使用休眠在 spring 中的实体 class 中获取 inet
How to get inet in entity class in spring using hibernate
public List getAllEmployees()
{
return sessionFactory.openSession().createSQLQuery("select * from employee order by eid").list();
}
员工 table 有一个列 ipadres
,在 postgresql 中类型是 inet
。
@Entity
@Table(name="employee")
public class Employee {
@Id
@Column(name="eid")
private int eid;
private int dept_id;
private String name;
private String address;
private String project;
private String password;
@Column(name="ipadres")
private String ipadres;
private double salary;
private Date Doj;
这是我的 pojo class。我已将 ipadres
作为字符串,但它给了我以下异常。
org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
当我必须创建自定义货币类型时,我遇到了类似的问题。
解决方案是创建您自己的 class 来实现 UserType
接口。
这是一篇关于此事的精彩文章:example
简而言之,您有兴趣实施以下方法:
import org.hibernate.usertype.UserType;
public InetType impelements UserType{
public Class<String> returnedClass() {
return String.class;
}
public int[] sqlTypes() {
return new int[] { Types.OTHER }; // as the db type is inet and not directly transmutable to hibernate type.
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
String value = (String) Hibernate.STRING.nullSafeGet(resultSet, names[0]);
return value;
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
Hibernate.STRING.nullSafeSet(preparedStatement,
(value != null) ? value.toString() : null, index);
}
}
然后在你的Employee实体中,你需要添加类型定义注解:
@Entity
@Table(name="employee")
@TypeDefs(value={@TypeDef(name="inetType",typeClass=InetType.class)})
public class Employee {
@Column(name="ipadres")
@Type(type="inetType")
private String ipadres;
要完成 Maciej 的正确答案,请添加:
在 hibernate 4+ 版本中,方法 nullSafeGet 和 nullSafeSet 将是:
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
final String value = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
return value;
}
public void nullSafeSet(PreparedStatement ps, Object value, int index,
SharedSessionContractImplementor session)
throws HibernateException, SQLException {
StandardBasicTypes.STRING.nullSafeSet(ps, (value != null) ?
value.toString(): null, index, session);
}
还说可以在包中定义类型-info.java like
@org.hibernate.annotations.TypeDef(name = "inetType", typeClass = InetType.class)
public List getAllEmployees()
{
return sessionFactory.openSession().createSQLQuery("select * from employee order by eid").list();
}
员工 table 有一个列 ipadres
,在 postgresql 中类型是 inet
。
@Entity
@Table(name="employee")
public class Employee {
@Id
@Column(name="eid")
private int eid;
private int dept_id;
private String name;
private String address;
private String project;
private String password;
@Column(name="ipadres")
private String ipadres;
private double salary;
private Date Doj;
这是我的 pojo class。我已将 ipadres
作为字符串,但它给了我以下异常。
org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
当我必须创建自定义货币类型时,我遇到了类似的问题。
解决方案是创建您自己的 class 来实现 UserType
接口。
这是一篇关于此事的精彩文章:example
简而言之,您有兴趣实施以下方法:
import org.hibernate.usertype.UserType;
public InetType impelements UserType{
public Class<String> returnedClass() {
return String.class;
}
public int[] sqlTypes() {
return new int[] { Types.OTHER }; // as the db type is inet and not directly transmutable to hibernate type.
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
String value = (String) Hibernate.STRING.nullSafeGet(resultSet, names[0]);
return value;
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
Hibernate.STRING.nullSafeSet(preparedStatement,
(value != null) ? value.toString() : null, index);
}
}
然后在你的Employee实体中,你需要添加类型定义注解:
@Entity
@Table(name="employee")
@TypeDefs(value={@TypeDef(name="inetType",typeClass=InetType.class)})
public class Employee {
@Column(name="ipadres")
@Type(type="inetType")
private String ipadres;
要完成 Maciej 的正确答案,请添加:
在 hibernate 4+ 版本中,方法 nullSafeGet 和 nullSafeSet 将是:
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
final String value = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
return value;
}
public void nullSafeSet(PreparedStatement ps, Object value, int index,
SharedSessionContractImplementor session)
throws HibernateException, SQLException {
StandardBasicTypes.STRING.nullSafeSet(ps, (value != null) ?
value.toString(): null, index, session);
}
还说可以在包中定义类型-info.java like
@org.hibernate.annotations.TypeDef(name = "inetType", typeClass = InetType.class)