Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » persisting a hashmap(problems with persisting a hashmap)
icon5.gif  persisting a hashmap [message #753524] Thu, 27 October 2011 15:08 Go to next message
Sebastian Busch is currently offline Sebastian BuschFriend
Messages: 1
Registered: October 2011
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 Wink

Enviroment: Eclipse Indigo, Eclipselink 2.3.0, Mysql Server 5.5, Windows Xp or MacOs Lion

Re: persisting a hashmap [message #754046 is a reply to message #753524] Mon, 31 October 2011 15:15 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
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 : Wiki : Book : Blog : Twitter
Previous Topic:Cache Coordination issue
Next Topic:persisting a hashmap
Goto Forum:
  


Current Time: Thu Dec 18 12:41:02 GMT 2014

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

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