hibernate映射创建表

时间:2012-05-15 12:18:47

标签: java mysql hibernate

我写了一个映射如下:

<hibernate-mapping auto-import="true" default-lazy="false">
    <class name="com.saman.entity.hibernate.EmployeeEntity"
           table="Employee" optimistic-lock="version">

        <id name="id" type="java.lang.Integer" >
            <column name="Id" />
            <generator class="identity"/>
        </id>
        <timestamp name="version" source="db"/>
        <property  name="firstName">
            <column name="FirstName" sql-type="nvarchar(300)"/>
        </property>
        <property name="lastName">
            <column name="LastName" sql-type="nvarchar(300)"/>
        </property>
        <property name="employeeType">
            <column name="EmployeeType" sql-type="nvarchar(300)"/>
        </property>
        <!--
        <set name="shifts" table="Shifts" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="Id" not-null="true"/>
            </key>
            <one-to-many class="com.saman.entity.hibernate.ShiftEntity"/>
        </set>
        -->
        <properties name="u1" unique="true">
            <property name="firstName"/>
            <property name="lastName"/>
            <property name="employeeType"/>
        </properties>

    </class>
</hibernate-mapping>

当我运行我的应用程序时,会出现如下错误:

2012-05-15 17:12:38,651 - 警告 - 不成功的架构语句:create table Employee(Id integer not null auto_increment,version datetime not null,FirstName nvarchar(300),LastName nvarchar(300),EmployeeType nvarchar(300),主键(Id),唯一(firstName,lastName,employeeType)) com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:指定的键太长;最大密钥长度为1000字节     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)     在com.mysql.jdbc.Util.handleNewInstance(Util.java:411)     在com.mysql.jdbc.Util.getInstance(Util.java:386)     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)     在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)     在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)     在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)     在com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1749)     在com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1666)     在com.mchange.v2.c3p0.impl.NewProxyStatement.executeUpdate(NewProxyStatement.java:64)     在org.springframework.orm.hibernate3.LocalSessionFactoryBean.executeSchemaStatement(LocalSessionFactoryBean.java:1115)     在org.springframework.orm.hibernate3.LocalSessionFactoryBean.executeSchemaScript(LocalSessionFactoryBean.java:1087)     在org.springframework.orm.hibernate3.LocalSessionFactoryBean $ 1.doInHibernate(LocalSessionFactoryBean.java:942)     在org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)     在org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)     在org.springframework.orm.hibernate3.LocalSessionFactoryBean.updateDatabaseSchema(LocalSessionFactoryBean.java:935)     在org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterSessionFactoryCreation(LocalSessionFactoryBean.java:883)     在org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:213)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)     at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:291)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)     在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)     在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)     在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)     在Main.main(Main.java:18)

它有什么问题?

我使用mysql并在MAMP上运行它。

2 个答案:

答案 0 :(得分:2)

MyISAM引擎存在已知问题。您可以尝试使用此link将引擎更改为InnoDB:

答案 1 :(得分:2)

  

指定的密钥太长;最大密钥长度为1000字节

此错误表示您的唯一索引(firstName,lastName,employeeType)超过1000个字节,这是最大值。来自mysql文档

"UTF-8 encoding of the Unicode character set using one to three bytes per character"

因此,您可以看到您有三个300长度的字段,因此您的索引大小可以是300 * 3 * 3 = 2700&gt; 1000