曾问过同一问题多次,但没有一个能解决我的问题。
我创建了一个休眠+ H2 + Sprinvg mvc项目。我正在使用基于Java的配置。我有以下与Datasource,SessionFactory和TransactionManager相关的bean
@Configuration
@ComponentScan(basePackages="org.testpackage")
@EnableWebMvc
@EnableTransactionManagement
public class MyConfiguration extends WebMvcConfigurationSupport {
@Bean(initMethod="start",destroyMethod="stop")
public org.h2.tools.Server h2WebConsonleServer () throws SQLException {
return org.h2.tools.Server.createWebServer("-web","-webAllowOthers","-
webDaemon","-webPort", "8082");
}
@Bean
public DataSource getDataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(false)
.setName("mytestdb")
.setType(EmbeddedDatabaseType.H2)
.addDefaultScripts()
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.build();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(getDataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
sessionFactory.setPackagesToScan(new String[] {"org.testpackage.model"});
return sessionFactory;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
final Properties hibernateProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
hibernateProperties.setProperty("hibernate.show_sql", "true");
return hibernateProperties;
}
//Some more beans
}
我有以下Entity类
@Entity
@Table(name = "MYTESTDB.TEST_TABLE")
public class User{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "email", unique = true)
private String email;
public User(int id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public User() {
}
//Getters and Setters
}
在DataSource bean中,我正在使用addDefaultScripts(),并且我有2个sql脚本,它们在H2中创建Schema并插入一些预定义的值。如下。
//schema.sql Script
CREATE SCHEMA `MYTESTDB` ;
Drop TABLE IF EXISTS MYTESTDBDB.TEST_TABLE;
CREATE TABLE MYTESTDB.TEST_TABLE (
ID INT NOT NULL PRIMARY KEY,
NAME VARCHAR(50) NOT NULL,
EMAIL VARCHAR(20) NOT NULL,
);
CREATE UNIQUE INDEX ON MYTESTDB.TEST_TABLE (EMAIL)
//data.sql Script
INSERT INTO MYTESTDB.TEST_TABLE(id, name, email)
VALUES ('1', 'Tom', 'tom12@hotmail.com');
如果我使用休眠 hbm2ddl.auto 属性值“创建” ,则一切正常,休眠会删除该表并重新创建它。我已经从网络浏览器验证了它。但是,如果我使用“ validate” 属性,则会收到以下错误异常
Error creating bean with name 'sessionFactory' defined in org.testPackage.configuration.MYConfiguration: Invocation of init method failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [MYTESTDB.TEST_TABLE]
任何人都可以帮助我找出问题所在吗?
答案 0 :(得分:1)
它现在正在工作。在@Slaw的帮助下,我可以使用“验证” 属性。
使用@Table(schema = "MYTESTDB", name = "TEST_TABLE")
代替@Table(name = "MYTESTDB.TEST_TABLE")
。但是我必须在用户实体类中进行更多更改。我添加了@GeneratedValue
而不是@GeneratedValue(strategy = GenerationType.IDENTITY)
注释。现在一切正常。
感谢@Slaw和@Mykhailo宝贵的时间。