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?

2
  • 1
    I hope you are able to change the database... this sounds like a schema that needs some normalization. Commented Sep 7, 2010 at 10:29
  • for this question let's assume that i can't. i think i'm close to hitting the brick wall. Commented Sep 7, 2010 at 10:49

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

+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 ResultTransformers whenever possible, that's all Hibernate can provide here.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@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.
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

1 Comment

Link-only answers are strongly discouraged due to links eventually disappearing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.