JPA collection has an undesirable unique constraint in the mapping table

When I automatically create my database using hibernate.hbm2ddl.auto=create , a "unnecessary" unique constraint is created in the mapping table. I run postgres 9.1, the create table statement becomes:

 CREATE TABLE schemaname.scanalerts ( scanid bigint NOT NULL, alerts_id bigint NOT NULL, CONSTRAINT fkd65bd7541b5b1a8e FOREIGN KEY (scanid) REFERENCES rfid.scan (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT fkd65bd754860b0886 FOREIGN KEY (alerts_id) REFERENCES rfid.alert (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT scanalerts_alerts_id_key UNIQUE (alerts_id ), CONSTRAINT scanalerts_scanid_alerts_id_key UNIQUE (scanid , alerts_id ) ) 

Unwanted CONSTRAINT scanalerts_alerts_id_key UNIQUE (alerts_id ) restriction CONSTRAINT scanalerts_alerts_id_key UNIQUE (alerts_id ) , basically I want only a unique restriction on scanid and warn_id.

I am using JPA annotations to create a mapping, here is my code:

 @ElementCollection(targetClass = Alert.class, fetch = FetchType.EAGER) @CollectionTable(name = "scanalerts", schema = RfidConstants.SCHEMA, joinColumns = @JoinColumn(name = "scanid"), uniqueConstraints = @UniqueConstraint(columnNames = { "scanid", "alerts_id" })) private List<Alert> alerts; 

Is there a way to stop creating a unique alert_id constraint?

thanks

@JBNizet Here are annotations for displaying alerts:

 @Entity @Table(name = "alert", schema = "schemaname", uniqueConstraints = @UniqueConstraint(columnNames = {"message", "alertPriority"}) ) public class Alert implements Serializable { @Id @Column(name="id") @SequenceGenerator(name = "alertSeq", sequenceName="ALERT_SEQ", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "alertSeq") private Long id; @Column(name="versionnum") @Version private Long version; @Column(name = "message", nullable = false) private String message; @Column(name = "alertpriority", nullable = false) @Enumerated(EnumType.STRING) private AlertPriority alertPriority; 
+8
source share
2 answers

As stated in > JavaDocs , @ElementCollection used to map a collection of base types or attachments. Alert is an entity and, therefore, List<Alert> is not a collection of basic types or attachments.

Since the only restriction consisting of scanid and alert_id is preferred, I assume the relationship between Scan and Alert is many-to-many. What can be achieved as follows:

 @ManyToMany @JoinTable(name = "scanalerts", schema = RfidConstants.SCHEMA, joinColumns = @JoinColumn(name = "scanid"), inverseJoinColumns = @JoinColumn(name = "alert_id") ) private List<Alert> alerts; 

The main key of the table contains both columns and why the use of @UniqueConstraint not required.

+8
source

The created restriction is declared here:

 uniqueConstraints = @UniqueConstraint(columnNames = { "scanid", "alerts_id" })) 

Just remove this and declare a constraint inside the alert object for each attribute.

0
source

All Articles