Primary key constraint generated for a "Set" based ElementCollection in Hibernate

Solution Verified - Updated -

Issue

  • Defining an @ElementCollection with an @Embeddable type using java.util.Set

    @Embeddable
    public class Task implements Serializable {
        @Column(nullable = false)
        private String name;
        ....
    }
    
    @Entity
    public class Employee ... {
        ...
        @ElementCollection
        @CollectionTable(name = "employee_tasks" ...)
        private Set<Task> tasks = new HashSet<Task>();
        ...
    }
    
  • Hibernate generates a primary key constraint on the collection table

    create table employee_tasks (
        Employee_name varchar(255) not null,
        name varchar(255) not null,
        primary key (Employee_name, name)
    )
    
  • No primary key constraint is generated when using java.util.List

Environment

  • Red Hat JBoss Enteprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.