11

I have an existing DB schema which I'm trying to ORM'ise with JPA/Hibernate annotations. The schema is designed to allow for clients to assoicate extra information with each row in a table. Rather than using a key-value pattern the existing solution determines the required number of dynamic columns at install time, and alters the table to add extra columns for the dynamic fields. In effect each table has a set of known/fixed columns and group of dynamic columns.

|table:X|
|id|known1|known2|dynamic1|dynamic2|..|dynamicx|

A parameter table indicates which tables have extra dynamic fields

|table:parameter|
|table|column|meta|
|x|dynamic1||
|x|dynamic2||
|x|dynamicx||
|y|dynamic15||

In JPA modelling this, it is easy to create a definition for the known fixed columns on each table.

@Entity
@Table(name="x")
public class X
{
    @Id
    private Long id = null;

    @Column(name="known1")
    private Long known1 = null;
}

I plan to model the parameter table as a class, and then allow each of my classes to have a list of parameters. I'm unsure how i should go about mapping this List object so that the details are written to the same table.

@Entity
@Table(name="x")
public class X
{
    // @Column(name="x",class="X") ??????????
    List<Parameter> dynamicColumns = null;
}

I believe i can use a custom naming strategy to correctly determine the column name for the dynamic field. Is this type of mapping even possible?

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
emeraldjava
  • 10,894
  • 26
  • 97
  • 170

2 Answers2

2

I think you will need to use a nativeQuery to get at this dynamic data using jpa.

AmanicA
  • 4,659
  • 1
  • 34
  • 49
  • +1: I think JPA was not designed for this kind of need, and if you manage to do it, it will be at a great cost (and dirty hacks). Just use native queries and `ResultTransformer`s whenever possible, that's all Hibernate can provide here. – Vincent Mar 04 '13 at 13:30
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Chaos Apr 25 '14 at 19:48
  • @Chaos This is not meant as a critique, in my humble opinion this is the best way to solve this problem i.e. querying a list of dynamic columns from a table. – AmanicA Apr 25 '14 at 23:43
0

In Hibernate this is possible,Please see the article below. However that is a old approach (2007) i myself am breaking head to implement it using JPA. not succeeded so far

http://www.infoq.com/articles/hibernate-custom-fields

Another email thread in this topic is

https://www.java.net//node/666078

Mohammed Rafeeq
  • 2,586
  • 25
  • 26