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, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content