Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动-休眠-表不存在_Java_Spring_Hibernate_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java Spring启动-休眠-表不存在

Java Spring启动-休眠-表不存在,java,spring,hibernate,spring-boot,spring-data-jpa,Java,Spring,Hibernate,Spring Boot,Spring Data Jpa,我有一个第三方jar,它保存所有实体和映射。我目前正在一个经典的SpringMVC应用程序中成功地使用这个jar,但现在我正在尝试在SpringBoot应用程序中使用它。(1.5.7.发布) 我为application.java准备了这个: @SpringBootApplication @EntityScan(basePackages = "com.third.party.entity.package") public class Application extends SpringBootSe

我有一个第三方jar,它保存所有实体和映射。我目前正在一个经典的SpringMVC应用程序中成功地使用这个jar,但现在我正在尝试在SpringBoot应用程序中使用它。(1.5.7.发布)

我为application.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 answer,但没有成功,但是我可以查看这个第三方jar文件的源代码,它看起来是这样的:

@Entity
@Table(name = "User")
public class User {
     etc...
}
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
因此注释中的表名是正确的。我如何让Spring使用它?我正在寻找一个默认的命名策略和东西。。。有什么建议吗

Spring启动-休眠-表不存在

我认为您的程序@EntityScan(basePackages=“com.third.party.entity.package”)的问题不正确,package在java中不能作为包名接受

其他

确保正确定义了pojo实体用户

@Entity
@Table(name = "User")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    }
尝试在应用程序.properties中添加以下行代码并运行

应用程序属性

# 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自动配置类(如果有)

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})
这个问题类似于

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

@Entity
@Table(name = "User")
public class User {
     etc...
}
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
感谢@rsinha的回答:


我遇到了同样的问题,但解决方案不同:显然,Hibernate/JPA将表名中的所有字母小写。所以即使我的表是
User
,而我

@Entity
@Table(name = "User")
public class OfficeUser {
...
}
我仍然会得到这样的错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'doctors\u office.user'不存在

我的解决方案是将数据库中的表名从
User
更改为
User
(大写->小写)


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

我遇到了类似的问题,原因是我的实体对象有一个名为“group”的类变量,这是一个mysql关键字。因为我没有用@Column注释它以给出不同的名称,所以它被迫使用“group”作为列名。解决方案是直接用@Column注释类变量,给它一个不同的名称

以前

 private String group;
之后


我尝试了上面所有的解决方案,但最终错误来自一个更简单的原因

隐藏在异常Stacktrace中,我发现模型中的一个属性被命名为SQL保留字。这会阻止创建表

重命名属性解决了这个问题


我无法将ddl auto设置为update,因为我无法更改数据库。排除HibernateJPA自动配置只会给我一个类型为“javax.persistence.EntityManagerFactory”的bean not found bean@mmaceachran您是否检查了@EntityScan(basePackages=“com.third.party.entity.package”)是否正确!!是的,那是正确的包裹。例如,我刚刚重命名了它。我编辑了我的问题,所以显示了一点用户类。对我来说也是一样的。我不知道他们为什么使用小写,但是mysql不区分大小写。我遇到了完全相同的问题,我的一列名为KEY,hibernate无法创建表。谢谢你的评论。