1
votes

I'm trying to save a form that has a foreign key in select tag but it's always null. When I use the inspect element it posts the selected id so I can't say it's problem with form. Can someone please help. Form

   <form:select path="categoryId">
            <form:option value="0" label="   -- Please Select --" />
            <form:options items="${categoryList}"  itemValue="categoryId" itemLabel="categoryName"/>
    </form:select>

Controller

@RequestMapping(value = "product", method = RequestMethod.POST)
public String addNewProduct(@ModelAttribute("product") Product product, Model model){
    model.addAttribute("product", product);
    purchaseOrderService.addProduct(product);
    return "categories";
}

JPA

@Transactional(propagation = Propagation.REQUIRED)
    public boolean saveProduct(Product product) {
        EntityManager em = getEm();
        em.persist(product);
        return true;
    }

Persistence Object

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CATEGORY_ID", referencedColumnName = "CATEGORY_ID")
private Category categoryId;

Error

java.lang.NoClassDefFoundError: org/hibernate/exception/LockTimeoutException
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1328)
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80)
org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy34.addProduct(Unknown Source)
com.logicalideas.purchaseorder.ProductController.addNewProduct(ProductController.java:30)
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:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.ClassNotFoundException: org.hibernate.exception.LockTimeoutException org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556) org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1328) org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300) org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80) org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512) org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394) org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) $Proxy34.addProduct(Unknown Source) com.logicalideas.purchaseorder.ProductController.addNewProduct(ProductController.java:30) 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:601) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Many Thanks

1
@Zed429: as you see in the stacktrace, it's a ClassNotFound exception. Are you sure you have included all the libraries needed?Hoàng Long
Well when I save categories there is no problem so what other libraries could I be missing ? The only difference between Category and Product is Product has a foreign key which is always null for some strange reason.Zed420
Anyone please ??? getting bit desperate nowZed420
Thank you Hoang Long for your input, I had to update these dependencies to get it working. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.7.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.1.7.Final</version> </dependency>Zed420
glad to hear that you fixed the issue. Sorry, I'm quite busy lately so that I couldn't follow it through. For this kind of problem(ClassNotFound), it is important to maintain the version compatibility among the libraries added. I guess that it was your problem.Hoàng Long

1 Answers

0
votes

As you solve the first question (ClassNotFound), I think it's ok by now.

For your second question:

Well this has got rid of the errors but it has a strange behaviour now, it creates a new category even thou I'm in product controller and trying to save a new product with selected category(FK). Then it adds this newly created categoryId which is duplicate of the one you selected( the name, not the id) into product.categoryId. I really hope I'm not confusing anyone. I would really appreciate some please.

This is not the same problem. I think you are misunderstanding the way Hibernate works. When you let the Spring mechanism map the object in this case, it will always create a new categoryId object. On the other hand, by another way you may be able to make Spring understand they are the same, but then you will have troubles when trying to edit the category.

The problem here is that you depends on Spring to map the right Hibernate object - the Spring mapping is not always good. Instead, I think you can send a parameter like "idOfCategory" to the server, get the CategoryId object by that id, then set the category to your newly created Product. A better way is that you only transfer from client a DTO object (ProductDTO), then on server side, create a full-fledged Hibernate Product object.

For example:

@RequestMapping(value = "product", method = RequestMethod.POST)
public String addNewProduct(@ModelAttribute("productDTO") ProductDTO productDTO, Model model){
    model.addAttribute("product", product);
    Product product = new Product(productDTO);
    CategoryId id = productService.getCategoryById(productDTO.getCategoryId());
    product.setCategoryId(id);
    purchaseOrderService.addProduct(product);
    return "categories";
}