将Spring Roo与Postgres一起使用时出现数据插入错误

时间:2013-07-02 10:09:32

标签: java spring hibernate jpa spring-roo

我很擅长使用Roo,Hibernate和Postgres。到目前为止,我一直在研究Java,JDBC和MySQL,但最近我不得不改用以前的平台。

我尝试使用Roo和现有的Postgres数据库创建基于Spring Web MVC的基本CRUD Web应用程序。读取,显示,更新和删除功能都可以正常工作,但不能创建/插入功能。我真的不知道出了什么问题,因为它是一个非常基本的应用程序,其他功能正常。我试过谷歌搜索和搜索,但我找不到任何有助于我的问题。我真的很感激任何人都能给予的帮助。

版本详细信息:

JDK: 1.7u25
Spring: 3.2.3.RELEASE
Database: Postgres 9.2
IDE: Spring Tool Suite 3.2.0.RELEASE
RAD: Spring Roo 1.2.4.RELEASE
JDBC Driver: 9.1-901-1.jdbc4

* 数据库架构: public


- 学生的表结构


DROP TABLE IF EXISTS "public"."student";
CREATE TABLE "public"."student" (
"uid" int2 NOT NULL PRIMARY KEY,
"name" varchar(30) COLLATE "default" NOT NULL,
"math" int2 NOT NULL,
"science" int2 NOT NULL,
"english" int2 NOT NULL,
"total" int2,
"grade" varchar(2) COLLATE "default"
);

- 天气表结构


DROP TABLE IF EXISTS "public"."weather";
CREATE TABLE "public"."weather" (
"city" varchar(30) COLLATE "default",
"temp_hi" int2,
"temp_lo" int2,
"prcp" float4,
"date" date,
"uid" int2 REFERENCES "public"."student" ("uid")
);

的applicationContext.xml

<context:component-scan base-package="com.springtest.sample">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
        <property name="validationQuery" value="SELECT version();"/>
    </bean>
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

