Here is my 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>
The table consist of two columns: name VARCHAR(20), cost Integer
.
Controller:
@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 method:
public void addProduct(Product product) throws SQLException {
Session session = Hibernate.util.HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(product);
session.getTransaction().commit();
session.close();
}
When I'm trying to run this I have exception:
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)
How to fix that?
you have added the id as <generator class="increment"/>
then it must be integer
use integer instead of string at <id column="name" name="name" type="integer">
,change the hibernate mapping file as shown below
<?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>
You should use a technical id for your product and not the name of the product as the identifier. There might be two products with the same name or products that change their name. But if you want you could just leave out the generator and always set the name to a unique value before saving.
Add a number column, a Long member variable and the appropriate mapping for your technical Id and everything should work.
You still can use a index for faster fetching or a unique constraint if you want.
As an alternative you could write your own generator if there is a automatic way to generate unique product names. See this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With