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?
I think you will need to use a nativeQuery to get at this dynamic data using jpa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With