Spring JPA自动创建表失败

2024-02-01

我对 Spring JPA、Hibernate、MySQL 有疑问。 我有一个实体(Nom.java)和存储库(公共接口 NomRepository 扩展 JpaRepository)。它们的创建和注入都很好。

问题是,当我尝试通过存储库的保存方法保存记录时,spring 抱怨“表''不存在”。 事实上我在 MySQL 中没有看到这个表。您尝试了 hibernate.hbm2ddl.auto 的不同值,但没有帮助。

顺便说一句,我使用无 XML 配置。

这是配置文件:

    package ru.interosite.awp.config;
    
    import java.util.Properties;
    import javax.sql.DataSource;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.orm.jpa.JpaVendorAdapter;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.orm.jpa.vendor.Database;
    import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    
    @Configuration
    @ComponentScan("ru.interosite.awp")
    @EnableAutoConfiguration
    public class AppConfiguration {
    
        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/awp");
            dataSource.setUsername("root");
            dataSource.setPassword("password");
            return dataSource;
        }
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
            LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
            lef.setPersistenceUnitName("my_pu");
            lef.setPackagesToScan("ru.interosite.awp.data");
            lef.setDataSource(dataSource);
            lef.setJpaVendorAdapter(jpaVendorAdapter);
            lef.setJpaProperties(getJpaProperties());
            return lef;
        }
    
        @Bean
        public JpaVendorAdapter jpaVendorAdapter() {
            HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    
            jpaVendorAdapter.setDatabase(Database.MYSQL);
            jpaVendorAdapter.setGenerateDdl(true);
            jpaVendorAdapter.setShowSql(true);
            jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
    
            return jpaVendorAdapter;
        }
    
        private Properties getJpaProperties() {
            return new Properties() {
                {
                    setProperty("hibernate.hbm2ddl.auto", "update");
                    setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
                    setProperty("hibernate.show_sql", "true");
                    setProperty("hibernate.format_sql", "true");
                }
            };
        }
    }

以下是我启动应用程序的方法:

    package ru.interosite.awp;
    
    import java.awt.Font;
    import javax.swing.UIManager;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.context.ApplicationContext;
    import ru.interosite.awp.config.AppConfiguration;
    import ru.interosite.awp.gui.UIUtils;
    
    public class Boot {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(Boot.class);
        
        public static void main( String[] args )
        {
            
            UIUtils.setUIFont(new javax.swing.plaf.FontUIResource(Font.SANS_SERIF, Font.PLAIN, 16));
            
            try {
                String lafClassName = UIManager.getSystemLookAndFeelClassName();
                UIManager.setLookAndFeel(lafClassName);
            } catch (Exception e) {
                LOGGER.debug(e.getMessage());
            }        
            
            ApplicationContext ctx = SpringApplication.run(AppConfiguration.class, args);
            ((Runner)ctx.getBean("runner")).start();
        }    
    }

这是我的 pom.xml:

 
<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0" 
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ru.interosite</groupId>
    <artifactId>AWP</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>AWP</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>ru.interosite.awp.Runner</start-class>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.M4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

您需要更改两个方法并删除 getProperties() 方法:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(dataSource);
    lef.setJpaVendorAdapter(jpaVendorAdapter);
    lef.setPackagesToScan("com.spring.domain");
    return lef;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(true);
    hibernateJpaVendorAdapter.setGenerateDdl(true); //Auto creating scheme when true
    hibernateJpaVendorAdapter.setDatabase(Database.H2);//Database type
    return hibernateJpaVendorAdapter;
}

重点是:

hibernateJpaVendorAdapter.setGenerateDdl(true); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring JPA自动创建表失败 的相关文章