WEB-INF \ views \ weathers \ create.jspx(由roo制作,用于创建我认为的域天气记录。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>
    <form:create compositePkField="id" id="fc_com_springtest_sample_domain_Weather" modelAttribute="weather" path="/weathers" render="${empty dependencies}" z="PF86xg+kbnl7RAqTjojWlF4Z3qE=">
        <field:input disableFormBinding="true" field="id.city" id="c_com_springtest_sample_domain_Weather_id_city" required="true" z="ckELowpSAQzvf9cAv6oCYkRbCvk="/>
        <field:input disableFormBinding="true" field="id.tempHi" id="c_com_springtest_sample_domain_Weather_id_tempHi" required="true" validationMessageCode="field_invalid_integer" z="VLv8DnAYnP8Q7dWMOUFpOsX1gN4="/>
        <field:input disableFormBinding="true" field="id.tempLo" id="c_com_springtest_sample_domain_Weather_id_tempLo" required="true" validationMessageCode="field_invalid_integer" z="wxk3KZImWeSNkXxtgFk8xmg7hOQ="/>
        <field:input disableFormBinding="true" field="id.prcp" id="c_com_springtest_sample_domain_Weather_id_prcp" required="true" validationMessageCode="field_invalid_number" z="g0pZudMkjApBEG0kGtrzcEMdIXo="/>
        <field:datetime dateTimePattern="${weather_id.date_date_format}" disableFormBinding="true" field="id.date" id="c_com_springtest_sample_domain_Weather_id_date" required="true" z="mxySE+ylX1MvkpeC32dz6kE1yO4="/>
        <field:input disableFormBinding="true" field="id.uid" id="c_com_springtest_sample_domain_Weather_id_uid" required="true" validationMessageCode="field_invalid_integer" z="B7pFK6f2H5X8jUsDisVjPiKRaqg="/>
    </form:create>
    <form:dependency dependencies="${dependencies}" id="d_com_springtest_sample_domain_Weather" render="${not empty dependencies}" z="5v9Q9zzCQAQ4XyDzykoEl2YVqjI="/>
</div>

WEB-INF \视图\学生\ create.jspx

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>
    <form:create id="fc_com_springtest_sample_domain_Student" modelAttribute="student" path="/students" render="${empty dependencies}" z="4DI952XoJPRi7uvp6wH3vh4jH+c=">
        <field:simple field="weathers" id="c_com_springtest_sample_domain_Student_weathers" messageCode="entity_reference_not_managed" messageCodeAttribute="Weather" z="kF6JmO7MlzSoqhfAd2ZHM5ai8Kc="/>
        <field:input field="name" id="c_com_springtest_sample_domain_Student_name" required="true" z="OSD2mrzOmmRQA6VBIJPVNVnSCWI="/>
        <field:input field="math" id="c_com_springtest_sample_domain_Student_math" required="true" validationMessageCode="field_invalid_integer" z="VIm+H0xvZNmMgWvRhcVKfhL1520="/>
        <field:input field="science" id="c_com_springtest_sample_domain_Student_science" required="true" validationMessageCode="field_invalid_integer" z="Uyq3S34h4XdMpkGZ0/lOm8daWWA="/>
        <field:input field="english" id="c_com_springtest_sample_domain_Student_english" required="true" validationMessageCode="field_invalid_integer" z="3TeOvLZYSl+T2s9ClWQmMc+XJX0="/>
        <field:input field="total" id="c_com_springtest_sample_domain_Student_total" validationMessageCode="field_invalid_integer" z="ksAXIgx4f3bLj1Mn3bmEMqZU8OI="/>
        <field:input field="grade" id="c_com_springtest_sample_domain_Student_grade" z="wuIbwBuzL0bBpARHtTH1bb9L6IA="/>
    </form:create>
    <form:dependency dependencies="${dependencies}" id="d_com_springtest_sample_domain_Student" render="${not empty dependencies}" z="0oOOhh7rARbZ6RteFU2igEZ3BTE="/>
</div>

好吧,我希望我已经粘贴了所有重要的东西。我不确定什么是重要的,什么不重要,所以如果有太多信息,我很抱歉!

错误详细信息:

数据访问失败

异常消息:

org.hibernate.exception.SQLGrammarException: could not extract ResultSet; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

异常堆栈跟踪:

org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:321)
org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:33)
com.springtest.sample.domain.Student_Roo_Jpa_ActiveRecord.persist_aroundBody10(Student_Roo_Jpa_ActiveRecord.aj:43)
com.springtest.sample.domain.Student_Roo_Jpa_ActiveRecord$AjcClosure11.run(Student_Roo_Jpa_ActiveRecord.aj:1)
org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:59)
org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:65)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:63)
com.springtest.sample.domain.Student_Roo_Jpa_ActiveRecord.ajc$interMethod$com_springtest_sample_domain_Student_Roo_Jpa_ActiveRecord$com_springtest_sample_domain_Student$persist(Student_Roo_Jpa_ActiveRecord.aj:41)
com.springtest.sample.domain.Student.persist(Student.java:1)
com.springtest.sample.domain.Student_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_springtest_sample_domain_Student_Roo_Jpa_ActiveRecord$com_springtest_sample_domain_Student$persist(Student_Roo_Jpa_ActiveRecord.aj)
com.springtest.sample.web.StudentController_Roo_Controller.ajc$interMethod$com_springtest_sample_web_StudentController_Roo_Controller$com_springtest_sample_web_StudentController$create(StudentController_Roo_Controller.aj:30)
com.springtest.sample.web.StudentController.create(StudentController.java:1)
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:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:0)

我可以通过此例外和Spring Roo / JPA来解决我的问题......

您需要查看生成的实体。

在我的情况下,我使用逆向工程生成了我的实体,但我的属性的映射是错误的...更新我的java代码(和映射)以反映我的数据库表(pks,属性,fks等)的结构。我只需要更新相关的jsp页面,问题就解决了。

我建议您配置Hibernate以在persistence.xml中使用以下参数显示在日志中执行的查询:

 <property name = "hibernate.show_sql" value = "true" />

使用您执行的查询,您可以在应用程序之外的数据库中尝试它并检查它是否正常。

我希望它会有所帮助。