在语句中找不到 Spring Hibernate JPA HSQL 表

Posted

技术标签:

【中文标题】在语句中找不到 Spring Hibernate JPA HSQL 表【英文标题】:Spring Hibernate JPA HSQL Table not found in statement 【发布时间】:2014-05-09 12:09:15 【问题描述】:

我正在使用 LocalContainerEntityManagerFactoryBean 训练 Spring 持久性并收到错误:

 Caused by: java.sql.SQLException: Table not found in statement [insert into PERSON      (ID, email, name) values (null, ?, ?)]

我真的不知道我做错了什么

我的主课(仅供测试)

    package com.me.test;

    import com.me.model.Person;
    import com.me.service.PersonService;
    import java.util.List;
    import java.util.logging.Logger;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main 

    /**
     *
     */
    public static final Logger log = Logger.getLogger(Main.class.getName());
    public static void main(String[] args) 
        log.info("************** BEGINNING PROGRAM **************");

        ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/spring-config.xml");
        PersonService personService = (PersonService) context.getBean("personService");

        Person person = new Person();
        person.setName("name");
        person.setEmail("name@name.com");
        personService.addPerson(person);
        log.info("Person : " + person + " added successfully");

        List<Person> persons = personService.fetchAllPersons();
        log.info("The list of all persons = " + persons);

        log.info("************** ENDING PROGRAM *****************");
    

我的应用程序上下文:

    <?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 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/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.me" />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="personPersistenceUnit" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="HSQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:file:/home/me/Pulpit/hsql1/db3; shutdown=true" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <bean id="dbUtil" class="com.me.service.DbUtil">
<!--        init-method="initialize">-->
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans> 

persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="1.0">
        <persistence-unit name="personPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
            <class>com.me.model.Person</class>
        </persistence-unit>
    </persistence> 

PersonService 类

package com.me.service;

import com.me.dao.PersonDao;
import com.me.model.Person;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PersonService 

    private PersonDao personDao;

    public PersonDao getPersonDao() 
        return personDao;
    
    @Autowired
    public void setPersonDao(PersonDao personDao) 
        this.personDao = personDao;
    

    public void addPerson(Person person) 
        getPersonDao().insert(person);
    

    public List<Person> fetchAllPersons() 
        return getPersonDao().selectAll();
    

personDAO 类

package com.me.dao;

import com.me.model.Person;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository("personDao")
@Transactional(propagation = Propagation.REQUIRED)
public class PersonDao 

    private static final String SELECT_QUERY = "select p from Person p";

    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() 
        return entityManager;
    

    public void setEntityManager(EntityManager entityManager) 
        this.entityManager = entityManager;
    

    public void insert(Person person) 
        entityManager.persist(person);
    

    public List<Person> selectAll() 
        Query query = entityManager.createQuery(SELECT_QUERY);
        List<Person> persons = (List<Person>) query.getResultList();
        return persons;
    


你们怎么了?

编辑: 添加人员类:

package com.me.model;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "PERSON")
public class Person implements Serializable

    private Integer id;
    private String name;
    private String email;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    

    @Override
    public String toString() 
        return "Person [id=" + id + ", name=" + name + ", email=" + email + "]";
    


和persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="1.0">
    <persistence-unit name="personPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
    <class>com.me.model.Person</class>
</persistence-unit>
</persistence> 

【问题讨论】:

你的 Person 类是否用 @Entity 注释? 见上面的问题版 :) 你能不能也显示persistence.xml 确定 - 问题已更新 在下面查看我的答案 【参考方案1】:

问题是您没有告诉 Hibernate 在加载时需要创建表的任何地方。

一个简单的方法是添加

<properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties>

persistence.xml&lt;class&gt; 之后

【讨论】:

是的,它成功了!谢谢你。为了在 persistence.xml 文件中更具体,我必须创建 元素并将 放入其中。 太棒了!很高兴为您提供帮助!

以上是关于在语句中找不到 Spring Hibernate JPA HSQL 表的主要内容,如果未能解决你的问题,请参考以下文章

我在 JPA Facet 平台中找不到 Hibernate 2.* 选项

在 osgi-container 中找不到 hibernate-validator 的 El 实现

在 Java 构建路径中找不到超类“javax.servlet.http.HttpServlet”

org.hibernate.PropertyNotFoundException:在类Address中找不到customerId的getter

在类路径中找不到休眠验证器

在 SecurityContext 中找不到 Authentication 对象 - Spring 3.2.2