0

I have this model:

@Entity
public class ImageModel extends Model {
@Id
private String id;
private String url;

@ManyToMany(cascade = CascadeType.ALL)
private Map<String,String> tags = new HashMap<>();

public void add(String key, String value){
    tags.put(key,value);
}

public String get(String key){
     return tags.get(key);
}

}

This how I create a new object:

 ImageModel imageModel = new ImageModel();
/* fill object with data */
imageModel.save(); // I saved it

But when I try to retrieve data, the HashMap has zero records:

HashMap<String, String> imageTags = (HashMap<String, String>) imageModel.getTags();

What can I do, so the informations from hashmap will be saved too? I have already looked to the other SO posts and I tried, but nothing seems to work: @ManyToMany(cascade = CascadeType.ALL) JPA Map<String,String> mapping

I must say that I am using Play framework 2.1 .

Community
  • 1
  • 1
Alin Ciocan
  • 3,082
  • 4
  • 34
  • 47
  • You need to describe, through more annotations, which entities the map corresponds to, `@Join` and such. http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html – Matt Ball May 24 '13 at 17:08
  • @Matt Ball, thank you for your response so quickly and I appreciate your help. But could you tell me more. I have no experience with JPA. Why do I need @Join? I just want to store that hash map and to retrieve it. ( I am sorry, for my silliness). – Alin Ciocan May 24 '13 at 17:14
  • By definition, a many-to-many means that you want to join across tables, but you haven't told JPA what tables to join. If you simply want to store the `Map` in the entity's table, **don't use `@ManyToMany` (or any other relationship).** – Matt Ball May 24 '13 at 17:18

1 Answers1

0

You seem to be trying to use a ManyToMany, which is used when referencing entities that have their own table and identity. Strings are not entities, so you likely want to use an elementCollection for simple types. ElementCollections are discussed here http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/ElementCollections with a simple example using a map shown here: http://wiki.eclipse.org/EclipseLink/Development/JPA2.0/Extended_Map_support#Basic_-_Basic.2FEmbeddable

Chris
  • 20,138
  • 2
  • 29
  • 43
  • I tried with a List. I changed HashMap with List and put @ElementCollections, but it still have zero records. What I didn't understood was if I need to do any XML file with entity? – Alin Ciocan May 24 '13 at 17:41