随机推荐

  • 在 EV3 和 PC 之间发送数据

    我正在对机器人进行编程 它需要在 EV3 和我的笔记本电脑 Windows 之间发送数据 我在 EV3 上运行 pybricks 这使我能够使用 python 进行编码 我已经做了研究 但唯一剩下的就是 2014 年的一些博客 它们也没有帮
  • 这是C# Monad,问题出在哪里?

    读一上一个问题 https stackoverflow com questions 35951818 why can the monad interface not be declared in java 35959910 35959910
  • 使用 VBS 更改 Active Directory 中的密码到期日期

    我正在尝试使用 VBScript 更改 Active Directory 中用户的密码到期日期 我有获取有关用户密码的信息的代码 但我找不到有关如何更改密码的任何信息 任何帮助将不胜感激 这是我的代码 Const SEC IN DAY 86
  • 为什么编译器抱怨对齐?

    我想了解更多关于对齐的信息 为什么 Microsoft 编译器 Visual Studio 2012 Express 会抱怨以下代码片段的对齐情况 declspec align 16 class Foo public virtual Foo
  • seq 和 == 运算符的神秘行为。精度问题?

    我遇到了该函数的某种奇怪 或只是出乎意料 的行为seq 创建简单序列时 某些值无法与 运算符正确匹配 看这个最小的例子 my seq lt seq 0 0 4 len 5 table my seq ok returns 0 0 1 0 2
  • XSLT 聚合 XML 标记值并存储新标记

    我是 xslt 的新手 并尝试使用 XSLT 处理以下 XML
  • 奥尔良任务的交错

    有一个问题here https stackoverflow com questions 54456369 orleans single threaded nature not respected by continuewith其中 Orle
  • 如何使用 knit::spin 注释掉行

    使用时spin包装内knitr 如何简单地注释掉一些东西 使它们看不见旋转 roxygen风格线 被视为出现在报告中的行 通常的 R 注释 被视为R注释并出现在代码块中 仅是文本且开头没有特殊字符的行会导致错误 以 开头的行LaTeX评论
  • PHP 类自动加载

    我有一个 简单框架 其主要实例是 app 现在 实现自动加载器的最佳方法是什么 不使用 Composer 我需要的是有一个处理所有自动加载的类 支持各种名称空间 我有一些方法 困境 起初我认为我应该创建一个处理所有事情的 静态 类 但后来我
  • Boost random::discrete_distribution 构建后如何更改权重?

    好的 可以在 boost random discrete distribution 中给出权重 概率 e g 双倍概率 0 5 0 1 0 1 0 1 0 1 0 1 boost random discrete distributiondi
  • 使用 PowerShell 重命名文件以增加文件编号?

    我有一堆名为 attachment 023940 attachment 024039 attachment 024041 attachment 024103 etc 我需要通过将文件号增加给定的数字来重命名文件 以便它们与数据库中的正确 I
  • gperftools CPU profiler 究竟是如何启动的?

    gperftools 文档 http google perftools googlecode com svn trunk doc cpuprofile html说libprofiler应该链接到目标程序 gcc myprogram c lp
  • 无法从具有多个返回的 Select 中的使用情况推断出 C# 类型参数

    我不认为我做了任何太深奥的事情 但我没有看到任何其他与此相关的问题 以下代码 我已将其简化为要点 在 C 4 中生成编译器错误 但是 类型参数是什么应该是显而易见的 有一个最大公分母 类 A 也明确定义在方法 Frob 的返回类型 难道编译
  • NodeJS base64 图像编码/解码不太工作

    我一直在尝试将发布到nodeJS 和express框架 的图像保存到数据库 但遇到了一些麻烦 忽略所有的 Web 处理 我认为我已经将问题范围缩小到了 Node js 中进行 Base64 编码的方式 我相信下面过于简化的示例应该可以工作
  • 如何将 tibble 导出为 .csv

    我使用 rfm 包进行了 rfm 分析 结果在 tibble 中 我似乎不知道如何将其导出到 csv 我尝试了下面的参数 但它导出了一个空白文件 gt dim bmdata4RFM 1 1182580 3 gt str bmdata4RFM
  • C 中的 #line 关键字

    我试图理解一些代码 但遇到了一个我以前从未见过的关键字 我尝试用谷歌搜索它 但也没有找到任何有关它的信息 char valtext line 1 Values l define INITIAL 0 line 2 Values l int r
  • 如何使用注释在 Spring MVC 中创建默认方法?

    我找不到解决办法 这让我发疯 我映射了 Controller 它使用 RequestMapping 响应多种方法 当没有指定更具体的情况时 我想将其中一种方法标记为默认方法 例如 Controller RequestMapping user
  • 使用 Click 库的 Python 3.6 中没有模块错误

    我正在尝试使用该包在 python 中构建 CLIclick 我使用的Python版本是3 6 这是我的应用程序的主要部分 import os import click cmd folder os path join os path dir
  • php if() 中的多个 OR 似乎没有正确响应。测试数组值和所有。我究竟做错了什么?

    我正在运行一个非常简单的 if 语句 在我添加两个额外的 之前它工作得很好 或 运算符 这是我的代码 if planDetails Company name company1 planDetails PlanDetail name pd n
  • Spring JPA自动创建表失败

    我对 Spring JPA Hibernate MySQL 有疑问 我有一个实体 Nom java 和存储库 公共接口 NomRepository 扩展 JpaRepository 它们的创建和注入都很好 问题是 当我尝试通过存储库的保存方