mercredi 24 février 2016

Detecting duplicate table name via unit / integration test

On a couple of occasions now, as a result of copy-paste, I have created two JPA entities with the same table name. i.e.:

@Table(name = "myfirsttable")
public class MyFirstTable { @Id @Column private Long id; }

@Table(name = "myfirsttable")
public class MySecondTable { @Id @Column private Integer id; }

I'm using Spring Test, which means that fortunately at least one test fails when I do this. The trouble is that the failures I see will complain about data types. For example, in the above, I would see an exception raised from HibernateJpaAutoConfiguration.class such as expected int but found bigint for myfirsttable. If I look at the class which is supposed to be myfirsttable, I get confused (I'm easily confused), thinking "But it says it's a Long, so surely bigint is the correct mapping?" It can take me a while to work out why I'm seeing that particular message. Similarly, the stack trace may mention being unable to find a field.

So far, there are only a couple of occasions when I have felt the need to create two differing entities pointing at the same table, so as a means of covering the 99% of cases where two entities pointing at the same table is an error, I was wondering whether there is a simple way to set up a test, which would fail in a way that tells me up front that I have created a duplicate table name. I'm thinking about a single test that I can put into all of my projects, which could give me a useful warning identifying this issue.

Aucun commentaire:

Enregistrer un commentaire