Unable to create unique key constraint not found
Asked Answered
T

6

8

I have the following entity:

@Entity
@Table(name = "campaign_content", uniqueConstraints = @UniqueConstraint(columnNames = { "campaignContentId", "campaignId", "fieldTag" }))    
public class CampaignContent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "campaign_content_id")
    private Integer campaignContentId;

    @Column(name = "field_content")
    private String fieldContent;

    @Column(name = "campaign_id")
    private Integer campaignId;

    @Column(name = "field_tag")
    private String fieldTag;

With getter and setter.

However I get:

caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (campaignContentId, campaignId, fieldTag) on table campaign_content: campaignContentId, campaignId, fieldTag not found

What is wrong?

Thormora answered 7/11, 2011 at 19:44 Comment(1)
try "campaign_content_id", "campaign_id", "field_tag"Shelbyshelden
R
12

The name of the column is campaign_content_id, not campaignContentId. Same thing for the other columns, of course. The columnNames attribute expects an array of ... column names. Not an array of Java field or property names.

Reimport answered 7/11, 2011 at 19:48 Comment(2)
I thought that I need the logical names!Thormora
for my @ManyToOne private Location location;, the default column name was location_id - Location's PK column is named id. Without taking the time to confirm it, the pattern seems to be <local-reference-field>_<foreign-pk-field>Riccio
A
7

In my case this code works, one physical table field name and one entity object member field name.

@Table(uniqueConstraints={@UniqueConstraint(columnNames = {"account_id" , "measureDate"})})

but this code doesn't work at all with same exception.

@Table(uniqueConstraints={@UniqueConstraint(columnNames = {"account_id" , "measure_date"})})

Someone reported this bug to hibernate. Check this. https://forum.hibernate.org/viewtopic.php?f=9&t=986581&view=next

I use

  • spring boot
  • spring data
  • mysql
Assembly answered 19/8, 2015 at 8:9 Comment(1)
i faced the same issue. What i noticed is; if you extend your entity from another entity; for inherited column names, hibernate accepts logical naming(like; createdAt), but for not inherited column names it accepts column name how it appears in database(like; question_id).Clunk
T
1

If any of constrained column anotated with @Column(name="..."): You need add @Column(name = "...") to another constraining columns too.

Thora answered 26/1, 2022 at 10:21 Comment(0)
N
0

my problem solved by this Your table definition must look like this @Table(name = "campaign_content", uniqueConstraints = @UniqueConstraint(columnNames = { "campaign_content_id", "campaign_id", "field_tag" }))

Neocene answered 24/1, 2016 at 15:1 Comment(0)
I
0

For anyone getting this error while using Kotlin, my issue was caused by using @Column instead of @get:Column on a var. Doh!

Imp answered 10/2, 2020 at 17:36 Comment(0)
P
0

Simply if you see detail of error stack trace, it refers error to this class of Hibernate: InFlightMetadataCollectorImpl and it will go into this method processUniqueConstraintHolders. For better understanding of what every version of your spring boot can behave, This is a good point for debugging.

Totally this error occur when Hibernate needs to know about real name of database column.

This is what I have seen in hibernate 5.6.14:

if you put column name for your field, you have to use it in UniqueConstraint#columnNames, otherwise you have to put real field name.(ex: campaignContentId)

Personate answered 13/1 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.