@CollectionTable with Composite Keys

I am currently refactoring some of our old code, from Strut to Spring. It is a legacy application and some of the tables in the database don’t have foreign key constraints.

I am dealing with the above tables. A BookMarketingMedium (=a newspaper, magazine) can have zero or more BookMarketingMediumEmails (=when we inform the newspapers that an article has been published on our website). The BookMarketingMediumEmails table does not have any foreign keys and two composite keys: bookMarketingMediumId and languageId

To deal with the composite key, I had created an embeddable class:

BookMarketingMediumEmail.java:

@Data
@EqualsAndHashCode(of = { "email" })
@Embeddable
public class BookMarketingMediumEmail
{
    @EmbeddedId
    private BookMarketingMediumEmailIdentity bookMarketingMediumEmailIdentity;

    @NotNull
    private String subject = "";

    @Email
    @NotNull
    private String email = "";

    public BookMarketingMediumEmail(BookMarketingMediumEmailIdentity bookMarketingMediumEmailIdentity, String subject, String email)
    {
        this.bookMarketingMediumEmailIdentity = bookMarketingMediumEmailIdentity;
        this.subject = subject;
        this.email = email;
    }

}

BookMarketingMediumEmailIdentity.java:

@Data
@Embeddable
public class BookMarketingMediumEmailIdentity implements Serializable
{
    @NotNull
    private long bookMarketingMediumId;

    @NotNull
    private long languageId;
}

The problem occurs in this class

BookMarketingMedium.java:

@Data
@Entity
@Table(name = "bookMarketingMedium")
public class BookMarketingMedium
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int bookMarketingMediumId;

    @NotNull
    private String medium;

    @NotNull
    private String mediumNameEN;

    @NotNull
    private String mediumNameGE;

    private String mailFrom;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "bookMarketingMediumEmail")
    @JoinColumns({
            @JoinColumn(name = "bookMarketingMediumId"),
            @JoinColumn(name = "languageId")
    })
    private Set<BookMarketingMediumEmail> bookMarketingEmail = new HashSet<>();

}

What I would like to achieve is that when I ‘get’ the BookMarketingMedium it should also load all the BookMarketingMediumEmails that are associated with that particular ‘medium’.

Unfortunately, I get the following BeanCreatingException:

2020-02-20 16:05:57,042 ERROR [org.springframework.web.context.ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/appContext-persistence.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: com.getabstract.common.model.pps.bookmarketing.BookMarketingMediumEmailIdentity must not have @Id properties when used as an @EmbeddedId: com.getabstract.common.model.pps.bookmarketing.BookMarketingMedium.bookMarketingEmail.collection&&element.bookMarketingMediumEmailIdentity
2020-02-20 16:05:57,403 JAVA ProResin[id=default] started in 61801ms
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/appContext-persistence.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: com.getabstract.common.model.pps.bookmarketing.BookMarketingMediumEmailIdentity must not have @Id properties when used as an @EmbeddedId: com.getabstract.common.model.pps.bookmarketing.BookMarketingMedium.bookMarketingEmail.collection&&element.bookMarketingMediumEmailIdentity

Caused by: org.hibernate.AnnotationException: com.getabstract.common.model.pps.bookmarketing.BookMarketingMediumEmailIdentity must not have @Id properties when used as an @EmbeddedId: com.getabstract.common.model.pps.bookmarketing.BookMarketingMedium.bookMarketingEmail.collection&&element.bookMarketingMediumEmailIdentity

Can someone please advise me what I’m doing wrong?

Thanks

Massimo