5

I'm using DBUnit during integration tests for mocking data for a legacy Spring/Hibernate project I've inherited. Unfortunately a long while ago someone decided that it would be nice to have a user table called user, which is a reserved keyword in our DB. This was then solved by renaming the entity table mapping to `user` which now has been living on for ages.

As I've now a couple years later started to incorporate integration testing this presents a problem as DBUnit does not allow ` characters in it's xml dataset files (it is XML you know...) and the CSV reader would require the file to be called `user`.csv which seems to fail under my OS.

Is there any way to add this table without using either XML or CSV readers of DBUnit, or should I tacke this problem from another angle? One alternative would be to i.e. use another mapping during integration tests so that I could name the table to it's intended name (user). But I dont know whether that is doable or not.

I've been fighting this for 2 days now so I'd love some external feedback.

So, just to clarify. I have an entity such as:

@Entity (name = "`user`")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Auditable
public class User implements UserDetails, Serializable {
  properties etc....

During my integration tests hibernate takes care of creating this an other tables by configuration of the persistence.xml file. When dbunit then tries to populate the data I get errors due to missing table irregardless of how I try to escape the user table name.

Billybong
  • 697
  • 5
  • 18

1 Answers1

2

I suspect you need the escape pattern configuration.

Something resembling

dbunitConn.getConfig().setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "\"?\"");

where dbUnitConn is your dbUnit connection, should do the trick.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • I've read about the escape pattern, but I'm not sure how to use it in this context. If I set the escape pattern to ? what should the element in the testdata xml be then? – Billybong Dec 16 '13 at 13:03
  • I think you should then just be able to use the reserved word user, and it will recognize it as needing the escape and apply it. If you use the one I put in the answer it should quote the table name which works for many databases. – Don Roby Dec 16 '13 at 14:06