Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I map a set of string objects using JPA Annotations?

@Entity
public class TestClass implements Serializable{
    private Integer id;
    private Set<String> mySet;

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    @OneToMany(cascade={CascadeType.ALL})
    public Set<String> getMySet() {
        return mySet;
    }
}

I get the following error.

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: TestClass.mySet[java.lang.String]

or if I leave off the @OneToMany

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: test_class, for columns: [org.hibernate.mapping.Column(my_sets)]

like image 579
ScArcher2 Avatar asked May 28 '09 15:05

ScArcher2


2 Answers

You'll find a pretty decent answer here. The rules for Lists apply to Sets too.

like image 188
Cogsy Avatar answered Nov 03 '22 20:11

Cogsy


Ooh oh, I had to do this one.

@CollectionOfElements(targetElement = String.class)
like image 40
Joshua Avatar answered Nov 03 '22 20:11

Joshua