Mah*_*leh 6 hibernate derby hbm2ddl java-ee javadb
我正在使用hibernate与embeded derby,我希望hibernate创建数据库和表,所以我尝试了以下配置,但我收到错误:
java.sql.SQLException: Schema 'ROOT' does not exist
Run Code Online (Sandbox Code Playgroud)
这是我的配置:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.myapp.domain" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.DerbyDialect
hibernate.hbm2ddl.auto=create
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="url" value="jdbc:derby:test;create=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
Run Code Online (Sandbox Code Playgroud)
更新:这是我第一次使用德比,所以我可能有一些缺失的信息,所以我有一个问题:
我必须在此处配置嵌入式Derby:
http://db.apache.org/derby/papers/DerbyTut/install_software.html
更新2:我删除了classpath上的import.sql脚本文件,该文件负责在数据库中插入演示数据,我发现创建数据库表时出错:
1202 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
1202 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
1359 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table users (user_id bigint generated by default as identity unique, address varchar(255), email varchar(155) not null, mobile varchar(25), name varchar(25) not null, password varchar(255) not null, primary key (user_id))
1359 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Constraints 'SQL111223214919671' and 'SQL111223214919670' have the same set of columns, which is not allowed.
1359 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
1360 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Warning: 10000, SQLState: 01J01
1360 [main] WARN org.hibernate.util.JDBCExceptionReporter - Database 'test' not created, connection made to existing database instead.
Run Code Online (Sandbox Code Playgroud)
从类路径中删除 import.sql 文件并运行应用程序后,我发现错误是创建了数据库,但由于问题 http://issues.apache.org/jira/ ,表不是由 hbm2ddl 创建的browser/DERBY-789 在此声明:使用 apache derby 和 hbm2ddl 的约束问题
主键不能定义为唯一的,因为这是由数据库本身处理的。
因此,当我从主键中删除唯一属性时,错误就消失了。