我正在使用hibernate和 SQLite 数据库。
我只是创建一个具有外键约束的模型,当我运行程序时,在没有外键的情况下在数据库上创建表。
正如在SQLite Website上指定的那样,每次通过运行查询连接到数据库时,我都需要启用外键支持:
PRAGMA foreign_keys = ON;
是否有任何解决方案可以在hibernate.cfg.xml
?
模型类
@Entity
@Table(name = "problem")
public class ProblemInstance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(columnDefinition="integer")
private int idProblem;
private int numberOfPeriods;
private int numberOfProducts;
private int nominalProductRate;
// cost of preventive replacement
private float cpr;
// cost of minimal replacement
private float cmr;
// time elapsed on a preventive replacement
private double tpr;
// time elapsed on a minimal repair
private double tmr;
@OneToMany(mappedBy="problem", cascade = CascadeType.PERSIST)
private List<Product> products;
// weibull parameters k, lambda
private float kParam;
private float lambdaParam;
}
@Entity
@Table(name = "product")
public class Product implements Cloneable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(columnDefinition="integer")
private int idProduct;
private float productionCost;
private float holdingCost;
private float backorderCost;
private float setupCost;
@ManyToOne
private ProblemInstance problem;
}