Hibernate 在插入之前删除所有行
我有这个实体类,这是我为测试 Hibernate 构建的第一个实体类,它使用 oracle 序列来填充 ID 字段:
@Entity
@Table(name="COMPANIES")
public class Companies {
private Integer cmpid;
private String cmpname;
private String cmpcountry;
public Companies() {
}
public Companies(String name, String country) {
this.cmpname = name;
this.cmpcountry = country;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "gen_CMP")
@SequenceGenerator(name="gen_CMP", sequenceName = "CMP_ID_SEQ")
@Column(name="CMP_ID", nullable=false)
public Integer getCmpId() {
return this.cmpid;
}
public void setCmpId(Integer cmpid) {
this.cmpid = cmpid;
}
@Column(name="CMP_NAME", nullable=true)
public String getCmpName() {
return this.cmpname;
}
public void setCmpName(String cmpname) {
this.cmpname = cmpname;
}
@Column(name="CMP_COUNTRY", nullable=true)
public String getCmpCountry() {
return this.cmpcountry;
}
public void setCmpCountry(String cmpcountry) {
this.cmpcountry = cmpcountry;
}
}
然后在我的测试单元中,我创建一个 Companies 对象,然后将其保存在会话中:
Companies Companies = new Companies("qqq","FR");
session.save(Companies);
transaction.commit();
但每次都会删除Oracle 中 COMPANIES 表中的所有记录。
所以我第一次尝试我的数据时,它充满了很多行,并且在其他表中使用了这个主键,并且它能够删除 Companies 中的这些行,而不是其他表......
所以这意味着 Hibernate 实际上禁用我的所有约束,清空表,并从我的测试单元添加新行...
我做错了什么?
我不希望休眠弄乱我的数据,除非我告诉它这样做......
I have this entity class, the first one I built to test Hibernate, it uses an oracle sequence to fill the ID field :
@Entity
@Table(name="COMPANIES")
public class Companies {
private Integer cmpid;
private String cmpname;
private String cmpcountry;
public Companies() {
}
public Companies(String name, String country) {
this.cmpname = name;
this.cmpcountry = country;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "gen_CMP")
@SequenceGenerator(name="gen_CMP", sequenceName = "CMP_ID_SEQ")
@Column(name="CMP_ID", nullable=false)
public Integer getCmpId() {
return this.cmpid;
}
public void setCmpId(Integer cmpid) {
this.cmpid = cmpid;
}
@Column(name="CMP_NAME", nullable=true)
public String getCmpName() {
return this.cmpname;
}
public void setCmpName(String cmpname) {
this.cmpname = cmpname;
}
@Column(name="CMP_COUNTRY", nullable=true)
public String getCmpCountry() {
return this.cmpcountry;
}
public void setCmpCountry(String cmpcountry) {
this.cmpcountry = cmpcountry;
}
}
Then in my Test Unit, I create a Companies object then save it in the session:
Companies Companies = new Companies("qqq","FR");
session.save(Companies);
transaction.commit();
But every time, it delete ALL the records in the COMPANIES table in Oracle.
So the first time I tried on my data, it was filled with many rows, and with this primary keys used in other tables, and it was able to delete those rows in Companies and not the other tables...
So it means Hibernate actually disabled all my constraints, empty the table, and added the new row from my Test Unit...
What am I doing wrong?
I don't want hibernate to mess with my data unless I tell it to do so...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的休眠配置中,你需要设置 hbm2dll 来验证。像这样的事情:
如果它设置为创建,它将在每次运行时删除并重新创建数据库表。
此页面更详细地描述了该设置。
In your hibernate configuration you need to set hbm2dll to validate. Something like this:
If it is set to create it will drop and re-create your database tables every time it runs.
This page describes the setting in more detail.