aymeba
aymeba

Reputation: 934

Hibernate, save the datas from a Map

i want to save the datas on hibernate dynamically from a Map(like HashMap).

String entityname = "table1";
Map<String,Object> myMap;

session.save(entityname, myMap);

My HashMap has informations like : {videoResolution=921600}

but i get following error :

org.hibernate.MappingException: Unknown entity: table1

My Hibernate configuration file configured default-entity-mode :

    <property name="default_entity_mode">dynamic-map</property>

May you help me?

i've founded :

Session session = HibernateUtil.getSessionFactory().openSession(); 
Session ds = session.getSession(EntityMode.MAP); 

to save a map to the db vai Hibernate but i get always same error...

Thanks :)

Upvotes: 2

Views: 3320

Answers (2)

duffymo
duffymo

Reputation: 308733

Hibernate without objects or mapping? I'd say you don't want Hibernate - try iBatis or straight JDBC instead. What's Hibernate buying you here? Nothing.

If you have a Map of (key, value) pairs that you want to store in a table, do it like this:

public interface TableMapper<K extends Serializable, V>
{
   Map<K, V> find();
   V find(K key);
   void save(Map<K, V> m);
   void update(Map<K, V> m);
   void delete(K key);
}

Implement this interface in Hibernate or iBatis or JDBC or JDO or JPA. It'll let you do CRUD operations using a Java Map with a backing data store.

Upvotes: 0

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

Erm, Hibernate is an Object-Relational Mapping framework. Consequently, you need both an Object (containing the data to be persisted) and a Mapping (to describe how the object's attributes should be put into certain database tables to use it).

There's no way to avoid this fundamental requirement, just like you can't run a Java application without a Main class no matter how much you might want to.

The Hibernate documentation is very comprehensive, so I recommend looking at Getting Started guide and following it through. You'll notice as well from the API that session.save requires a persistent class for an argument (i.e. one with a known mapping), which is unlikely to be the case for java.util.Map.

Upvotes: 2

Related Questions