简体   繁体   中英

Unable to create unique key constraint not found

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;

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.

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

我的问题由此解决了您的表定义必须如下所示 @Table(name = "campaign_content", uniqueConstraints = @UniqueConstraint(columnNames = { "campaign_content_id", "campaign_id", "field_tag" }))

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

如果任何用@Column(name="...") 注释的约束列,您还需要将@Columnt(name = "...") 添加到另一个约束列

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM