Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Collection has unwanted unique constraint in mapping table

When I auto generate my database with hibernate.hbm2ddl.auto=create an 'unwanted' unique constraint is being created in a mapping table. I am running 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 )
)

The unwanted constraint is CONSTRAINT scanalerts_alerts_id_key UNIQUE (alerts_id ), basically I only want the unique constraint on scanid and alerts_id.

I'm using JPA annotations to create the 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 any way to stop the creation of the alert_id unique constraint?

Thanks

@JBNizet Here is the Alert mapping annotations:

@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;
like image 299
user2274508 Avatar asked Apr 12 '13 13:04

user2274508


1 Answers

As said for example in JavaDocs, @ElementCollection is used to map collection of basic types or embeddables. Alert is an entity and consequently List<Alert> is not collection of Basic types or embeddables.

Because unique constraint that consists of scanid and alert_id is preferred, I assume that relationship between Scan and Alert does have many-to-many nature. That 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;

Primary key of table contains both columns and that's why using @UniqueConstraint is not needed.

like image 143
Mikko Maunu Avatar answered Oct 20 '22 08:10

Mikko Maunu