我们使用spring,hibernate和hsql来持久化一个简单的用户实体。我们总是得到错误“table not found”。你知道这可能是什么吗?看起来好像没有生成表,或者hsql数据库根本没有运行。
此致 -G。
@Entity
@Table (name="USER")
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="ID")
private Long id;
@Column(name="NAME", length = 100, nullable = false)
private String name;
public User(){}
//getters and setters ...
由这个dao类:
package de.hsrm.mediathek;
import java.util.List;
public class UserDao implements IUserDao{
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(final HibernateTemplate hibernateTemplate){
this.hibernateTemplate = hibernateTemplate;
}
@Transactional
public void store(final User user){
hibernateTemplate.saveOrUpdate(user);
}
@Transactional
public void delete(final Long userId){
final User user = (User) hibernateTemplate.get(User.class, userId);
hibernateTemplate.delete(user);
}
@Transactional(readOnly = true)
public User findById(final Long userId){
return (User) hibernateTemplate.get(User.class, userId);
}
@Transactional(readOnly = true)
public List<User> findAll(){
return hibernateTemplate.find("from User");
}
}
配置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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<tx:annotation-driven/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:mediathekdb" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>de.hsrm.mediathek.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2dll.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop
key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userDao" class="de.hsrm.mediathek.UserDao">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Interceptor for hibernate calls to be able to create and close sessions
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
-->
</beans>
错误日志:
Caused by: java.sql.SQLException: Table not found in statement [insert into User (ID, NAME) values (null, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:528)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:95)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:30)
... 63 more
答案 0 :(得分:8)
问题是属性“hbm2dll”拼写错误。它应该是“hbm2ddl”:
是:
<prop key="hibernate.hbm2dll.auto">create-drop</prop>
应该是:
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
答案 1 :(得分:3)
最好还是留意任何其他错误的日志,因为我遇到了类似的问题,并发现创建表语句失败了,因为Hibernate生成的HSQLDB的DDL和Hibernate正在默默吞噬它
2011-06-09 10:48:30,722 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Wrong data type: ALERT_ID in statement [create table ACCOUNT_ALERT (ALERT_ID numeric generated by default as identity (start with 1)]
后来我更改了以下内容
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ALERT_ID")
private BigInteger alertId;
到
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name="ALERT_ID")
private BigInteger alertId;
它完美无缺。
答案 2 :(得分:0)
我建议尝试使用以下命令在服务器模式下运行HSQLDB:
java -classpath hsqldb.jar org.hsqldb.server.Server
使用数据库管理器检查它是否正在运行:
java -classpath hsqldb.jar org.hsqldb.util.DatabaseManagerSwing
接下来,更改您的Hibernate URL,因此它使用服务器模式HSQLDB:
<property name="url" value="jdbc:hsqldb:mediathekdb" />
我使用这种方法取得了成功。使用仅内存实例时,create-drop可能无法正常工作。