toman
toman

Reputation: 308

How to map individual columns into a collection using JPA

I have a legacy database, the design of which I can not change. I'm trying to implement a DAO layer to access it using JPA 2.0 (Hibernate). I am however running into problems as the layout is just .. horrible. I could switch to a Spring-JDBC-based solution, but I would not want to give up on the original idea just yet.

Here's an issue that I'm struggling with right now:

Table A:

 Id    Number  SomeProperty
---  --------  ------------
 1        100             X
 2        200             Y
 3        999             Z
...

Table B:

Id     A1    A2    A3    A4    A5    A6
---  ----  ----  ----  ----  ----  ----
 1    100   200   
 2    200
 3    999     
 4    100   200   999
...

So each B can contain up to 6 A's which are referred to by their Number. I would like to map this layout to a model something like:

@Entity
class A {
    String someProperty;
}

@Entity
Class B {
    List<A> listOfAs; // 
}

When I would query for B's I would, with the example data, end up having 4 instances of B:

Is there any nice to way do this? I would definately not want to have class B containing A1 a1, A2 a2, A3 a3 and so on.

Do you think it would be better to just forget about JPA when using a legacy DB like this? Any suggestions are welcome. Thanks!

Upvotes: 0

Views: 111

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

I would create views on top of these tables that fix the problems with the data model. You could then use the improved model to build your JPA entities from.

Upvotes: 1

Related Questions