gpt4 book ai didi

java - hibernate 映射创建表

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:55 26 4
gpt4 key购买 nike

我写了一个映射,如下所示:

<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-警告-模式声明未成功:创建表Employee(Id整数不为null auto_increment,版本日期时间不为null,FirstName nvarchar(300),LastName nvarchar(300),EmployeeType nvarchar(300 ),主键(Id),唯一键(firstName,lastName,employeeType))
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:指定的密钥太长;最大密钥长度为1000个字节
    在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
    在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    在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)
    在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)
    在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上运行它。

最佳答案

指定的密钥太长;最大密钥长度为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> 1000

关于java - hibernate 映射创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10600489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com