【问题标题】:How to get inet in entity class in spring using hibernate如何在春季使用hibernate在实体类中获取inet
【发布时间】:2017-02-04 08:44:58
【问题描述】:
public List getAllEmployees() 
{
    return sessionFactory.openSession().createSQLQuery("select * from employee order by eid").list();
}

employee 表有一列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 课。我已将ipadres 作为字符串,但它给了我以下异常。

org.hibernate.MappingException:没有 JDBC 类型的方言映射:1111

【问题讨论】:

    标签: java spring postgresql hibernate inet


    【解决方案1】:

    当我必须创建自定义 Money 类型时,我遇到了类似的问题。

    解决方案是创建您自己的实现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;
    

    【讨论】:

    • 谢谢兄弟!!!我见过类似的解决方案,正在寻找一个简单的解决方案..nvm 非常感谢@Maciej Kowalski
    • 很高兴我能帮上忙
    【解决方案2】:

    要完成Maciej的答案,即正确,请添加:

    在 hibernate 4+ 的版本中,方法 nullSafeGetnullSafeSet 将是:

    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);
    }
    

    也说可以在package-info.java中定义类型

    @org.hibernate.annotations.TypeDef(name = "inetType", typeClass = InetType.class)
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      相关资源
      最近更新 更多