I'm trying to use an entity (SpeakerKey) as an index for a many to many association of speakers to events. (As outlined in Ch5 pgs 134 & 135 og 'Hibernate Quickly'). Below are the mappings and classes....
that produce this error:
ERROR BasicPropertyAccessor: - IllegalArgumentException in class: com.manning.hq.ch05.manytomany.EventManyToMany, setter method of property: speakers
2008-06-05 20:38:29,562 ERROR BasicPropertyAccessor: - expected type: java.util.HashMap, actual value: org.hibernate.collection.PersistentMap
if I did it correctly how could I insert an event with it's map of speakers?
If I'm out to lunch on this could someone give me an explanation on how this works (conceptionally)?
thx !
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">stella</property>
<property name="connection.url">jdbc:mysql://localhost/events_calendar</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/manning/hq/ch05/manytomany/EventManyToMany.hbm.xml"/>
<mapping resource="com/manning/hq/ch05/manytomany/SpeakerKey.hbm.xml"/>
<mapping resource="com/manning/hq/ch05/manytomany/SpeakerManyToMany.hbm.xml"/>
</session-factory>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.manytomany.EventManyToMany" table="m_events">
<id name="id" column="uid" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="100"/>
<property name="startDate" column="start_date"
type="date"/>
<property name="duration" type="integer"/>
<map name="speakers" table="event_speakers" cascade="all">
<key column="event_id"/>
<map-key-many-to-many column="speaker_key_id" class="com.manytomany.SpeakerKey" />
<many-to-many column="speaker_id" class="com.manytomany.SpeakerManyToMany" />
</map>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.manytomany.SpeakerManyToMany" table="m_speakers">
<id name="id" column="uid" type="long">
<generator class="native"/>
</id>
<property name="firstName" type="string" length="20"/>
<property name="lastName" type="string" length="20"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.manytomany.SpeakerKey" table="speaker_keys">
<id name="id" column="uid" type="long">
<generator class="native"/>
</id>
<property name="value" type="string" length="20"/>
</class>
</hibernate-mapping>
package com.manytomany;
import java.util.Date;
import java.util.Set;
import java.util.HashMap;
public class EventManyToMany {
private Long id;
private String name;
private Date startDate;
private int duration;
private HashMap speakers;
private Set attendees;
private LocationManyToMany location;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public LocationManyToMany getLocation() {
return location;
}
public void setLocation(LocationManyToMany location) {
this.location = location;
}
public void setSpeakers(HashMap speakers) {
this.speakers = speakers;
}
public HashMap getSpeakers() {
return speakers;
}
public Set getAttendees() {
return attendees;
}
public void setAttendees(Set attendees) {
this.attendees = attendees;
}
}
package com.manning.hq.ch05.manytomany;
public class SpeakerKey {
private Long id;
private String value;
//private String lastName;
//private Set events;
public SpeakerKey() {
}
public SpeakerKey(String value) {
setValue(value);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value=value;
}
}
package com.manytomany;
import java.util.HashSet;
import java.util.Set;
public class SpeakerManyToMany {
private Long id;
private String firstName;
private String lastName;
private Set events;
public SpeakerManyToMany() {
}
public SpeakerManyToMany(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
public SpeakerManyToMany(String firstName, String lastName, EventManyToMany event) {
this(firstName, lastName);
addEvent(event);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set getEvents() {
return this.events;
}
public void setEvents(Set events) {
this.events = events;
}
private void addEvent(EventManyToMany event) {
if (events == null) {
events = new HashSet();
}
events.add(event);
}
}
package com.manytomany;
import java.util.Calendar;
import java.util.Date;
public class EventLoader{
public static void main(String[] args) {
try {
EventTest test = new EventTest("tester");
test.testSave();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
}
package com.manning.hq.ch05.manytomany;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.HashSet;
import java.util.HashMap;
public class EventTest extends TestCase {
private SessionFactory factory;
public EventTest(String name) throws Exception {
super(name);
factory = new Configuration().configure().buildSessionFactory();
}
public void testSave() throws Exception {
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
EventManyToMany event = new EventManyToMany();
event.setName("Many test");
HashMap spkMap = new HashMap();
SpeakerKey spKey = new SpeakerKey("sp1");
session.save(spKey);
spkMap.put(spKey.getId(), new SpeakerManyToMany("Donna", "Busby"));
System.out.println("id="+spKey.getId());
event.setSpeakers(spkMap);
//event.getSpeakers().put("sp1", new SpeakerManyToMany("Donna", "Busby"));
//event.getSpeakers().put("sp1", new SpeakerManyToMany("Donna", "Busby"));
//event.getSpeakers().put("sp2", new SpeakerKey("Dave Smith"));
//event.getSpeakers().put("sp3", new SpeakerKey("Joan Smith"));
session.saveOrUpdate(event);
session.flush();
trans.commit();
session.close();
}
}
|