【发布时间】:2013-10-09 00:40:39
【问题描述】:
我对 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:
4.0.0 ru.interosite AWP 1.0-SNAPSHOT 罐子包装>
AWP http://maven.apache.org UTF-8 UTF-8 ru.interosite.awp.Runner 属性>
org.springframework.boot spring-boot-starter-parent 0.5.0.M4版本>
父>
org.springframework spring-orm 依赖>
org.springframework.data spring-data-jpa 依赖>
org.springframework spring-tx 依赖>
org.springframework.boot spring-boot-starter-data-jpa 依赖>
org.hibernate 休眠实体管理器 依赖>
mysql mysql-connector-java 5.1.26版本>
依赖>
org.mockito mockito-all 1.9.5版本>
依赖>
依赖>
maven-compiler-plugin 2.3.2版本>
插件>
org.springframework.boot spring-boot-maven-plugin 插件>
插件>
弹簧快照 春季快照 http://repo.spring.io/libs-snapshot 真启用>
快照>
春天的里程碑 春季里程碑 http://repo.spring.io/libs-milestone 假启用>
快照>
org.jboss.repository.releases JBoss Maven 发布存储库 https://repository.jboss.org/nexus/content/repositories/releases 假启用>
快照>
存储库>
弹簧快照 春季快照 http://repo.spring.io/libs-snapshot 真启用>
快照>
春天的里程碑 春季里程碑 http://repo.spring.io/libs-milestone 假启用>
快照>
项目>
【问题讨论】:
标签: mysql spring hibernate spring-data-jpa