0

I deleted my tables to let them be recreated by Spring JPA, but spring does not create them. Instead, I'm getting the following exception:

Unable to create unique key constraint (guild_id, setting_key) on table guild_setting: database column 'guild_id' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)

I don't know why I get this error, but before I deleted the tables in the database, the column name was guild_id, so what JPA says is not right.

This is an excerpt of the Entity:

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"guild_id", "setting_key"}))
public class GuildSetting extends Setting {

    @Column(nullable = false)
    private long guildId;

The following properties are set with the spring.datasource properties:

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
Nightloewe
  • 918
  • 1
  • 14
  • 24
  • What's your MySQL version? Can you try this? spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect spring.jpa.properties.hibernate.dialect.storage_engine=innodb spring.jpa.hibernate.ddl-auto=create – Govind Jun 16 '20 at 16:41
  • https://stackoverflow.com/questions/438146/what-are-the-possible-values-of-the-hibernate-hbm2ddl-auto-configuration-and-wha – Gurkan İlleez Jun 16 '20 at 16:43
  • I‘m using MariaDB – Nightloewe Jun 16 '20 at 16:57
  • 1
    Side note: consider Liquibase or Flyway to generate your db tables – Puce Jun 16 '20 at 19:49

1 Answers1

1

If you don't want to use @Column(name="guild_id")

You should use @UniqueConstraint(columnNames={"guildId", ...}

The generated table will contain the (correct) column, named guild_id

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"guildId", "setting_key"}))
public class GuildSetting extends Setting {

    @Column(nullable = false)
    private long guildId;

Agreed, this looks like a bug...


Note: You did not mention you have problems with the setting_key unique constraint.

Do you use @Column(name="setting_key") or private String setting_key?

Dirk Deyne
  • 6,048
  • 1
  • 15
  • 32
  • I use "@Column(name=setting_key) private String key" – Nightloewe Jun 17 '20 at 00:09
  • So using `@Column(name="guild_id")` will solve your problem, right? It's is probably a bug in Hibernate. – Dirk Deyne Jun 17 '20 at 05:02
  • I changed the constraint to "guildId" and "key", this apparently works. There are problems in the naming with Spring. I never had problems using custom names and UniqueConstraints with only Hibernate. – Nightloewe Jun 17 '20 at 13:15
  • 1
    @Nightloewe if this answer works, please accept it. As far as I can see in the debugger it is a Hibernate problem and not a Spring problem. – Dirk Deyne Jun 17 '20 at 13:22