当前位置: 首页 > 知识库问答 >
问题:

Spring靴 - Hibernate - 表不存在

艾宏远
2023-03-14

我有一个第三方jar,可以容纳所有实体和映射。我目前正在经典的Spring-MVC应用程序中成功使用此jar,但现在我正在尝试在Spring-Boot应用程序中使用它。(1.5.7.发布)

我有这个用于我的应用程序.java:

@SpringBootApplication
@EntityScan(basePackages = "com.third.party.entity.package")
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
    }
}

由于它是第三方,我也必须进行会话扫描,所以我在我的@Configuration类中有这个:

@Bean
public LocalSessionFactoryBean sessionFactory(EntityManagerFactory emf) throws ClassNotFoundException {
    LocalSessionFactoryBean fact = new LocalSessionFactoryBean();
    fact.setAnnotatedPackages("com.third.party.entity.package");
    fact.setPackagesToScan("com.third.party.entity.package");
    fact.setDataSource(dataSource);
    return fact;
}

在我的application.properties中:

spring.datasource.url=jdbc:mysql://dbserver:3306/mydb?useSSL=false
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

我收到此错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' doesn't exist

现在表名是“User ”,这样就有点意义了。然而,我将这个罐子用于另一个应用程序,所以关于这个主题的其他100个答案都不适用。

一定是配置问题吧?右?

更新我尝试了下面的@sam答案无济于事,但我可以查看这个第三方jar文件的源代码,它看起来像这样:

@Entity
@Table(name = "User")
public class User {
     etc...
}

所以注释中的表名是正确的。我如何让Spring使用它呢?我在寻找一个默认的命名策略和东西...有什么建议吗?

共有3个答案

仲学真
2023-03-14

我有同样的问题,但有不同的解决方案:显然,Hibernate/JPA降低了表名中所有字母的大小写。因此,即使我的表是User并且我有

@Entity
@Table(name = "User")
public class OfficeUser {
...
}

我仍然会得到错误说:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException表doctors_office.user不存在

我的解决方案是将 DB 中的表名称从“用户”更改为“用户”(大写 -

注意:我对我的表使用了不同的实体名(这就是为什么我用< code>OfficeUser作为类名和不同的表名)

白宏放
2023-03-14

Spring靴 - Hibernate - 表不存在

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' **doesn't exist**

我认为你的程序@ entity scan(base packages = " com . third . party . entity . package ")的问题是不正确的,在java中package不能作为包名。

其他

确保您的pojo实体用户类已正确定义

@Entity
@Table(name = "User")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    }

尝试在应用程序中添加以下行代码。属性和运行

应用程序.属性

# Hibernate ddl auto (create, create-drop, update, none): with "update" the database
# schema will be automatically updated accordingly to java entities found in the project
spring.jpa.hibernate.ddl-auto = update

并通过添加下面的注释排除Hibernate AutoConfiguration类,以防它在那里

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})

这个问题类似于Hibernate说表不存在,但它存在

任飞龙
2023-03-14

可能对某人有所帮助的真正答案(对我来说)是根本不使用隐式命名策略。如果您只想使用实体类中注释的内容,请使用这样的物理:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

感谢@rsinha的回答:

Hibernate命名策略更改表名

 类似资料:
  • 当我测试我的项目时,数据库中只创建了4个表,但没有创建其他表,我不知道为什么。创建了表、、和,但没有创建表和我没有放在本例中的其他表。有一些属性我忘记了?感谢您的帮助。以下是一些文件: hibernate.cfg.xml Position.hbm.xml 拒绝位置.hbm.xml 通知.hbm.xml Demande.java Demande.hbm.xml User.java 用户.hbm.xm

  • 问题内容: 在配置hibernate.cfg.xml中,我添加 Hibernate并在运行应用程序时自动创建表。但是,我通过运行drop table sql从数据库中手动删除该表。然后再次运行hibernate应用程序。出现异常 引起原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表’test.person’不存在 解决该问

  • 我使用的是Hibernate 4.3.6,我尝试通过将@Audited注释添加到我的一个@Entity类中来使用Envers功能。(envers jar-hibernate-envers-4.3.6.Final.jar-位于我的CLASSPATH上。) 当我运行没有@Audited注释的代码时,我得到一个org . hibernate . exception . sqlgrammarexcepti

  • 问题内容: 我正在使用Spring + Hibernate+JPA,但遇到无法将实体持久保存到数据库的情况。我已经建立了一个带有@Transactional注释的服务类。它使用包含注入的EntityManager的DAO。当我在服务对象上调用函数时,我看到DAO正在进行大量的选择读取,但是由于我的DAO发出的合并和删除操作,没有更新/删除。当然,我的设置有问题,但是我看不到它。 persisten

  • 我正在使用Spring和Hibernate编写Web应用程序,但第一次遇到这种问题。每当我在服务器上运行我的应用程序时,它都会说**“java.sql.SQLSyntaxErrorException:表'restaurantapp.users'不存在。**我不明白的是,我的数据库中甚至没有一个名为“users”的表,而且我从未在我的应用程序中使用过表“users”。代码部分如下所示。需要帮助来解决

  • 我的服务器运行在localhost:8080(spring boot app)上,前端运行在localhost:3000(angular app)上。问题是我想从前端向后端服务器发出请求。我熟悉cors,但对我来说不起作用。我已将此添加到我的服务器: ,但我仍然得到 XMLHttpRequest无法加载http://localhost:8080/cars.请求被重定向到http://localho