0

Having the following kind of table tables. What will be the good approach to persist these tables? Used inheritance strategy for this, but it didn't work as expected.

Requirement 1: Need to persist student table, it will persist the member as well as address table as well

Requirement 2: Need to persist teacher table, it will persist the member as well as address table as well

Need to perform get, update and delete option on these tables.

Member {
  member_id - have one to one relation with student id and teacher id
  lastupdateddate
  latupdatedby
}

Student {
  student id - have one to one relation with member id
  student name
  lastupdateddate
  latupdatedby
}

teacher {
  teacher id - have one to one relation ship with member
  teacher name
  lastupdateddate
  latupdatedby
}

address {
  address id
  member_id - have one to one relationship with member class
  lastupdateddate
  latupdatedby
}

When I persist/update student details, the address related info is not properly inserted or updated.When I check insert queries fired on member, then student table after on address table. But, in the insert query to address table, the member_id value is coming as null.Because of this only address table is not populated.

Entity structure is is as given below

public abstract class Member implements Serializable {
}
public class Student extends member implements Serializable {
}
public class Teacher extends member implements Serializable {
}
public class Address implements Serializable {
}

The mapping is mentioned as given below. Tried out various available options.

In member entity class
       @Access(AccessType.PROPERTY)
       @OneToOne(mappedBy="member", cascade=CascadeType.ALL)
       public Address getAddress() {
              return postalAddress;
       }

In  address entity class
       @OneToOne(fetch=FetchType.LAZY)
       @JoinColumn(name = "MEMBER_ID")
       private Address address;
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

It looks like you have 2 options:

  1. InheritanceType.JOINED. It's almost what you've described: common part is in one table, different parts are in different tables. On each request JOIN will occure
  2. InheritanceType.SINGLE_TABLE. Here all the data will be stored in single table and descriminator will be used to determine if record if for student or for teacher.

Personally I would prefer second options because you have almost all fields in common, also most of operations are lighter and involve WHERE, not JOIN.

asm0dey
  • 2,841
  • 2
  • 20
  • 33
  • Thank you for your details. What about address table? If am saving or persisting the content of student table, does it presist address table as well? How can this be handled? – user1866342 Apr 23 '18 at 07:51
  • @user1866342 it looks like both types of members may have address, so it should be in superclass – asm0dey Apr 23 '18 at 08:58
  • Yes..In super class only, it is there. But while persisting student class, the address related details are not populated correctly, I am getting member_id as null. When I checked the quires, I found that insertion query is executed on member as well as student class. – user1866342 Apr 25 '18 at 08:19
  • @user1866342 I think you should clarify your question. – asm0dey Apr 25 '18 at 10:13