0

I am trying to create Student and Address objects from a table called STUDENT_ADDRESS that is defined as below.

Table Name: STUDENT_ADDRESS
Columns:
STUDENT_ID
STUDENT_NAME
STUDENT_END_DATE
STUDENT_ADDRESS_END_DATE
ADDRESS_ID
ADDRESS_VALUE
ADDRESS_END_DATE

I am aware that this is the most inefficient table that can ever be created. But changing the table structure is an uphill battle that I will most likely lose.

What I am trying to do is to create a Student object that has a set of Address objects, and the address objects can be filtered out based on both the STUDENT_ADDRESS_END_DATE and ADDRESS_END_DATE.

I hope that made sense. Thanks in advance for your help. :)

Mahmut Ali ÖZKURAN
  • 1,120
  • 2
  • 23
  • 28
  • 1
    *Please* don't do this. Fight whatever fight you have to and get the table properly de-normalized. Storing multiple records in the table just to store different addresses is bad design and will be a ***nightmare*** to maintain. – Perception Mar 11 '13 at 22:49

1 Answers1

0

You can look this up in Hibernate recipes under "disasters waiting to happen" :)

However if you really must then maybe try it with discriminators?

I'm not sure if this will do a one to many relationship though.

@Entity  
@Table( name =  "OBJECT")  
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)  
@DiscriminatorColumn(   
    name="OBJ_TYPE",  
    discriminatorType=DiscriminatorType.STRING,  
    length=1  
)  
@DiscriminatorValue("c")  

public class ObjOne {  
...  
}

@DiscriminatorValue("b")
public class ObjTwo {
....
}

Be vary wary of creating a n + 1 problem (see What is SELECT N+1?) when building a hierarchy within a single table, as this will ruin your day, which was probably going well after you got the initial issue sorted...

Community
  • 1
  • 1
stringy05
  • 6,511
  • 32
  • 38