ids的未知整数数据类型:java.lang.String

时间:2014-07-15 10:12:00

标签: java hibernate spring-mvc

这是我的hibernate-mapping:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="Entities.Product" table="products">
        <id column="name" name="name" type="java.lang.String">
            <generator class="increment"/>
        </id>
        <property column="cost" name="cost" type="java.lang.Integer"/>
    </class>
</hibernate-mapping>

该表由两列组成:name VARCHAR(20), cost Integer

控制器:

@Controller
public class ProductController {

    @RequestMapping("/products.htm")
    public String getAllProducts() throws SQLException
    {
        ProductDAOImpl mapping = new ProductDAOImpl();
        Product p = new Product();
        p.setCost(1000);
        p.setName("Саморезы");
        mapping.addProduct(p);
        return "index";
    }     
}

addProduct方法:

public void addProduct(Product product) throws SQLException {
        Session session = Hibernate.util.HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(product);
        session.getTransaction().commit();
        session.close();
    }

当我试图运行时,我有例外:

org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String
    org.hibernate.id.IdentifierGeneratorHelper.getIntegralDataTypeHolder(IdentifierGeneratorHelper.java:215)
    org.hibernate.id.IncrementGenerator.initializePreviousValueHolder(IncrementGenerator.java:123)
    org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:69)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:764)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:756)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:752)
    DAOImpl.ProductDAOImpl.addProduct(ProductDAOImpl.java:26)
    Controllers.ProductController.getAllProducts(ProductController.java:20)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

如何解决这个问题?

3 个答案:

答案 0 :(得分:11)

您已将ID添加为<generator class="increment"/>,然后必须为整数 在<id column="name" name="name" type="integer">使用整数而不是字符串,更改hibernate映射文件,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="Entities.Product" table="products">
        <id column="name" name="name" type="integer">
            <generator class="increment"/>
        </id>
        <property column="cost" name="cost" type="java.lang.Integer"/>
    </class>
</hibernate-mapping>

答案 1 :(得分:1)

您应该使用产品的技术ID,而不是产品名称作为标识符。可能有两个名称相同的产品或产品会更改其名称。但是如果你想要,你可以省略生成器,并在保存之前始终将名称设置为唯一值。

添加一个数字列,一个Long成员变量以及适用于您的技术ID的映射,一切都应该有效。

如果需要,您仍然可以使用索引进行更快的提取或使用唯一约束。

作为替代方案,如果有自动生成唯一产品名称的方法,您可以编写自己的生成器。请参阅this answer

答案 2 :(得分:1)

如果要将字符串用作主键,请使用:

<generator class="assigned"/>

查看https://www.javatpoint.com/generator-classes