Hibernate 在插入之前删除所有行

发布于 2024-09-18 23:40:34 字数 2023 浏览 6 评论 0原文

我有这个实体类,这是我为测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2024-09-25 23:40:34

在你的休眠配置中,你需要设置 hbm2dll 来验证。像这样的事情:

 <property name="hbm2ddl.auto">validate</property>

如果它设置为创建,它将在每次运行时删除并重新创建数据库表。

此页面更详细地描述了该设置。

In your hibernate configuration you need to set hbm2dll to validate. Something like this:

 <property name="hbm2ddl.auto">validate</property>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文