自从过去3周以来,我一直面临着一个问题。这个问题可以在mac上看到。我已经指示Hibernate为我创建表,但是在执行应用程序时会抛出以下异常:
Caused by: ERROR 42X05: Table/View 'STUDENTS' does not exist.
我正在使用Java 6,Spring 3,Hibernate 3& Java DB。我使用Java DB作为内存数据库。以下是代码:
package com.transactions.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import com.transactions.dao.StudentDAO;
import com.transactions.dao.hibernate.StudentHibernateDAO;
import com.transactions.entities.Student;
@Configuration
public class TransactionConfiguration {
@Bean
public DriverManagerDataSource dataSourceBean() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
// dataSource.setUrl("jdbc:hsqldb:mem:testdb;shutdown=false");
dataSource.setUrl("jdbc:derby:memory:student;create=true");
dataSource.setUsername("");
dataSource.setPassword("");
return dataSource;
}
@Bean
public AnnotationSessionFactoryBean sfBean() {
AnnotationSessionFactoryBean sfBean = new AnnotationSessionFactoryBean();
Properties properties = new Properties();
sfBean.setDataSource(dataSourceBean());
properties.setProperty(new String("hibernate.dialect"), new String("org.hibernate.dialect.DerbyDialect"));
properties.setProperty(new String("hibernate.show_sql"), new String("true"));
properties.setProperty(new String("hibernate.hmb2ddl.auto"), new String("create"));
properties.setProperty(new String("hibernate.format_sql"), new String("true"));
sfBean.setAnnotatedClasses(Student.class);
sfBean.setHibernateProperties(properties);
return sfBean;
}
@Bean
public HibernateTemplate hibernateTemplate() {
HibernateTemplate hibernateTemplate = new HibernateTemplate();
hibernateTemplate.setSessionFactory(sfBean().getObject());
return hibernateTemplate;
}
@Bean
public StudentDAO studentDAO() {
StudentDAO studentDAO = new StudentHibernateDAO(hibernateTemplate());
return studentDAO;
}
}
package com.transactions.dao.hibernate;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.transactions.dao.GenericDAO;
public class GenericHibernateDAO<T> implements GenericDAO<T> {
private HibernateTemplate hibernateTemplate;
public GenericHibernateDAO(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public List<T> findAll() {
List<T> t = (List<T>) hibernateTemplate.find("FROM Student");
return t;
}
public T findByName(String name) {
return null;
}
}
package com.transactions.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "students")
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int studentId;
@Column
private String name;
public Student() {
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.transactions</groupId>
<artifactId>transactions</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>transactions</name>
<properties>
<org.springframework.version>3.2.11.RELEASE</org.springframework.version> <!-- 4.1.0.RELEASE -->
<org.slf4j.version>1.7.0</org.slf4j.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-complier-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- <dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency> -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
以下是我的测试类:
package com.transactions.dao.hibernate;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.transactions.config.TransactionConfiguration;
import com.transactions.dao.StudentDAO;
import com.transactions.entities.Student;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {TransactionConfiguration.class})
public class GenericHibernateDAOTest {
@Autowired
private StudentDAO studentDAO;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testFindAll() {
List<Student> students = studentDAO.findAll();
}
}
我还在内存数据库中使用了HSQLDB,但同样引发了类似的异常。我确信Hibernate,HSQLDB&amp; Java DB应该都可以在mac上运行。
我哪里错了?
答案 0 :(得分:0)
经过一段时间后,我意识到属性中存在拼写错误。我添加了以下内容:
properties.setProperty(new String("hibernate.hmb2ddl.auto"), new String("create"));
值应为:
properties.setProperty(new String("hibernate.hbm2ddl.auto"), new String("create"));
这现在有效。