Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » persisting a hashmap
persisting a hashmap [message #753529] Thu, 27 October 2011 15:08 Go to next message
Sebastian Busch is currently offline Sebastian BuschFriend
Messages: 1
Registered: July 2009
Junior Member
Hi

I have problems persisting a simple Hashmap with eclipselink.
I try to generate tables from my entitys but the table generation chrashes with some exception. I wrote some simple classes, created persistence.xml with eclipese Dali tools, added class by class and creted the Tables from the Entitys . Everthing works fine until i add an entity containing hashmaps

To demonstrate my problem i wrote a small sample code.

Class Bar
package com.jpatest;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Entity;
import org.eclipse.persistence.annotations.ReadOnly;

@Entity

public class Bar {

@Id
@GeneratedValue
private int id;
private String mystring;
private int myint;
public String getMystring() {
return mystring;
}
public void setMystring(String mystring) {
this.mystring = mystring;
}
public int getMyint() {
return myint;
}
public void setMyint(int myint) {
this.myint = myint;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}


class Foo

package com.jpatest;

import java.util.HashMap;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Inheritance;
import javax.persistence.MapKeyColumn;

import static javax.persistence.InheritanceType.JOINED;
import javax.persistence.OneToMany;
import static javax.persistence.FetchType.EAGER;
import javax.persistence.JoinColumn;
import javax.persistence.SequenceGenerator;
import org.eclipse.persistence.annotations.ReadOnly;

@Entity

public class Foo {
@Id
@GeneratedValue
private int id;
private String mystring;
private int myint;
@OneToMany(fetch = EAGER, targetEntity = com.jpatest.Bar.class)
@MapKeyColumn(name="barid")
private HashMap<String, Bar> mymap;
public String getMystring() {
return mystring;
}
public void setMystring(String mystring) {
this.mystring = mystring;
}
public int getMyint() {
return myint;
}
public void setMyint(int myint) {
this.myint = myint;
}
public HashMap<String, Bar> getMymap() {
return mymap;
}
public void setMymap(HashMap<String, Bar> mymap) {
this.mymap = mymap;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}



persistence.xml

<persistence-unit name="jpatest" transaction-type="RESOURCE_LOCAL">
<class>jpatest.User</class>
<class>jpatest.Adress</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpatest"/>
<property name="javax.persistence.jdbc.user" value="jpatest"/>
<property name="javax.persistence.jdbc.password" value="########"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>


Exceptions

Runtime Exceptions:
---------------------------------------------------------

javax.persistence.PersistenceException: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
---------------------------------------------------------

Exception [EclipseLink-163] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: This mapping's attribute class does not match the collection class. [class java.util.Hashtable] cannot be assigned to [class java.util.HashMap].
Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[mymap]
Descriptor: RelationalDescriptor(com.jpatest.Foo --> [DatabaseTable(FOO)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
Descriptor: RelationalDescriptor(com.jpatest.Foo --> [DatabaseTable(FOO)])

Runtime Exceptions:
---------------------------------------------------------

at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:501)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
at org.eclipse.jpt.eclipselink.core.ddlgen.Main.perform(Main.java:86)
at org.eclipse.jpt.eclipselink.core.ddlgen.Main.execute(Main.java:77)
at org.eclipse.jpt.eclipselink.core.ddlgen.Main.main(Main.java:64)
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
---------------------------------------------------------


Any suggestions ? Please help ;)

Enviroment: Eclipse Indigo, Eclipselink 2.3.0, Mysql Server 5.5, Windows Xp or MacOs Lion
Re: persisting a hashmap [message #753741 is a reply to message #753529] Fri, 28 October 2011 14:46 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
The JPA specification states that the collection type should be Map not the concrete HashMap type, and EclipseLink is using a HashTable as the map implementation. Please file a bug to have this supported, and change the collection type to Map in the mean time. You might also be able to change change the collection type used in the mapping and MappedKeyMapContainerPolicy using a customizer if you need to use the HashMap over a Map type.

Best Regards,
Chris
Re: persisting a hashmap [message #754048 is a reply to message #753529] Mon, 31 October 2011 15:15 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
You should define the variable as Map not HashMap. JPA requires that you use the interface so that LAZY and other features work.

However, EclipseLink should allow HashMap if using EAGER, so please log a bug for this. You could probably use a DescriptorCustomizer and set the collection class in the mapping to HashMap.
--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Previous Topic:persisting a hashmap
Next Topic:Failed to map sql result set when execute stored procedure returning cursor
Goto Forum:
  


Current Time: Fri Apr 19 18:39:50 GMT 2024

Powered by FUDForum. Page generated in 0.02976 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top