Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Map<String,String> using Hibernate

It seems that everywhere I look, there are outdated versions of this which no longer work. My problem seems really simple. I have a class in Java, which maps to a derby database. I'm using annotations, and have successfully managed to create all the other tables in my DB, yet with this specific example, where I just need a Map, which does not use any other class, just two simple string values. I have come across all types of errors trying everything I've found online.

Does anybody know of a simple way of doing this, without using deprecated annotations?

Thanks in advance!

like image 733
DFM Avatar asked Aug 04 '11 18:08

DFM


People also ask

How do you add a string value to a string map?

Maps are associative containers that store elements in a specific order. It stores elements in a combination of key values and mapped values. To insert the data in the map insert() function in the map is used.

What is string string map?

The first part to read is Map<String, String> This means "an instance of Map whose keys are String s and whose values are String s." For example, this might associate peoples' names with their driver's license numbers. We then use these as elements in a List<Map<String, String>>

How mapping is done in hibernate?

Define Hibernate Mapping FileThe <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.

How can you implement or mapping in hibernate give an example?

One To Many Mapping in Hibernate. In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table. For example, think of a Cart system where we have another table for Items. A cart can have multiple items, so here we have one to many mapping.


1 Answers

Chapter 2.2.5.3.4 of Hibernate Annotations documentation describes the necessary annotations. You need to do something like:

@Entity
public class MyEntity {
    ...

    @ElementCollection // this is a collection of primitives
    @MapKeyColumn(name="key") // column name for map "key"
    @Column(name="value") // column name for map "value"
    public Map<String,String> getMyMap() {

    ...
}
like image 144
ChssPly76 Avatar answered Oct 05 '22 23:10

ChssPly76