0

I have a class defined as the following;

@Entity
@Table(name = "student")
@Getter
@Setter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_id_seq")
    @SequenceGenerator(name = "student_id_seq", sequenceName = "student_id_seq", allocationSize = 1)
    private Long id;

    @ToString.Exclude
    @Builder.Default
    @OneToMany(mappedBy = "studentId", targetEntity = Note.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private LinkedHashSet<Note> notes = new LinkedHashSet<>();
}

However, when I try to run the program, the following error logs occur;

Caused by: org.hibernate.AnnotationException: java.util.LinkedHashSet collection type not supported for property: com.example.studentmanagementservice.model.Student.notes
    at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:316)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1956)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:979)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:806)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:248)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:239)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:282)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    ... 21 common frames omitted

And also the following error;

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: java.util.LinkedHashSet collection type not supported for property: com.example.studentmanagementservice.model.Student.notes

How can I use LinkedHashSet inside an entity?

hakansander
  • 337
  • 4
  • 13
  • `LinkedHashSet` is backed by a hashmap. How do you map a hashmap onto a database table? – Tim Biegeleisen Jan 13 '22 at 10:00
  • 3
    Why do you want to use a LinkedHashSet? Do you want your entities inside the Set in a specific order? If yes you can archieve that by using the javax.persistence.OrderBy annotation and just declaring the field as Set. Hibernate will then use its own internal implementation of Set that allows for deterministic orders. – OH GOD SPIDERS Jan 13 '22 at 10:03
  • It may be helps you https://stackoverflow.com/questions/4243865/does-hibernate-preserve-the-order-of-a-linkedhashset-and-if-so-how – vleg1991 Jan 13 '22 at 12:43
  • Yes I want my entities in a Set in specific order so I have tried to use LinkedHashSet. OrderBy annotation and using Set solved my problem. Thanks for your comments. – hakansander Jan 13 '22 at 21:07

1 Answers1

1

You need to declare the type of collection from one of the following types:

  • java.util.List
  • java.util.Set
  • java.util.SortedSet
  • java.util.Map
  • java.util.SortedMap
  • java.util.Collection

Change the following line :

private LinkedHashSet<Note> notes = new LinkedHashSet<>();

to

private Set<Note> notes = new LinkedHashSet<Note>();