4

I am wondering if hibernate allowing use arrayList to represent multiple columns.

For example: I have a single table, called "tableA" with columns: tableA: |id|c1|c2|c3|c4|

I would like to create a java object use arrayList to map to columns c1,c2,c3,c4.

    @Entity
    @Table(name = "tableA")
    public class tableA{

        @Column(name = "id", unique = true, nullable = false)
        public int id;

        //QUESTION:  How do I annotate here to map columns c1,c2,c3,c4 to below arraylist ? ----
        public List<String> columns = new ArrayList<>();


        //I tried this, but it doesn't work :
        @Column(name = "c1", length=10)
        public String getC1(){
            return columns.get(0);
        }

        public String setC1(String c1){
            columns.add(c1);
        }

    }

How do I annotate here to map columns c1,c2,c3,c4 to below arraylist ?

Shawn
  • 121
  • 11

2 Answers2

2

It may seem that I do not answer your question... but... There is a feature like you are requesting:

  • but it is not supported by annotations - just xml mapping
  • but it is not about List<> - but Map non-generic

Firstly, why just xml? because it is a dynamic component not related to class, see:

In the Component class, there is an attribute called dynamic which allows for support of the dynamic-component property type in xml configuration. Annotations do not currently have support for dynamic-components...

Which leads us to what we are talking about:

9.5. Dynamic components <dynamic-component>

Let's summarize the doc:

You can also map a property of type Map:

<dynamic-component name="userAttributes">
    <property name="foo" column="FOO" type="string"/>
    <property name="bar" column="BAR" type="integer"/>
    <many-to-one name="baz" class="Baz" column="BAZ_ID"/>
</dynamic-component>

The semantics of a <dynamic-component> mapping are identical to <component>. The advantage of this kind of mapping is the ability to determine the actual properties of the bean at deployment time just by editing the mapping document. Runtime manipulation of the mapping document is also possible, using a DOM parser. You can also access, and change, Hibernate's configuration-time metamodel via the Configuration object.

As described in the documentation, this way we can work with some dictionary of keys representing columns and values representing the content.

Maybe, check also this (NHibernate but the essential meaning is the same)

Finally: I am using this feature a lot - for Additional fields - driven by business users. These do need just some metadata management and udpated xml mapping to work. Nothing else. And what's more important, because these are columns (not dynamic rows) we can order by them as well...

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • How does the dynamic-component feature behave when I have multiple nodes sitting behind a load balancer? I don't think Hibernate will take the responsibility to update the metadata on all nodes. So could this be a constraint while using this feature? – Taher Aug 11 '17 at 13:15
0

There is no such feature in Hibernate, you need to map individual fields of your entity to corresponding columns in your mapping table.

Chaitanya
  • 15,403
  • 35
  • 96
  • 137