【发布时间】:2015-10-12 09:48:04
【问题描述】:
我正在尝试使用 hibernate.hbm2ddl.import_files 在 webapp 启动时运行 sql 脚本,但这似乎不起作用。我在我的 persistence.properties 中使用以下内容:
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost/rays_rentals?createDatabaseIfNotExist=true
dataSource.username=root
dataSource.password=
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
hibernate.hbm2ddl.import_files=bikes.sql
我的 bikes.sql 文件保存在与我的属性文件相同的位置。 这是我的sql文件:
INSERT INTO `bikes` (`id`, `brand`, `model`) VALUES (1, 'Giant', 'Propel Advanced 0');
这是我的自行车模型:
package com.BrightFuture.RaysRentalSystem.bikes;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Proxy;
@Entity
@Proxy(lazy = false)
@Table(name = "bikes")
public class Bike {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy="bike", cascade=CascadeType.ALL)
private List<BikeRecord> bikeRecords = new ArrayList<BikeRecord>();
@Column(name="brand", nullable=false)
private String brand;
@Column(name="model", nullable=false)
private String model;
}
谢谢。
【问题讨论】:
-
请解释一下您所说的似乎不起作用是什么意思?你真的使用
persistance.properties- 还是persistence.properties? -
我检查了我的数据库,但我尝试插入的值不存在。我使用persistence.properties。对此感到抱歉.. 更新了我的问题
-
@Shaun Lavelle 你检查过你的日志,没有例外。
-
我没有例外。我还需要配置 hibernate.hbm2ddl.import_files 以便我可以使用它吗?
标签: java sql database hibernate import