ken4ward
ken4ward

Reputation: 2296

Exception Hibernate insert statement

I am working on a small project. This is the code for the whole project. What happens within the code is that when the dropdown box is loaded the values from the ownerName in the entity class, from the dropdown when any of the member is selected then a the credit limit should be assigned by the input box on the jsp page. So no input is made for the ownerName but only the credit limit is set based on the selected ownerName from the dropdown box.

This is the entity file:

@Entity
@Table(name = "accounts")
public class CyclosUsers {

    @Id
    @Column(name = "id")
    @GeneratedValue
    private int id;


    @Column(name = "owner_name")
    private String ownerName;

    @Column(name = "credit_limit")
    private float creditLimit;

    public CyclosUsers(){}

    public CyclosUsers(String ownerName, float creditLimit) 
    {
        super();
        //this.id = id;
        this.ownerName = ownerName;
        this.creditLimit = creditLimit;
    }

    public CyclosUsers(int id, String ownerName, float creditLimit) {
        super();
        this.id = id;
        this.ownerName = ownerName;
        this.creditLimit = creditLimit;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOwnerName() {
        return ownerName;
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public float getCreditLimit() {
        return creditLimit;
    }

    public void setCreditLimit(float creditLimit) {
        this.creditLimit = creditLimit;
    }

}

This is the DAO implemetation file:

@Repository
public class CyclosUsersDaoImpl implements CyclosUsersDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void saveCyclosUsers(CyclosUsers cyclosUsers) {
        sessionFactory.getCurrentSession().createSQLQuery("INSERT INTO credit_limit WHERE" +" " +"WEB-INF.views.Register.ownerName.selectedItem("  +"ownerName");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CyclosUsers> addToDropDown() {
        return sessionFactory.getCurrentSession().createSQLQuery("SELECT owner_name FROM accounts").list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CyclosUsers> CyclosUsersAndAccountDetails() {
        return sessionFactory.getCurrentSession().createCriteria(CyclosUsers.class).list();
    }

}

The service implementation file:

@Service
public class CyclosUsersServiceImpl implements CyclosUsersService {

    @Autowired
    private CyclosUsersDao cyclosUsersDao;

    @Override
    @Transactional
    public void saveCyclosUsers(CyclosUsers cyclosUsers) {
        cyclosUsersDao.saveCyclosUsers(cyclosUsers);
    }

    @Override
    @Transactional
    public List<CyclosUsers> addToDropDown() {
        return cyclosUsersDao.addToDropDown();
    }

    @Override
    @Transactional
    public List<CyclosUsers> CyclosUsersAndAccountDetails() {
        return cyclosUsersDao.CyclosUsersAndAccountDetails();
    }

}

This is the controller file:

@Controller
public class CyclosUsersController {

    @Autowired
    private CyclosUsersService cyclosUsersService;

    @RequestMapping("/register")
    public ModelAndView displayOverdraftForm(@ModelAttribute("cyclosUsers") CyclosUsers cyclosUsers, BindingResult bindingResult)
    {
        Object addMembersListToDropdown = null;
        ArrayList<Object> membersListing = new ArrayList<>();
        membersListing.add(cyclosUsersService.addToDropDown());

        for(Object listingMembers : membersListing)
        {
            addMembersListToDropdown = listingMembers;
        }
        System.out.println(addMembersListToDropdown);
        return new ModelAndView("Register", "addMembersListToDropdown", addMembersListToDropdown);  
    }

    @RequestMapping("/saveCyclosUsers")
    public ModelAndView saveCyclosUsersCredentials(@ModelAttribute("cyclosUsers") CyclosUsers cyclosUsers, BindingResult bindingResult)
    {
        cyclosUsersService.saveCyclosUsers(cyclosUsers);
        System.out.println("Cyclos Users List:");
        return new ModelAndView("redirect:cyclosUsersList.html");   
    }

    @RequestMapping("/cyclosUsersList")
    public ModelAndView listCyclosUsersOverdraftDetails()
    {
        @SuppressWarnings("unused")
        CyclosUsers cyclosUsers = new CyclosUsers();
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("cyclosUsers", cyclosUsersService.CyclosUsersAndAccountDetails());
        return new ModelAndView("cyclosUsersDetails", model);
    }   
}

This is the JSP page:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file = "includeFile.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cyclos Users | Overdraft Form</title>
</head>
<body>
    <center>
        <br><br><br><br><br><br>
        <div style = "color:steal; font:30px">Cyclos Users | Overdraft Form</div>
        <c:url var = "overdraftGranting" value = "saveCyclosUsers.html"></c:url>
        <form:form id = "cyclosOverdraftGrantingForm" modelAttribute = "cyclosUsers" method = "post" action = "${overdraftGranting}">
            <table width = "400px" height = "150px">
                <tr>
                    <td><form:label path="ownerName" name = "ownerName">Select Overdraft User:</form:label></td>
                    <td><form:select path="ownerName" name = "ownerName" items = "${addMembersListToDropdown}"></form:select></td>
                </tr>
                <tr>
                    <td><form:label path="creditLimit">Enter an Amount:</form:label></td>
                    <td><form:input path="creditLimit"/></td>
                </tr>
                <tr><td></td><td><input type = "submit" value = "Grant Overdraft"></td></tr>
            </table>
        </form:form>
        <br>
        <a href = "cyclosUsersList.html">Click here to see Overdraft Details</a>
    </center>
</body>
</html>

This is the error log after compilation:

message Request processing failed; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of nigeria.development.foundation.entity.CyclosUsers.creditLimit

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of nigeria.development.foundation.entity.CyclosUsers.creditLimit
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of nigeria.development.foundation.entity.CyclosUsers.creditLimit
    org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:83)
    org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
    org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
    org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3571)
    org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:133)
    org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
    org.hibernate.loader.Loader.doQuery(Loader.java:729)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    org.hibernate.loader.Loader.doList(Loader.java:2213)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    org.hibernate.loader.Loader.list(Loader.java:2099)
    org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
    org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    nigeria.development.foundation.daoImpl.CyclosUsersDaoImpl.CyclosUsersAndAccountDetails(CyclosUsersDaoImpl.java:30)
    nigeria.development.foundation.serviceImpl.CyclosUsersServiceImpl.CyclosUsersAndAccountDetails(CyclosUsersServiceImpl.java:34)
    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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    com.sun.proxy.$Proxy16.CyclosUsersAndAccountDetails(Unknown Source)
    nigeria.development.foundation.controller.CyclosUsersController.listCyclosUsersOverdraftDetails(CyclosUsersController.java:50)
    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.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.IllegalArgumentException: Can not set float field nigeria.development.foundation.entity.CyclosUsers.creditLimit to null value
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    sun.reflect.UnsafeFloatFieldAccessorImpl.set(UnsafeFloatFieldAccessorImpl.java:80)
    java.lang.reflect.Field.set(Field.java:680)
    org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:79)
    org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
    org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
    org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3571)
    org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:133)
    org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
    org.hibernate.loader.Loader.doQuery(Loader.java:729)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    org.hibernate.loader.Loader.doList(Loader.java:2213)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    org.hibernate.loader.Loader.list(Loader.java:2099)
    org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
    org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    nigeria.development.foundation.daoImpl.CyclosUsersDaoImpl.CyclosUsersAndAccountDetails(CyclosUsersDaoImpl.java:30)
    nigeria.development.foundation.serviceImpl.CyclosUsersServiceImpl.CyclosUsersAndAccountDetails(CyclosUsersServiceImpl.java:34)
    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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    com.sun.proxy.$Proxy16.CyclosUsersAndAccountDetails(Unknown Source)
    nigeria.development.foundation.controller.CyclosUsersController.listCyclosUsersOverdraftDetails(CyclosUsersController.java:50)
    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.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.

Upvotes: 2

Views: 1070

Answers (2)

Lucky
Lucky

Reputation: 17345

Set nullable property in your @Column annotation to true,

@Column(name = "credit_limit", nullable=true) //nullable set true
private Float creditLimit;  //Float instead of float

and use the non-primitive wrapper type Float instead of float(which is a primitive type).

Upvotes: 2

user520458
user520458

Reputation:

The stack trace is pretty clear: you're not assigning a value to the creditLimit property of your CyclosUsers class, which is a primitive so it can't be null.

Also, I don't think your query has the correct syntax; instead of:

INSERT INTO credit_limit WHERE

try something like:

INSERT INTO table_name (column_1, column_2) VALUES (value_1, value_2);

(the exact syntax may vary depending on the database engine you're using).

If you post the code of the CyclosUsers class I might be able to help a bit more.

Upvotes: 0

Related Questions