With DataJpaTest Repository Tests using MySQL, Hibernate doesn't set User to auto-increment in H2 DB

TiggerToo :

I'm trying to test my Spring Boot repositories using DataJpaTest. I'm using MySQL, so all of my models are using IDENTITY for id generation:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

But as you can see from the screenshot below, when I run my tests, Hibernate sets "generated by default as identity" for the ids of all of my models except for User and Property:

screenshot of table generation

This makes it so that I cannot create users (or properties) in my tests, since GenerationType.IDENTITY sets always sets the id to null before sending it to the db, which results in the following error:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Does anyone know why this is happening? And, of course, more important than the why is what can I do to fix it? :)

Update To simplify the problem a bit and provide different errors for different things I've tried...

My DataJpaTests are using H2, which I guess is automagically generating the db structure from the code. My real DB is MYSQL, which is generated in a separate project.

Scenario 1: When I set GenerationType to IDENTITY, the live DB works, but the test DB gives me could not execute statement; SQL [n/a]; constraint [null].

Scenario 2: When I set GenerationType to AUTO, the test DB works, but the live DB gives me Table 'tablename.hibernate_sequence' doesn't exist.

Scenario 3: Trying to make MYSQL work with GenerationType AUTO. I have found nothing in quite a lot of searching on how to tell Hibernate to default MYSQL to identity instead of table. I've found rather a lot saying to never use table and that overriding this is impossible without changing the Hibernate source code. This is a bunch of BS that makes me wonder what the Hibernate developers are smoking.

Scenario 4: Trying to make H2 work with GenerationType IDENTITY. I've had some luck here with the following:

  1. Putting spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MYSQL in a test.properties file.
  2. Annotating my Test class with @TestPropertySource(locations= "classpath:test.properties") and @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

This gets rid of the null constraint error, but instead I'm now getting The database returned no natively generated identity value. Presumably, this is because, while the actual SQL code had auto_increment for the id, whatever magic is being used to generate the table for H2 isn't getting it there.

Addendum: The thing that is really frustrating about all of this is that it works totally fine for most of the tables. Out of the more than 20 tables in my DB, all but two of them auto generate and increment their ids just fine using IDENTITY. It makes no sense at all that some of them would work and some of them wouldn't.

TiggerToo :

The problem was another model with @Table(name = "user") as an annotation. That model also had the Id column defined, but without a generated value. With the conflict, Hibernate chose that one to define the id.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123634&siteId=1