2013-10-02 14 views
3

我有一個Spring JPA,Hibernate,MySQL的問題。 我有一個實體(Nom.java)和存儲庫(公共接口NomRepository擴展JpaRepository)。他們創建並注入得很好。自動創建表在Spring中失敗JPA

問題是,當我試圖通過存儲庫的save方法保存記錄時,spring會抱怨「Table」不存在「。 的確我在MySQL看不到這張表。我試過了hibernate.hbm2ddl.auto的不同值,但沒有幫助。

我使用XML-less配置BTW。

這裏的配置文件:

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> 

回答

1

好吧,最後我發現瞭如何解決它。 1)首先,我搬到AppConfiguration類頂級包,ru.interosite.awp在我的情況 2)其次,我改變了註解是:

@Configuration 
@ComponentScan 
@EnableJpaRepositories 
public class AppConfiguration {... 

似乎@EnableAutoConfiguration註解把事情搞亂。 我不知道,如果它是一個錯誤或功能。看起來實際上是一個彈簧引導的錯誤。

1

如果你想表到被創建,您必須將hibernate.hbm2ddl.auto屬性設置爲create。以下是hibernate.hbm2ddl.auto的可能值:

  • 驗證:驗證架構,不更改數據庫。
  • 更新:更新架構。
  • create:創建模式,破壞以前的數據。
  • create-drop:在會話結束時刪除架構。

此外,請檢查您的數據庫網址是否正確。

[更新]不要忘記爲spring定義事務管理器。

@Bean 
    public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() 
    { 
    PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor = new PersistenceAnnotationBeanPostProcessor(); 
    return persistenceAnnotationBeanPostProcessor; 
    } 
+0

你說得對。正如你所看到的,我將hibernate.hbm2ddl.auto設置爲在getJpaProperties方法中「更新」。但它沒有幫助。 – Cyrusmith

+0

我也嘗試過「創建」。沒有運氣。 – Cyrusmith

+0

persistenceAnnotationBeanPostProcessor也沒有幫助。 Plz看到我的答案。 – Cyrusmith

2

我交換到M5彈簧引導起動XXX罐子,現在我看到我的表獲得創建

這是我的應用程序類(這是我在春天開機,以便第一次嘗試...)

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); 
    } 
} 
4

需要昌二方法和刪除的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); 
0

在最新版本[春天開機] .INCLUDE的以下在application.properties

spring.jpa.generate-ddl=true