persisting a hashmap [message #753529] |
Thu, 27 October 2011 15:08 |
Sebastian Busch 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02976 seconds