Persistence for Hashmap<Integer,Object>

389 Views Asked by At

I'm getting these errors trying to mapping a Hashmap i dont know how to solve this. Netbeans 8.1 Jpa 2.1

Mapping

@Entity
@Table(name = "libros")
public class Libro implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idlibro", nullable = false)
private int idlibro;

@Column(name = "isbn", nullable = false, length = 50, unique = true)
private long isbn;

@Column(name = "titulo", nullable = false, length = 200, unique = false)
private String titulo;

@Column(name = "anio", nullable = true, length = 4, unique = false)
private int anio;

@Column(name = "idioma", nullable = true, length = 50, unique = false)
private String idioma;

@JoinColumn(name = "idautor", referencedColumnName = "idautor", insertable=false, updatable=true)
@ManyToOne(optional = false)
private Autor elautor;

@Column(name = "genero", nullable = true, length = 50, unique = false)
private String genero;

@JoinColumn(name = "ideditorial", referencedColumnName = "ideditorial", insertable=false, updatable=true)
@ManyToOne(optional = false)
private Editorial laeditorial;

@ElementCollection
@MapKeyColumn(name="nrocopia")
@Column(name="ejemplares")
@CollectionTable(name="ejemplares", joinColumns=@JoinColumn(name="idejemplar"))
private HashMap<Integer, Ejemplar> ejemplarlibro;


@Column(name = "estado", nullable = true, length = 50, unique = false)
private String estado;

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException Exception Description: Deployment of PersistenceUnit [BibliotecaTPFINALPU] failed. Close all factories for this PersistenceUnit. Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException

Descriptor Exceptions:

Exception [EclipseLink-138] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException Exception Description: The attribute [ejemplarlibro] is of type [class java.util.HashMap] but the mapping uses transparent indirection (lazy loading), requiring it to be a superclass of [IndirectMap]. Mapping: org.eclipse.persistence.mappings.DirectMapMapping[ejemplarlibro] Descriptor: RelationalDescriptor(Clases.Libro --> [DatabaseTable(libros)])

Runtime Exceptions:

at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:182)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:527)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:140)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at Jpa.ControladorPersistencia.getEmf(ControladorPersistencia.java:20)
at Controladores.BibliotecaControlador.<init>(BibliotecaControlador.java:54)
at bibliotecatpfinal.BibliotecaMain.main(BibliotecaMain.java:46)

Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException Exception Description: Deployment of PersistenceUnit [BibliotecaTPFINALPU] failed. Close all factories for this PersistenceUnit. Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException Descriptor Exceptions:

1

There are 1 best solutions below

0
Mark W On

As @Neil Stockton says in his comment. The solution is to change your declaration of HashMap to the interface Map

private HashMap<Integer, Ejemplar> ejemplarlibro;

becomes

private Map<Integer, Ejemplar> ejemplarlibro;