2009-05-28 8 views
6
@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; 
    } 
} 

Je reçois l'erreur suivante.Comment mapper un ensemble d'objets chaîne à l'aide des annotations JPA?

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

ou si je pars de @OneToMany

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

+0

L'annotation @CollectionOfElements de Hibernate m'a permis de résoudre le problème. C'est spécifique hibernate, mais je ne prévois pas d'échanger ma mise en œuvre JPA. – ScArcher2

Répondre

6

Vous trouverez une réponse assez décente here. Les règles pour les listes s'appliquent également aux ensembles.

5

Ooh oh, je devais faire celui-ci.

@CollectionOfElements(targetElement = String.class) 
+0

C'est aussi ce que je devais faire, mais c'est spécifique hibernate. Dans ma situation, ça me va. – ScArcher2

+1

@ElementCollection en JPA2, cette réponse est la meilleure approche. voir http://stackoverflow.com/questions/287201/how-to-persist-a-property-of-type-liststring-in-jpa – pdem

Questions connexes