gpt4 book ai didi

java - org.hibernate.engine.jdbc.spi.SqlExceptionHelper 表 "Club"未找到

转载 作者:行者123 更新时间:2023-12-02 03:34:10 25 4
gpt4 key购买 nike

我是 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 !!!!!!

这是我的俱乐部类(class):

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 数据库管理器中执行 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_

有人可以帮助我吗?

最佳答案

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

<property name="hibernate.hbm2ddl.auto" value="update" />

关于java - org.hibernate.engine.jdbc.spi.SqlExceptionHelper 表 "Club"未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37645442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com