Fix for Issue 72 - Bug - models.Role cannot be cast to java.util.Map [error] at com.avaje.ebeaninternal.server.persist.DefaultPersister.saveAssocManyDetails(DefaultPersister.java:877)

This commit is contained in:
Rob Bygrave
2014-01-23 00:50:09 +13:00
parent 87a948223c
commit ee96c4afa6
12 changed files with 169 additions and 22 deletions
@@ -0,0 +1,28 @@
package com.avaje.tests.model.map;
import javax.persistence.*;
@Entity
public class MpRole {
@Id
private Long id;
private Long organizationId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getOrganizationId() {
return organizationId;
}
public void setOrganizationId(Long organizationId) {
this.organizationId = organizationId;
}
}
@@ -0,0 +1,45 @@
package com.avaje.tests.model.map;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.*;
@Entity
public class MpUser {
@Id
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@MapKey(name = "id")
public Map<Long, MpRole> roles = new HashMap<Long, MpRole>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<Long, MpRole> getRoles() {
return roles;
}
public void setRoles(Map<Long, MpRole> roles) {
this.roles = roles;
}
}
@@ -0,0 +1,42 @@
package com.avaje.tests.query.other;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.tests.model.map.MpRole;
import com.avaje.tests.model.map.MpUser;
public class TestOneToManyAsMap extends BaseTestCase {
@Test
public void test() {
EbeanServer eServer = Ebean.getServer(null);
MpUser u = new MpUser();
eServer.save(u);
MpUser u2 = eServer.find(MpUser.class, u.getId());
Assert.assertNotNull(u2);
u2.setName("Charlie Brown");
MpRole ourl = new MpRole();
ourl.setOrganizationId(47L);
u2.getRoles().put(ourl.getOrganizationId(), ourl);
eServer.save(u2);
MpUser u3 = eServer.find(MpUser.class, u.getId());
Assert.assertEquals("Charlie Brown", u3.getName());
Map<Long, MpRole> listMap = u3.getRoles();
Assert.assertEquals(1, listMap.size());
}
}