【问题标题】:hibernate.hbm2ddl.import_files in same directory not working同一目录中的 hibernate.hbm2ddl.import_files 不起作用
【发布时间】: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


【解决方案1】:

我解决了这个问题。我的问题中的一切都是正确的..我只是在我的 JpaConfig 中错过了它的配置

【讨论】:

    【解决方案2】:

    请使用以下属性修改您的属性文件: hibernate.hbm2ddl.auto=update

    【讨论】:

    • 这就是我的想法。仅创建和创建-删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多