Spring中的Hibernate数据库生成错误

Posted

技术标签:

【中文标题】Spring中的Hibernate数据库生成错误【英文标题】:Hibernate Database Generation Error in Spring 【发布时间】:2013-05-15 11:39:59 【问题描述】:

我在创建内存数据库时出错。 当我在我的 Tomcat 服务器上启动我的程序时,它似乎运行良好,直到我尝试插入数据库。 我的 Tomcat 日志显示以下内容:

Hibernate: alter table BrokerReferrerJoin drop constraint FK_cfc92321040c4c7e94cd7d7f0f0
Hibernate: alter table BrokerTable drop constraint FK_85c86a6cd9d14a23840e4746545
Hibernate: alter table LeadTable drop constraint FK_26c301acde8d4daa93734bda8ff
Hibernate: alter table LeadTable drop constraint FK_d358f93455874bcba7ed6afd996
Hibernate: alter table ReferrerBrokerJoin drop constraint FK_60dc4e2e6a44406a8fdf4e869e8
Hibernate: alter table ReferrerTable drop constraint FK_910ad4a791e9434da73fbc24ba5
Hibernate: drop table BrokerReferrerJoin if exists
Hibernate: drop table BrokerTable if exists
Hibernate: drop table LeadTable if exists
Hibernate: drop table Person if exists
Hibernate: drop table ReferrerBrokerJoin if exists
Hibernate: drop table ReferrerTable if exists
Hibernate: alter table BrokerReferrerJoin drop constraint FK_7100b66fe58043ed9fcba0c116c
Hibernate: alter table BrokerTable drop constraint FK_b5a6bbb0789c46b39597c4eb788
Hibernate: alter table LeadTable drop constraint FK_e19293e489d2443f80cef09bf10
Hibernate: alter table LeadTable drop constraint FK_0a56acd9932f4f4f930f1b4c23b
Hibernate: alter table ReferrerBrokerJoin drop constraint FK_c700fee7563d461fb33336901e7
Hibernate: alter table ReferrerTable drop constraint FK_c449adf2fb9e4f4d985c944d852
Hibernate: drop table BrokerReferrerJoin if exists
Hibernate: drop table BrokerTable if exists
Hibernate: drop table LeadTable if exists
Hibernate: drop table Person if exists
Hibernate: drop table ReferrerBrokerJoin if exists
Hibernate: drop table ReferrerTable if exists
Hibernate: create table BrokerReferrerJoin (FlexRowIDLink varchar(255) not null, InternalReferrerIDLink varchar(255) not null)
Hibernate: create table BrokerTable (AdminID varchar(255), isAdmin boolean, FlexRowID varchar(255) not null, primary key (FlexRowID))
Hibernate: create table LeadTable (LeadID varchar(255) generated by default as identity (start with 1), Amount double, FlexRowID varchar(255), LeadEmail varchar(50), LeadFName varchar(50) not null, HPhoneNo integer, LeadLName varchar(50) not null, MPhoneNo bigint, Notes varchar(4095), WPhoneNo integer, Status varchar(255), LoanType varchar(255), leadBroker_FlexRowID varchar(255), leadReferrer_FlexRowID varchar(255), primary key (LeadID))
Hibernate: create table Person (FlexRowID varchar(255) not null, CreatedOn date, Email varchar(255), FName varchar(255), LName varchar(255), LastUpdated date, OrgID varchar(255), OrgName varchar(255), Username varchar(255), primary key (FlexRowID))
Hibernate: create table ReferrerBrokerJoin (InternalReferrerIDLink varchar(255) not null, FlexRowIDLink varchar(255) not null)
Hibernate: create table ReferrerTable (LTSRowID varchar(255), FlexRowID varchar(255) not null, primary key (FlexRowID))
Hibernate: alter table BrokerReferrerJoin add constraint UK_bc91f298149c4420bc453811411 unique (InternalReferrerIDLink)
Hibernate: alter table BrokerReferrerJoin add constraint FK_7100b66fe58043ed9fcba0c116c foreign key (FlexRowIDLink) references BrokerTable
Hibernate: alter table BrokerTable add constraint FK_b5a6bbb0789c46b39597c4eb788 foreign key (FlexRowID) references Person
Hibernate: alter table LeadTable add constraint FK_e19293e489d2443f80cef09bf10 foreign key (leadBroker_FlexRowID) references BrokerTable
Hibernate: alter table LeadTable add constraint FK_0a56acd9932f4f4f930f1b4c23b foreign key (leadReferrer_FlexRowID) references ReferrerTable
Hibernate: alter table ReferrerBrokerJoin add constraint UK_836e448745504545aaa39709e22 unique (FlexRowIDLink)
Hibernate: alter table ReferrerBrokerJoin add constraint FK_c700fee7563d461fb33336901e7 foreign key (FlexRowIDLink) references BrokerTable
Hibernate: alter table ReferrerTable add constraint UK_b0c668bb487b4a1d8b5f7b9657b unique (LTSRowID)
Hibernate: alter table ReferrerTable add constraint FK_c449adf2fb9e4f4d985c944d852 foreign key (FlexRowID) references Person
18:52:45.573 [http-bio-8080-exec-3] ERROR com.au.curtin.Lead.HibernateLeadDao - The sessionFactory was set: org.springframework.orm.hibernate4.LocalSessionFactoryBean@6c80d028
18:52:45.592 [http-bio-8080-exec-3] ERROR com.au.curtin.Lead.LeadServiceClass - The HibernateLeadDao was set: com.au.curtin.Lead.HibernateLeadDao@1c90a278
18:52:49.862 [http-bio-8080-exec-7] ERROR com.au.curtin.LoginController - Starting logger
18:52:54.253 [http-bio-8080-exec-9] ERROR com.au.curtin.LeadController - Returning Referrer Home View
18:53:01.035 [http-bio-8080-exec-10] ERROR com.au.curtin.LeadController - org.springframework.validation.BeanPropertyBindingResult: 0 errors
18:53:01.035 [http-bio-8080-exec-10] ERROR com.au.curtin.Lead.LeadServiceClass - Home Phone Number: 94572707Work Phone Number : nullMobile Phone Number: nullEmail :
18:53:01.098 [http-bio-8080-exec-10] ERROR com.au.curtin.Lead.LeadServiceClass - com.au.curtin.Lead.HibernateLeadDao@1c90a278
18:53:01.098 [http-bio-8080-exec-10] ERROR com.au.curtin.Lead.HibernateLeadDao - Lead : Lead [leadID=null, flexRowID=null, leadFirstName=James, leadLastName=Massey, leadHomePhoneNumber=94572707, leadWorkPhoneNumber=null, leadMobilePhoneNumber=null, borrowingAmount=1000.0, leadEmail=, loanType=null, leadNotes=, leadBroker=null, leadReferrer=null, loanStatus=null]Session is: SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])
Hibernate: insert into LeadTable (LeadID, Amount, FlexRowID, leadBroker_FlexRowID, LeadEmail, LeadFName, HPhoneNo, LeadLName, MPhoneNo, Notes, leadReferrer_FlexRowID, WPhoneNo, Status, LoanType) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

