Mapeo de JPA


¿Cómo puedo mapear un mapa en JPA sin usar las clases de Hibernate?

Author: hello_there_andy, 2009-05-12

4 answers

¿No funciona lo siguiente para usted?

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

EntityType podría ser cualquier tipo de entidad, incluyendo un String.

 15
Author: Chris K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-16 11:55:04

Aunque la respuesta dada por Subhendu Mahanta es correcta. Pero @CollectionOfElements está en desuso. Puedes usar @ElementCollection en su lugar:

@ElementCollection
@JoinTable(name="ATTRIBUTE_VALUE_RANGE", joinColumns=@JoinColumn(name="ID"))
@MapKeyColumn (name="RANGE_ID")
@Column(name="VALUE")
private Map<String, String> attributeValueRange = new HashMap<String, String>();

No es necesario crear una clase de entidad separada para el campo Map. Se hará automáticamente.

 17
Author: Jatin Sehgal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-31 07:26:34

Supongamos que tengo una entidad llamada Book que tiene un Mapa de capítulos:

import java.io.Serializable;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;    
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.MapKey;
@Entity
public class Book implements Serializable{
@Column(name="BOOK_ID")
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long bookId;    

@CollectionOfElements(targetElement=java.lang.String.class)
@JoinTable(name="BOOK_CHAPTER",
        joinColumns=@JoinColumn(name="BOOK_ID"))
@MapKey (columns=@Column(name="CHAPTER_KEY"))
@Column(name="CHAPTER")
private Map<String,String> chapters;
public Long getBookId() {
    return bookId;
}
public void setBookId(Long bookId) {
    this.bookId = bookId;
}
public Map<String,String> getChapters() {
    return chapters;
}
public void setChapters(Map<String,String> chapters) {
    this.chapters = chapters;
}               

}

Funciona para mí.

 10
Author: Subhendu Mahanta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-02 09:45:50

Un ejemplo de trabajo:

@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name = "TABLENAME")
@MapKeyColumn(name = "KEY")
@Column(name = "VALUE")
public Map<String, String> getMap() {
    return _map;
}
 1
Author: Taioli Francesco,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-26 12:48:26