-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Accessing java.util.HashMap
PostPosted: Mon Mar 09, 2009 4:28 am 
Newbie

Joined: Mon Feb 18, 2008 5:45 am
Posts: 5
Hibernate version: 3.2.5-ga

Name and version of the database you are using: MySql 5.0.45

I'm using the following model:

Code:
package com.dummy.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.MapKey;

/**
* Dummy Model
*/
@Entity
@Table(name = "dummytable")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class MapTest implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long metaId;
    @Column(nullable = true)
    private String exampleKey;

    // more attributes here
       
    @CollectionOfElements
    @JoinTable(name = "criteriamap", joinColumns = @JoinColumn(name = "item_id"))
    @MapKey(columns = @Column(name = "key_criterion"))
    @Column(name = "value_criterion")
    private Map<String, String> criteriaMap = new HashMap<String, String>();

    public Map<String, String> getCriteriaMap() {
        return criteriaMap;
    }

    public void setCriteriaMap(Map<String, String> criteriaMap) {
        this.criteriaMap = criteriaMap;
    }

    public Long getMetaId() {
        return metaId;
    }

    public void setMetaId(Long metaId) {
        this.metaId = metaId;
    }

    public String getExampleKey() {
        return exampleKey;
    }

    public void setExampleKey(String exampleKey) {
        this.exampleKey = exampleKey;
    }
   
    // more getters and setters here
}


The criteriaMap is used to store user defined data which is entered by the end-user in a GUI.
To access this data i want to use HQL as i do with all other fields:

Code:
private Map<String, Object> buildSearchCriteria(final MapTest mapTest, final StringBuilder query) {
// ...
        if (mapTest.getExampleKey() != null) {
            query.append(getOperator(opCount)).append(" objectAlias.exampleKey = :exampleKey");
            opCount++;
            paramMap.put("exampleKey", mapTest.getExampleKey());
        }
// ...
}
private String getOperator(final int opCount) {
        if (opCount > 0) {
            return " AND";
        } else {
            return " WHERE";
        }
}

This works fine.

My idea to access the HashMap is as follows:

Code:
private Map<String, Object> buildSearchCriteria(final MapTest mapTest, final StringBuilder query) {
// ...
        if (mapTest.getCriteriaMap() != null && mapTest.getCriteriaMap().size() > 0) {
            int i=0;
            for (String key : mapTest.getCriteriaMap().keySet()) {
                query.append(getOperator(opCount)).append(" key_criterion= :keyName"+i).append(" AND value_criterion = :valueName"+i);
                opCount++;
                paramMap.put("keyName"+i, key);
                paramMap.put("valueName"+i, mapTest.getCriteriaMap().get(key));
                i++;
            }
        }
// ...
}


This doesn't work, because Hibernate doesn't know the key_criterion and the value_criterion columns defined by the model.
I tried to qualify both columns by objectAlias.* or by objectAlias.criteriaMap.*, both attempts didn't work either.

Scanning the HashMap in Java code is not an option, because the database is to big to load to memory and speed is very important.
So, I'm looking for a solution based on HQL.

Thank you for your help.


Top
 Profile  
 
 Post subject: Interesting problem
PostPosted: Fri Mar 13, 2009 7:49 am 
Newbie

Joined: Mon Mar 09, 2009 5:04 am
Posts: 2
Hi Oski,
I'm working on almost the same issue as you. I think it's strange, that Hibernate doesn't offer support for this kind of access to map. It's so easy to declare them inside a Java class and hibernate does there right thing in creating a separte table containing the right values. But it's not able to use this map for a search ...
Did you find a workaround for it?
Regards,
Claus


Top
 Profile  
 
 Post subject: My workaround is a for-loop...
PostPosted: Wed Mar 18, 2009 9:00 am 
Newbie

Joined: Mon Feb 18, 2008 5:45 am
Posts: 5
Hi Claus

My workaround for this problem is a for-loop in which the criterias are to be compared. Its really a disappointing solution...

Has anybody an idea?

Thanks, Rene


Top
 Profile  
 
 Post subject: no idea?
PostPosted: Fri Mar 27, 2009 3:31 am 
Newbie

Joined: Mon Feb 18, 2008 5:45 am
Posts: 5
Really does nobody have an idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 27, 2009 4:16 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There are some examples in the Hibernate documentation that indicates that you should be able to use a syntax similar to:

Code:
objectAlias.criteriaMap[:key] = :value


I don't know if it works with a parameter as the key (the examples are all hard-coded strings). For (some) more information read http://www.hibernate.org/hib_docs/v3/re ... sions.html


Top
 Profile  
 
 Post subject: Promising reply
PostPosted: Tue Mar 31, 2009 9:22 am 
Newbie

Joined: Mon Mar 09, 2009 5:04 am
Posts: 2
Thanks for this reply! This looks very promising...
I'll try it tomorrow.
Regards,
Claus


Top
 Profile  
 
 Post subject: Re: Accessing java.util.HashMap
PostPosted: Fri May 08, 2009 8:19 am 
Newbie

Joined: Mon Feb 18, 2008 5:45 am
Posts: 5
Hi nordborg
sorry for my late reply, i was busy by an emergancy customer problem...
I tried your proposal. My query was as follows:

"FROM FileStoreMetaImpl fsm WHERE fsm.criteriaMap[:key0] = :valueName0"

The values "key0" and "valueName0" are both filled with correct values and the HashMap exists on the MySql-table.

The result is always a DataNotFound... Have you an idea?

Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.