异常Hibernate插入语句

时间:2013-12-12 17:30:30

标签: java hibernate jsp spring-mvc

我正在开展一个小项目。这是整个项目的代码。代码中发生的事情是,当下拉框加载实体类中ownerName的值时,从选择任何成员时的下拉列表中,应该通过jsp页面上的输入框分配信用限额。因此,不会对ownerName进行任何输入,但只会根据下拉框中选定的ownerName设置信用额度。

这是实体文件:

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

}

这是DAO实现文件:

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

}

服务实施文件:

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

}

这是控制器文件:

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

这是JSP页面:

<%@ 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>

这是编译后的错误日志:

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.

2 个答案:

答案 0 :(得分:2)

将@Column注释中的nullable属性设置为true,

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

并使用非原始包装器类型Float而不是float(这是一种基本类型)。

答案 1 :(得分:0)

堆栈跟踪非常清楚:您没有为creditLimit类的CyclosUsers属性赋值,这是一个原语,因此它不能是null

另外,我认为你的查询没有正确的语法;而不是:

INSERT INTO credit_limit WHERE

尝试类似:

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

(具体语法可能因您使用的数据库引擎而异)。

如果您发布CyclosUsers课程的代码,我可以提供更多帮助。