DBUnit Sybase无法CLEAN_INSERT [英] DBUnit Sybase unable to CLEAN_INSERT

查看:247
本文介绍了DBUnit Sybase无法CLEAN_INSERT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据DBUnit文档链接,它应该支持在使用DatabaseSequenceFilter时使用CLEAN_INSERT 。

  com时,我得到以下异常。 sybase.jdbc2.jdbc.SybSQLException:引用完整性约束中的依赖外键约束违例。 dbname ='my_db',table name ='tbl_child',constraint name ='fk_tbl_child_parentid'。 

我的表格如下:

  CREATE TABLE dbo.tbl_parent 

parent_id numeric(19,0)IDENTITY,
...


CREATE TABLE dbo.tbl_child

child_id numeric(19,0)IDENTITY,
parent_id numeric(19,0)NOT NULL,
...


ALTER TABLE dbo.tbl_child
ADD CONSTRAINT fk_tbl_child_parentid
FOREIGN KEY(parent_id)
REFERENCES dbo.tbl_parent(parent_id)



我的DBUnit数据集看起来像这样:

 < dataset> 
< tbl_parent parent_id =41... />

< tbl_child child_id =1361parent_id =41/>
< / dataset>

所以在我的测试类中,我有这个代码,如果数据已经存在于数据库中,并且由于外键约束而无法删除

  @Before 
public void setUp()throws Exception {
InsertIdentityOperation.CLEAN_INSERT.execute(getConnection(),getDataSet());
}

有趣的是,一个解决方法是使用REFRESH而不是CLEAN_INSERT,理想,因为垃圾数据可能驻留在数据库中造成副作用:

  @Before 
public void setUp {
InsertIdentityOperation.REFRESH.execute(getConnection(),getDataSet());有没有人能够得到这个CLEAN_INSERT与Sybase和外键一起使用?
}


$ b <阅读其他帖子MySQL的类似问题,所以也许这里有一个常见的问题(或我不理解的东西)



/ p>

我现在在再次遇到相同问题后又添加了自己的解决方法。



我使用Sybase ASE 15 + DBUnit 2.4.8

解决方案

我得出的结论是,这是一个Sybase + DBUnit的问题,相同的问题在另一个项目(虽然我使用相同的JAR文件版本和数据库服务器)。



解决方案我最终解决了以下

  @Before 
public void setUp()throws Exception {
InsertIdentityOperation.TRUNCATE_TABLE.execute(getConnection(),getDataSet()) ;
InsertIdentityOperation.INSERT.execute(getConnection(),getDataSet());需要注意的是,TRUNCATE显然会清除数据库表中的所有数据。当测试运行,但这是我可以住的东西。至少我知道数据库中有什么数据,我的测试不太可能失败,因为在测试执行之间运行的其他开发人员插入错误的记录。可能是为什么你想要一个专用的数据库进行单元+集成测试的好理由。


According to the DBUnit docs link it should support using a CLEAN_INSERT when using a DatabaseSequenceFilter.

I get the following exception when attempting this

com.sybase.jdbc2.jdbc.SybSQLException: Dependent foreign key constraint violation in a referential integrity constraint. dbname =  'my_db', table name = 'tbl_child', constraint name = 'fk_tbl_child_parentid'.

My tables look like this:

CREATE TABLE dbo.tbl_parent
(
    parent_id            numeric(19,0) IDENTITY,
    ...
)

CREATE TABLE dbo.tbl_child
(
    child_id            numeric(19,0) IDENTITY,
    parent_id           numeric(19,0) NOT NULL,
    ...
)

ALTER TABLE dbo.tbl_child
    ADD CONSTRAINT fk_tbl_child_parentid
    FOREIGN KEY (parent_id)
    REFERENCES dbo.tbl_parent (parent_id)

And my DBUnit dataset looks like this:

<dataset>
  <tbl_parent parent_id="41" ... />

  <tbl_child child_id="1361" parent_id="41"/>
</dataset>

So within my test class where I have this code I get the error if data already exists in the database and it cannot be removed because of the foreign key constraints

@Before 
public void setUp() throws Exception {
    InsertIdentityOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
}

Interestingly a work around is to use REFRESH instead of CLEAN_INSERT but this is less than ideal because junk data could reside in the database causing side effects:

@Before 
public void setUp() throws Exception {
    InsertIdentityOperation.REFRESH.execute(getConnection(), getDataSet());
}

Has anyone been able to get this CLEAN_INSERT to work with Sybase and foreign keys? From reading other posts similar issues exist for MySQL so maybe there is a common issue here (or me not understanding something)

[EDIT]

I have now added my own workaround answer after running into the same problem again.

I am using Sybase ASE 15 + DBUnit 2.4.8

解决方案

I have come to the conclusion that this is indeed a problem with Sybase + DBUnit after hitting the same issues on another project (although I am using the same JAR file versions and database server).

The workaround I finally settled on was the following

@Before 
public void setUp() throws Exception {
    InsertIdentityOperation.TRUNCATE_TABLE.execute(getConnection(), getDataSet());
    InsertIdentityOperation.INSERT.execute(getConnection(), getDataSet());
}

The caveat is that the TRUNCATE will obviously blow away all data in the database tables when the test runs, but this is something I can live with. At least I know for certain what data is in the database and my tests are less likely to fail due to erroneous records being inserted by other developers running in between test executions. Probably a good reason why you want a dedicated database for unit + integration tests.

这篇关于DBUnit Sybase无法CLEAN_INSERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