Good morning Stackoverflow,
I have the problem that it gives me the error:
Failed to create sessionFactory object.org.hibernate.AnnotationExcept
You should map to interfaces and not implementations. This:
@OneToMany(cascade=CascadeType.ALL, targetEntity=CoachGroup.class)
@JoinColumn(name="id")
private TreeSet<CoachGroup> coachGroups = new TreeSet<>();
Should be (also replaced the TreeSet because a HashSet is enough here):
@OneToMany(cascade=CascadeType.ALL, targetEntity=CoachGroup.class)
@JoinColumn(name="id")
private Set<CoachGroup> coachGroups = new HashSet<>();
You are not allowed to use a concrete implementation on the entities field declaration. You are allowed to use one of the following:
So in your case it would have to be:
@OneToMany(cascade=CascadeType.ALL, targetEntity=CoachGroup.class)
@JoinColumn(name="id")
private Set<CoachGroup> coachGroups = new TreeSet<>();
Another possible reasons for this exception to occur is using a non-collection object for
@ManyToMany
and@OneToMany
mappings Or using collection object for @ManyToOne
and @OneToOne
mappings. All examples below are incorrect.
INCORRECT
@ManyToMany
private User user;
@ManyToOne
private User user;
@OneToOne
private List<User> users;
@ManyToOne
private List<User> users;
You can't save your collection fields as Concrete classes.
Got this,
As a requirement persistent collection-valued fields must be declared as an interface type (see Example 7.2, “Collection mapping using @OneToMany and @JoinColumn”). The actual interface might be java.util.Set, java.util.Collection, java.util.List, java.util.Map, java.util.SortedSet, java.util.SortedMap or anything you like ("anything you like" means you will have to write an implementation of org.hibernate.usertype.UserCollectionType).
From Chapter 7. Collection Mapping.
You can use below code to save a sorted set(KINDLY READ THE COMMENTS):
@OneToMany(cascade=CascadeType.ALL) //Removed targetEntity, as you are already using generics.
@JoinColumn(name="team_id") // Use this name as to show the presence of foreign key of EducationTeam in CoachGroup.
@SortNatural // Make sure that your CoachGroup Entity has implemented Comparable<CoachGroup> interface which wii be used while sorting.
private SortedSet<CoachGroup> coachGroups = new TreeSet<>();
The Exception is straightforward and says : Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements, so the cause is obvious here and if we take a look at the Hibernate Collection mapping documentation it clearly states that:
As a requirement persistent collection-valued fields must be declared as an interface type (see Example 7.2, “Collection mapping using @OneToMany and @JoinColumn”). The actual interface might be
java.util.Set
,java.util.Collection
,java.util.List
,java.util.Map
,java.util.SortedSet
,java.util.SortedMap
...
And you used TreeSet which is an implementation class for both Set<E>
and SortedSet<E>
interfaces. So your actual mapping won't work with TreeSet
, you should use a Set<CoachGroup>
instead of a TreeSet<CoachGroup>
:
private Set<CoachGroup> coachGroups = new HashSet<CoachGroup>();