Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create unique key constraint not found

Tags:

java

hibernate

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?

like image 450
Dejell Avatar asked Nov 07 '11 19:11

Dejell


3 Answers

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.

like image 116
JB Nizet Avatar answered Oct 20 '22 19:10

JB Nizet


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
like image 29
Dennis Kim Avatar answered Oct 20 '22 21:10

Dennis Kim


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

like image 1
Alisher Gulov Avatar answered Oct 20 '22 19:10

Alisher Gulov