0

I have been using Hibernate for a while and this time I am trying to do something uncoventional here. Not even sure of its possible.

What I want to do is to load data from a Single table, where in few of this columns will have fields defined for them for rest (almost around 20 and can increase with time) all integers, I want to load them in a Map. Such that the name of the Column become the key and and the data its value.

I am using Hibernate 4.1.4 and using Annotations to map Fields with Columns.

As further clarification: Table Definition :

CREATE TABLE TempTable
(
 id SERIAL,
 revId TEXT,
 type INTEGER,
 group INTEGER,

 col_1 INTEGER,
 col_2 INTEGER,
 col_3 INTEGER,
 col_4 INTEGER,
 col_5 INTEGER,
 col_6 INTEGER,
 col_7 INTEGER,
 col_8 INTEGER,
 col_9 INTEGER
}

The DAO Model would look something like

@Entity
@Table(name = "TempTable")
public class StatsModel {
    private Long entryId;
    private String revId;

    private Integer type;
    private Integer group;

    private Map<String, Integer> metrics; // Map in which I want columns col_1 to col_9 as "Column Name" Vs. "Value"

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @Generated(GenerationTime.INSERT)
    public Long getEntryId() {
        return entryId;
    }

    public void setEntryId(Long entryId) {
        this.entryId = entryId;
    }

    @Column(name = "tx_rev")
    public String getRevId() {
        return revId;
    }

    public void setRevId(String revId) {
        this.revId= revId;
    }

    @Column(name = "nu_type")
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    @Column(name = "nu_group")
    public Integer getGroup() {
        return group;
    }

    public void setGroup(Integer group) {
        this.group = group;
    }
}
Salman A. Kagzi
  • 3,833
  • 13
  • 45
  • 64
  • You mean a naming strategy? or you want to load not annotated fields? – Ziul May 13 '13 at 17:40
  • @Ziul I am sorry, I didn't understand that. I am not sure if there are any annotations which can be used to load data from a table directly in a Map. Also, this is a single table I am trying to read from. There are no JOIN or anything here, Just 1 table and 1 DAO model for it. – Salman A. Kagzi May 13 '13 at 19:14
  • @Ziul I have updated the question text further to add Table Schema from which I am trying to read data from. I hope this explains my question better. – Salman A. Kagzi May 13 '13 at 19:20

2 Answers2

0

could use a view of the remaining columns to "pivot" the columns. Then map this view, e.g. a view created like this (note even the DDL for the view could be dynamically created by reading the data dictionary of your db, but that is another question!)

create or replace view TempTable_metrics as
select t.id,
       pv.name as key,
       case
          when pv.name = 'COL1' then t.col1
          when pv.name = 'COL2' then t.col2
          when pv.name = 'COL3' then t.col3
       end as value
from   temp_table t,
       (
       select 'COL1' as name from dual union all
       select 'COL2' as name from dual union all
       select 'COL3' as name from dual
       ) pv

Edited: (adding how to do the hibernate part) Forgot to mention, on the java side you need these annotations by your map:

@CollectionTable(name="TempTable_metrics", joinColumns=@JoinColumn(name="id"))
@MapKeyColumn(name="key", nullable=false)
@Column(name="value")
Map<String,Integer> metrics;

Just realized though that in order to do updates you'd need some messy "instead of" triggers in your DB (I've done this with Oracle but elsewhere YMMV).

dmansfield
  • 1,108
  • 10
  • 22
0

You can load your data as a map changing your query, this is the hibernate doc example:

select 
    new map(
        max(bodyWeight) as max,
        min(bodyWeight) as min, count(*) as n 
    ) 
from Cat cat

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select

Ziul
  • 883
  • 1
  • 13
  • 24