I can map java.util.Map in Server.java
Hibernate version:3.1.2
Mapping documents:
Code:
@Entity
@Table(name="ServerGroup")
public class Group {
private long id;
private String name;
private Collection<Server> servers;
@Id @GeneratedValue
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
public Collection<Server> getServers() {
return servers;
}
public void setServers(Collection<Server> servers) {
this.servers = servers;
}
}
Code:
@Entity
public class Server {
private long id;
private String name;
private String address;
private String user;
private String password;
private Map<String, SharedFolder> sharedFolders;
@Id @GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
...
...
...
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@MapKey(name="letter")
@JoinColumn(name="server_id")
public Map<String, SharedFolder> getSharedFolders() {
return sharedFolders;
}
public void setSharedFolders(Map<String, SharedFolder> sharedFolders) {
this.sharedFolders = sharedFolders;
}
}
Code:
@Entity
public class SharedFolder {
private long id;
private String relativePath;
private String letter;
@Id @GeneratedValue
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
public String getRelativePath(){
return relativePath;
}
public void setRelativePath(String path){
this.relativePath = path;
}
...
}
Code between sessionFactory.openSession() and session.close():Code:
Insert some values
Session se = HibernateUtil.getSessionFactory().getCurrentSession();
se.beginTransaction();
Group g = new Group();
g.setName("Group 1");
LinkedList<Server> list = new LinkedList<Server>();
g.setServers(list);
for (int i=0; i<10; i++){
Server s = new Server();
s.setName("Server "+i);
TreeMap<String, SharedFolder> map = new TreeMap<String, SharedFolder>();
s.setSharedFolders(map);
for (int k=0; k<10; k++){
SharedFolder folder = new SharedFolder();
folder.setRelativePath("\\share"+k);
s.getSharedFolders().put(" "+k,folder);
}
g.getServers().add(s);
}
se.save(g);
se.getTransaction().commit();
Read some values
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
List result = s.createQuery("from Group").list();
Group g = (Group)result.get(0);
logger.debug("Servers> "+ g.getServers().size());
Server server = g.getServers().iterator().next();
logger.debug(server.getName()+" SharedFolders>"+server.getSharedFolders().size());
}
rootNode.add(serverNode);
}
s.getTransaction().commit();
Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.HibernateException: null index column for collection: sk.netmap.bo.Server.sharedFolders
at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:652)
at org.hibernate.collection.PersistentMap.readFrom(PersistentMap.java:222)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:994)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:635)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1695)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
at org.hibernate.collection.PersistentMap.size(PersistentMap.java:95)
at sk.netmap.gui.TreeModel.createTree(TreeModel.java:43)
at sk.netmap.gui.MainFrame.buildMainPanel(MainFrame.java:381)
at sk.netmap.gui.MainFrame.buildContentPane(MainFrame.java:331)
at sk.netmap.gui.MainFrame.buildInterface(MainFrame.java:227)
at sk.netmap.gui.MainFrame.main(MainFrame.java:175)
Name and version of the database you are using:
HSQLDB 1.8.0