org.hibernate.engine.jdbc.spi.SqlExceptionHelper Table" Club"找不到

时间:2016-06-05 18:38:14

标签: java hibernate jdbc

我是Java EE的新手,当我尝试通过WEB-APP从我的HSQL DB获取数据时,我在Wildfly控制台中收到此错误。

20:11:53,780 INFO  [stdout] (default task-12) Hibernate: select club0_.id as id1_2_, club0_.league_id as league_i4_2_, club0_.name as name2_2_, club0_.nbTitles as nbTitles3_2_ from "Club" club0_
20:11:53,785 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-12) SQL Error: 42102, SQLState: 42S02
20:11:53,785 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-12) Table "Club" not found; SQL statement:
select club0_.id as id1_2_, club0_.league_id as league_i4_2_, club0_.name as name2_2_, club0_.nbTitles as nbTitles3_2_ from "Club" club0_ [42102-173]
20:11:53,787 ERROR [org.jboss.as.ejb3.invocation] (default task-12) JBAS014134: EJB Invocation failed on component FootballBean for method public abstract java.util.List ch.hevs.footballservice.Football.getClubs(): javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
!!!!!! DELETED LINES !!!!!!
!!!!!! DELETED LINES !!!!!!
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
!!!!!! DELETED LINES !!!!!!
!!!!!! DELETED LINES !!!!!!
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
!!!!!! DELETED LINES !!!!!!
!!!!!! DELETED LINES !!!!!!
Caused by: org.h2.jdbc.JdbcSQLException: Table "Club" not found; SQL statement:
select club0_.id as id1_2_, club0_.league_id as league_i4_2_, club0_.name as name2_2_, club0_.nbTitles as nbTitles3_2_ from "Club" club0_ [42102-173]
!!!!!! DELETED LINES !!!!!!
!!!!!! DELETED LINES !!!!!!

这是我的俱乐部课程:

import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="`Club`")
public class Club {

    // attributs
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String name;
    private int nbTitles;

    // relations
    @OneToMany(mappedBy = "club")
    private List<Player> players;

    @ManyToOne
    private League league;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getNbTitles() {
        return nbTitles;
    }

    public void setNbTitles(int nbTitles) {
        this.nbTitles = nbTitles;
    }

    public List<Player> getPlayers() {
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    public League getLeague() {
        return league;
    }

    public void setLeague(League league) {
        this.league = league;
    }

    // constructors
    public Club() {
    }
}

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="footballPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>ch.hevs.businessobject.Club</class>
        <class>ch.hevs.businessobject.League</class>
        <class>ch.hevs.businessobject.Player</class>
        <properties>
            <property name="hibernate.connection.url"
                value="jdbc:hsqldb:hsql://localhost/DB" />
            <property name="hibernate.connection.driver_class"
                value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

我使用以下JUnit测试创建我的数据库:

import junit.framework.TestCase;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

public class CreateSchemaTest extends TestCase {

        @Test
        public void test() {
            Configuration cfg = new Configuration();
            cfg.addAnnotatedClass(ch.hevs.businessobject.Player.class);
            cfg.addAnnotatedClass(ch.hevs.businessobject.Club.class);
            cfg.addAnnotatedClass(ch.hevs.businessobject.League.class);

            cfg.setProperty("hibernate.dialect",
                    "org.hibernate.dialect.HSQLDialect");
            cfg.setProperty("hibernate.connection.driver_class",
                    "org.hsqldb.jdbcDriver");
            cfg.setProperty("hibernate.connection.driver_class",
                    "org.hsqldb.jdbcDriver");
            cfg.setProperty("hibernate.connection.url",
                    "jdbc:hsqldb:hsql://localhost/DB");
            cfg.setProperty("hibernate.connection.username", "sa");

            new SchemaExport(cfg).setOutputFile("schema.ddl").create(false, true);
        }
    }

当我在HSQL DATABASE Manager中执行EntityManager生成的以下查询时,它工作正常..

select club0_.id as id1_2_, club0_.league_id as league_i4_2_, club0_.name as name2_2_, club0_.nbTitles as nbTitles3_2_ from "Club" club0_

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

在您的俱乐部课程中,您有:

@Table(name="`Club`")

但是在您的测试查询中:

... from "Club" club0_ ...

尝试删除该引号。 我希望它会有所帮助。

答案 1 :(得分:1)

感谢您的回答。 我在persistence.xml中添加了以下行,问题解决了。

protected override string ResolveConnectionString(out string connectionStringContext)
{
    var connectionString = base.ResolveConnectionString(out connectionStringContext);

    if (string.IsNullOrEmpty(connectionString) || ReconnectOnError == false)
    {
        return connectionString;
    }

    var builder = new SqlConnectionStringBuilder(connectionString)
    {
        ConnectTimeout = 1; // Timeout value
    };

    return builder.ConnectionString;
}