0

I'm having some fields to be updated in hashmap and I'm trying to put it in the session.update(Object object). But Im getting exception error as Unknown entity: java.util.HashMap. Here is my code.

 private Map <String, Object> conditions=new HashMap <String, Object>();
    public void addCondition(String field, Object condition){
            conditions.put(field, condition);
        }
    public Object getFilter(String field){
            return conditions.get(field);
        }
    public Map<String,Object>getFilterMap(){        
            return conditions;
        }

To Update these fields:

 class_name.addCondition("loginName", "dadan_evil"); 
    class_name.addCondition("lastName", "Pirate");
    session.update(filter.getFilterMap());//I tried this, but not working

User.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="in.co.User" table="user">
      <meta attribute="class-description">
         This class contains the user detail. 
      </meta>
      <id name = "loginName" type="string" column="loginName">

      </id>
      <property name="passCode" column="passCode" type="string"/>
      <property name="firstName" column="firstName" type="string"/>
      <property name="lastName" column="lastName" type="string"/>
      <property name="alternateEMail" column="alternateEMail" type="string"/>
      <property name="mobile" column="mobile" type="string"/>
      <property name="sysAdmin" column="sysAdmin" type="boolean"/>
      <property name="deleted" column="deleted" type="boolean"/>
      <property name="authenticationCode" column="authenticationCode" type="string"/>
      <property name="authenticated" column="authenticated" type="boolean"/>

   </class>
</hibernate-mapping>

And my update method:

private static void updateFromQuery(DBFilter filter) throws Exception {
        Session session = factory.openSession();

        try{
            Class<?> className = filter.getCollectionClass();
            Criteria criteria = session.createCriteria(className);
            criteria.add(Restrictions.allEq(filter.getFilterMap()));
            //I dont know how to update this, just an imaginary parameter
            session.update(filter.getFilterMap());

        }catch(HibernateException e){
            e.printStackTrace();
        }finally{
            session.close();
        }
    }

And I can't write down a string query either because I have got multiple tables and different values. And I'm even getting the class Names dynamically. So I can't use the string query to be common for all. So, if I can then please tell me.

Now, how can I update this two values to the database. Please help

trying...
  • 309
  • 1
  • 11
  • You are trying to store a java.util.HashMap directly in your Database!? Are you sure you want to do this? – mwe Feb 09 '17 at 10:57
  • you can't pass hashmap to the update() method.... you should pass entity which is Annotated by @Entity annotation. Extract value from map and put them in entity class, then simply update – Nikesh Joshi Feb 09 '17 at 10:57
  • Have you already used Hibernate? Do you know that Hibernate is for persisting whole objects (with specification which attribute maps to which field) and not for searching the tablename and then stupidly creating sql-update-statements? – Niklas P Feb 09 '17 at 10:58
  • is there any way to update a single field.If yes, please give an example because that is my only intention for now. – trying... Feb 09 '17 at 11:00
  • first of all: Unknown entity: java.util.HashMap is being throw because you are trying to pass hashmap to update method which is then checking to what db table that entity is mapped to.... obviously it won't find anything because its a hashmap. to make your question more clear maybe add your database structure that you want to update and we may be able to suggest something – Zeromus Feb 09 '17 at 11:22
  • Possible duplicate of [hibernate update single column using criteria](http://stackoverflow.com/questions/26245881/hibernate-update-single-column-using-criteria) – Frederik Heremans Feb 09 '17 at 12:17

1 Answers1

0

I have done it by using java.lang.reflect.Field. So I have retrieved the particular object from the database and I will get the key and the values separated from Map and bind with the Field by making reference to the Class which I need. Now this works fine.Even if I get better answers than mine, its OK.

private static void setField(Class<?> class_name,Object object, String fieldName, Object value) throws Exception {
        Field field = class_name.getDeclaredField(fieldName);       
        field.setAccessible(true);
        field.set(object, value);       
    }

    private static void updateFromQuery(DBFilter filter) throws Exception {
        Session session = factory.openSession();

        try {
            Class<?> className = filter.getCollectionClass();
            Criteria criteria = session.createCriteria(className);
            criteria.add(Restrictions.allEq(filter.getFilterMap()));
            List objectList = criteria.list();
            Transaction tx;
            tx = session.beginTransaction();
            for (Object object : objectList) {
                for (Map.Entry<String, Object> entry : filter.getValueMap().entrySet()) {
                    setField(className,object, entry.getKey(), entry.getValue());
                }
                session.update(object);
            }
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

Thanks for everyone who supported.

trying...
  • 309
  • 1
  • 11