在Hibernate中,有可能import.sql在类路径的根目录中添加文件,并且在创建Hibernate会话时,将在数据库上执行此文件中的SQL表达式.
但是,如果某个表达式被分成2行或更多行,则Hibernate会抛出异常.如何将SQL表达式分解为更多行?
我是嵌入式数据库的新手,但我至少运行它.令我困惑的是我的数据没有在运行之间保存.我的意思是测试不是很好吗?每次运行应用程序时,我都不想将数据添加到我的数据库中
所以我搜索了一种方法来做到这一点,我发现我将配置一个hibernate连接URL,我尝试这样做
props.put("hibernate.connection.url", "jdbc:h2:~/test");
Run Code Online (Sandbox Code Playgroud)
在我的HibernateConfiguration.java中.虽然没有成功,但没有错误但也没有保存,我没有找到应该从该URL创建的测试文件.(运行Windows并检查我的用户文件夹)
我也看到它可以像这样做
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:db-schema.sql"/>
<jdbc:script location="classpath:db-test-data.sql"/>
</jdbc:embedded-database>
Run Code Online (Sandbox Code Playgroud)
并且每次运行应用程序时都执行脚本,但问题是我希望hibernate处理所有表的创建等.
这通常是怎么做的?
我现在搜索了几个小时,但还没有得到它.
PS.如果需要我会发布我的所有配置.
编辑: 更新了我的问题以包含对一个问题的关注并包括我的配置.
HibernateConfiguration.java包com.courseinfo.project;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import com.courseinfo.project.model.Course;
@Configuration
public class HibernateConfiguration {
@Value("#{dataSource}")
private DataSource dataSource;
@Bean
public AnnotationSessionFactoryBean sessionFactoryBean() {
Properties props = new Properties();
props.put("hibernate.dialect", H2Dialect.class.getName());
props.put("hibernate.format_sql", "true");
props.put("hibernate.connection.url", "jdbc:h2:~/test");
AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
bean.setAnnotatedClasses(new Class[]{Course.class});
bean.setHibernateProperties(props);
bean.setDataSource(this.dataSource);
bean.setSchemaUpdate(true);
return bean;
}
@Bean
public HibernateTransactionManager …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Spring Boot 和 MySQL 开发应用程序。正如文档所说,首先我使用Intelij Idea使用Spring initializr创建了项目,配置了application.properties文件,并编写了schema-mysql.sql文件和data-mysql.sql文件。跑完项目,发现MySQL数据库中没有表,也没有数据。我的配置有什么问题?请帮忙。
application.properties 文件,
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql
Run Code Online (Sandbox Code Playgroud)
pom.xml 文件中的依赖项,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
schema-mysql.sql 文件,
CREATE TABLE IF NOT EXISTS `SOL16_USERS` (
`USERNAME` VARCHAR(200) NOT NULL,
'PASSWORD' VARCHAR(200) NOT NULL,
PRIMARY KEY (`USERNAME`) …Run Code Online (Sandbox Code Playgroud) 我正在研究Spring MVC,
在项目启动时,我已设置database为sql
使用hibernate配置导入默认值hibernate.hbm2ddl.import_files.里面的数据import.sql用UTF-8.编码.
控制台输出
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000388:
Unsuccessful: INSERT INTO menu (id, DATE_CREATED, DATE_DELETED,
DATE_UPDATED, TITLE_ENG, TITLE_GEO, TITLE_RUS, ENABLED, PARENT_ID,
URL, SITE_ID, USER_ID) VALUES
ERROR:
org.hibernate.tool.hbm2ddl.SchemaExport - You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '' at line 1
ERROR:
org.hibernate.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: (1,
'2015-09-10 12:00:00', NULL, NULL, 'About Us', N'ჩვენს …Run Code Online (Sandbox Code Playgroud)