We're updating the issue view to help you get more done. 

Hibernate is not creating unique constraint specified in @JoinTable

Description

Hi,

When using join table for many to many relationship i have specified unique constraint, but the same has not been created in Database.

Here is my entity definition :-

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 @Cacheable @Entity(name = "ImageGroup") @Table(name = "\"image_group\"", indexes = { @Index(name = "\"img_grp_tenant_name_idx\"", columnList = "\"tenant\",\"name\""), @Index(name = "\"img_grp_tenant_idx\"", columnList = "\"tenant\"") }, uniqueConstraints = { @UniqueConstraint(name = "\"img_grp_tenant_name_uq\"", columnNames = { "\"tenant\"", "\"name\"" }), }) @SequenceGenerator(name = "image_group_id_sequence", sequenceName = "\"image_group_id_seq\"", initialValue = 1) public class ImageGroupDO { /** The id. */ @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "image_group_id_sequence") @Column(name = "\"id\"") private long id; /** The name. */ @NotNull @Size(min = 2, max = 255) @Column(name = "\"name\"", nullable = false, length = 255) private String name; /** The description. */ @Size(max = 255) @Column(name = "\"description\"", length = 255) private String description; /** The active. */ @NotNull @Column(name = "\"active\"", nullable = false) private boolean active; /** The tenant do. */ @NotNull @ManyToOne(fetch = FetchType.EAGER, optional = false) @JoinColumn(name = "\"tenant\"", foreignKey = @ForeignKey(name = "\"img_grp_tenant_fk\"")) private TenantDO tenantDo; /** The images. */ @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinTable(name = "\"image_group_image\"", joinColumns = @JoinColumn(name = "\"image_group\"", referencedColumnName = "\"id\""), inverseJoinColumns = @JoinColumn(name = "\"image\"", referencedColumnName = "\"id\""), foreignKey = @ForeignKey(name = "\"img_grp_img_image_group_fk\""), inverseForeignKey = @ForeignKey(name = "\"img_grp_img_image_fk\""), indexes = { @Index(name = "\"img_grp_img_image_group_fk_idx\"", columnList = "\"image_group\""), @Index(name = "\"img_grp_img_image_fk_idx\"", columnList = "\"image\"") }, uniqueConstraints = { @UniqueConstraint(name = "\"img_grp_img_img_grp_img_uq\"", columnNames = { "\"image_group\"", "\"image\"" }) }) private Set<ImageDO> images = new HashSet<>(); /** The created at. */ @NotNull @Column(name = "\"created_at\"", nullable = false) private Date createdAt; /** The modified at. */ @NotNull @Column(name = "\"modified_at\"", nullable = false) private Date modifiedAt; /** * On create. */ @PrePersist protected void onCreate() { this.createdAt = new Date(); this.modifiedAt = new Date(); } /** * On update. */ @PreUpdate protected void onUpdate() { this.modifiedAt = new Date(); } /** * Gets the id. * * @return the id */ public long getId() { return id; } /** * Sets the id. * * @param id * the new id */ public void setId(long id) { this.id = id; } /** * Gets the name. * * @return the name */ public String getName() { return name; } /** * Sets the name. * * @param name * the new name */ public void setName(String name) { this.name = name; } /** * Gets the description. * * @return the description */ public String getDescription() { return description; } /** * Sets the description. * * @param description * the new description */ public void setDescription(String description) { this.description = description; } /** * Checks if is active. * * @return true, if is active */ public boolean isActive() { return active; } /** * Sets the active. * * @param active * the new active */ public void setActive(boolean active) { this.active = active; } /** * Gets the tenant do. * * @return the tenant do */ public TenantDO getTenantDo() { return tenantDo; } /** * Sets the tenant do. * * @param tenantDo * the new tenant do */ public void setTenantDo(TenantDO tenantDo) { this.tenantDo = tenantDo; } /** * Gets the images. * * @return the images */ public Set<ImageDO> getImages() { return images; } /** * Sets the images. * * @param images * the new images */ public void setImages(Set<ImageDO> images) { this.images = images; } /** * Gets the created at. * * @return the created at */ public Date getCreatedAt() { return createdAt; } /** * Sets the created at. * * @param createdAt * the new created at */ public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; } /** * Gets the modified at. * * @return the modified at */ public Date getModifiedAt() { return modifiedAt; } /** * Sets the modified at. * * @param modifiedAt * the new modified at */ public void setModifiedAt(Date modifiedAt) { this.modifiedAt = modifiedAt; } /* * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof DataCenterDO)) return false; ImageGroupDO imageGroupDO = ImageGroupDO.class.cast(obj); return this.id == imageGroupDO.getId(); } /* * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return new Long(id).intValue(); } }

The unique constraint that i have added to the join table is not getting created in Postgresql.

Any unique constraint at entity level with @table is working fine.

Here is the config :-

jdbc.connection.datasource=org.postgresql.ds.PGSimpleDataSource
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

Update1 :- Also not working for Oracle 11G
Update2 :- Also not working for SqlServer 2016

Environment

Postgresql, Linux, Spring Data JPA Repository, Oracle 11g

Status

Assignee

Unassigned

Reporter

Avinash G A

Components

Affects versions

5.2.6

Priority

Major