2010-09-27 3 views
2

Je veux persister la classe suivante, y compris « bar »: Comment persister classe qui contient la carte <String, Liste <String>>

 
Class Foo 
{ 
    Map<String, List<String>> bar; 
    // ... 
    // other stuff..... 
} 

Comment puis-je faire en veille prolongée? Si possible, comment faire cela dans Hibernate avec des annotations? Oh, la nature de la chaîne de clé Map est qu'il y en a un petit nombre (5-40), et ils sont identiques sur les instances Foo. Les chaînes de la liste sont uniques à la fois dans la liste et entre les instances de Foo.

Merci.

+0

Et quelle serait la représentation de la base de données? –

Répondre

1

Ni cordes ni liste est une entité, vous devez créer une classe d'emballage qui encapsule votre liste

Ne pas oublier de poseur

@Entity 
public class Foo { 

    private MutableInt id = new MutableInt(); 

    private Map<String, CustomList> customListMap = new HashSet<String, CustomList>(); 

    @Id 
    @GeneratedValue 
    public Integer getId() { 
     return this.id.intValue(); 
    } 

    public void setId(Integer id) { 
     return this.id.setValue(id); 
    } 

    @OneToMany 
    @MapKey(name="key") 
    public Map<String, CustomList> getCustomListMap() { 
     return customListMap; 
    } 

    // add convenience method 
    public void addBar(String key, String bar) { 
     if(customListMap.get(key) == null) 
      customListMap.put(key, new CustomList(new CustomListId(id, key))); 

     customListMap.get(key).getBar().add(bar); 
    } 

} 

Et votre CustomList personnalisé (Ne pas oublier poseur de)

@Entity 
public class CustomList { 

    private CustomListId customListId; 

    private List<String> bar; 

    private String key; 

    @EmbeddedId 
    public CustomListId getCustomListId() { 
     return customListId; 
    } 

    @Column(insertable=false, updatable=false) 
    public String getKey() { 
     return this.key; 
    } 

    @CollectionOfElements 
    @JoinTable(name="BAR") 
    public List<String> getBar() { 
     return this.bar; 
    } 

    @Embeddable 
    public static class CustomListId implements Serializable { 

     private MutableInt fooId = new MutableInt(); 
     private String key; 

     // required no-arg construtor 
     public CustomList() {} 
     public CustomList(MutableInt fooId, String key) { 
      this.fooId = fooId; 
      this.key = key; 
     } 

     public Integer getFooId() { 
      return fooId.intValue(); 
     } 

     public void setFooId(Integer fooId) { 
      this.fooId.setValue(fooId); 
     } 

     // getter's and setter's 

     public boolean equals(Object o) { 
      if(!(o instanceof CustomListId)) 
       return false; 

      CustomListId other = (CustomList) o; 
      return new EqualsBuilder() 
         .append(getFooId(), other.getFooId()) 
         .append(getKey(), other.getKey()) 
         .isEquals(); 
     } 

     // implements hashCode 

    } 

} 

Vous pouvez même créer une méthode personnalisée appelée getBar qui en encapsule votre customListMap de manière transparente comme suit

@Entity 
public class Foo { 

    ... 

    public Map<String, List<String>> getBar() { 
     Map<String, List<String>> bar = new HashMap<String, List<String>>(); 

     for(Entry<String, CustomList> e: customListMap()) 
      bar.put(e.getKey(), e.getValue().getBar()); 

     return bar; 
    } 

} 
+0

La classe CustomList est dans la classe CustomList, ce qui me donne une erreur de compilation: – ggg

+0

CustomList.java:33: CustomList est déjà défini dans le package sans nom public static class CustomList implements Serializable { – ggg

Questions connexes