본문으로 바로가기

 

 org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements:

 

# 원인

 

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:

 

에러 메시지 내용을 그대로 해석해보면,

@ManyToMany, @OneToMany, @CollectionOfElements  annotation은 non collection 객체와 매핑될 수 없다는 의미이다.

 

나의 경우에는 @OneToMany를 붙여놓고 일반 Java 단일객체를 연결해서 문제가 되었다.

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="PRODUCTID")
Product product;

 

# 해결

 

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.Setjava.util.Collectionjava.util.Listjava.util.Mapjava.util.SortedSetjava.util.SortedMap...

 

Collection 객체인 List 로 바꾸어주었다.

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="PRODUCTID")
List<Product> product;

 


# 참고자료

 

https://stackoverflow.com/questions/44258541/illegal-attempt-to-map-a-non-collection-as-a-onetomany-manytomany-or-collec

 

“Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements”

Good morning Stackoverflow, I have the problem that it gives me the error: Failed to create sessionFactory object.org.hibernate.AnnotationException: Illegal attempt to map a non collection ...

stackoverflow.com

 

반응형