我不知道为什么它突然导致这个错误。 所有字段都设置有注释。 这是我的 Spring .xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <tx:annotation-driven transaction-manager="transactionManager" />
    <jdbc:embedded-database id="LTSDatabase" type="HSQL" />
    <context:component-scan base-package="x.y" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="LTSDatabase" />
        <property name="annotatedClasses">
            <list>
                <value>x.y.Lead</value>
                <value>x.y.Broker</value>
                <value>x.y.Referrer</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <bean id="PersistenceAnnotationPostProcessor"
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <bean id="hibernateExceptionTranslator"
        class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
        id="PersistenceExceptionTranslator" depends-on="hibernateExceptionTranslator" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

如您所见,我使用注释来设置数据库和我的事务。 我不知道为什么会发生这个错误。如果有人知道为什么会这样,请告诉我。

如果有帮助,我正在使用 Spring 3、Hibernate 4.2 和 Tomcat 7。

【问题讨论】:

【参考方案1】:

以防万一这对任何人都有帮助,我的问题是我试图将我的字段作为我的主键声明为字符串。 Hibernate 无法处理非数字自动生成的主键。

【讨论】:

以上是关于Spring中的Hibernate数据库生成错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring根据表中的行数生成ID

Spring 4和Hibernate 4 Session错误

spring整合hibernate的事务一直错误

Hibernate/JDBC 为 Informix 数据库生成错误的 SQL

Spring Boot Batch 中的 Hibernate_sequence 错误(预定)

Spring Boot Hibernate 将数据插入 Postgres 中的错误列