6

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>ジャー</packaging>

        <name>AWP</name>
        <url>http://maven.apache.org</url>

        <プロパティ>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <start-class>ru.interosite.awp.Runner</start-class>
        </プロパティ>

        <親>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <バージョン>0.5.0.M4</バージョン>
        </親>

        <依存関係>
            <依存関係>
                <groupId>org.springframework</groupId>
                <artifactId>スプリングオーム</artifactId>
            </依存>
            <依存関係>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
            </依存>
            <依存関係>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
            </依存>                    
            <依存関係>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </依存>        
            <依存関係>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </依存>
            <依存関係>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <バージョン>5.1.26</バージョン>
            </依存>

            <依存関係>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <バージョン>1.9.5</バージョン>
            </依存>                                        
        </依存関係>

        <ビルド>
            <プラグイン>
                <プラグイン>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <バージョン>2.3.2</バージョン>
                </プラグイン>
                <プラグイン>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </プラグイン>
            </プラグイン>
        </ビルド>

        <リポジトリ>
            <リポジトリ>
                <id>春のスナップショット</id>
                <name>春のスナップショット</name>
                <url>http://repo.spring.io/libs-snapshot</url>
                <スナップショット>
                    <有効>真</有効>
                </スナップショット>
            </リポジトリ>
            <リポジトリ>
                <id>春のマイルストーン</id>
                <name>春のマイルストーン</name>
                <url>http://repo.spring.io/libs-milestone</url>
                <スナップショット>
                    <有効>false</有効>
                </スナップショット>
            </リポジトリ>
            <リポジトリ>
                <id>org.jboss.repository.releases</id>
                <name>JBoss Maven リリース リポジトリ</name>
                <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
                <スナップショット>
                    <有効>false</有効>
                </スナップショット>
            </リポジトリ>
        </リポジトリ>

        <プラグインリポジトリ>
            <プラグインリポジトリ>
                <id>春のスナップショット</id>
                <name>春のスナップショット</name>
                <url>http://repo.spring.io/libs-snapshot</url>
                <スナップショット>
                    <有効>真</有効>
                </スナップショット>
            </プラグインリポジトリ>
            <プラグインリポジトリ>
                <id>春のマイルストーン</id>
                <name>春のマイルストーン</name>
                <url>http://repo.spring.io/libs-milestone</url>
                <スナップショット>
                    <有効>false</有効>
                </スナップショット>
            </プラグインリポジトリ>
        </pluginRepositories>        

    </プロジェクト>

4

6 に答える 6

14

2 つのメソッドを変更し、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;
}

ポイントは:</p>

hibernateJpaVendorAdapter.setGenerateDdl(true); 
于 2014-05-27T01:21:24.657 に答える
5

最新版【スプリングブート】では、application.propertiesに以下をインクルード

spring.jpa.generate-ddl=true
于 2017-01-24T13:24:15.373 に答える
2

M5 spring-boot-starter-xxx jar に交換したところ、テーブルが作成されていることがわかりました

これが私のアプリケーションクラスです(これは私の春のブートでの最初の試みです...)

import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
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.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.orm.jpa.JpaTransactionManager;
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;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

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

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(H2).build();
    }

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

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(true);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.H2);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager(
            EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}
于 2013-10-15T04:32:04.397 に答える