'Hibernate Entity and View-Entity use same ElementCollection Table

tldr: How do is associate the same element collection table from Entity and View Entity with Hibernate?

I try to use Hibernate to fetch data from a view. The view is based on a single table which has a 1:n-association to another table. I'd like to have this association in the view Entity also, targeting the same table of cause ;) desired table layout

I tried to archive this by using @ElementCollection as below:

enum Qualification { PROFESSIONAL, STUDENT}

@Entity
class JobEntity {
     @Id
     Long id;

     @ElementCollection
     Set<Qualification> qualifications;
     // ...
}

@Entity
@Immutable
class JobViewEntity {
     @Id 
     Long id;

     @ElementCollection
     @CollectionTable(name = "job_entity_qualifications", joinColumns = 
     @JoinColumn(name = "job_id"))
     Set<Qualification> qualifications;
}

When starting this application, Hibernate tries to generate the table job_entity_qualifications twice (once forced from JobEntity and from JobViewEntity). If I drop the @CollectionTable Annotation, Hibernate creates job_entity_qualification and job_view_entity_qualification as independent tables, which is not intended ;)

I have the feeling that I miss one Annotation on the JobViewEntity.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source