Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA/Hibernate mapping dynamic columns as a List of objects

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?

like image 611
emeraldjava Avatar asked Sep 07 '10 10:09

emeraldjava


1 Answers

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

like image 120
AmanicA Avatar answered Nov 16 '22 02:11

AmanicA