0

So I have this structure.

Table1 id field1 field2 discriminatorValue

Table2 id field3

Table3 id field4

And I want to use inheritance as follows.

@DiscriminatorColumn("discriminatorValue")
@Entity
@Inheritance
@Table("Table1")
public class T1  {

@Id
private int id;
...

private String field1;
..
private String field2;
..
}

@SecondaryTable(name = "Table2", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "id",     referencedColumnName = "id")})
@DiscriminatorValue("tbl2")
@Entity
public class T2 extends T1 {

private String field3;
..
}

@SecondaryTable(name = "Table3", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "id",     referencedColumnName = "id")})
@DiscriminatorValue("tbl3")
@Entity
public class T3 extends T2 {

private String field4;
..
}

What I'm finding is that when I try to save a T3 object the secondary table from T2 doesn't get included.

Thanks

wrschneider
  • 17,913
  • 16
  • 96
  • 176
BadgerB
  • 306
  • 3
  • 7
  • Does it work for T2? And does T3 save T1 and just not T2? – wrschneider Dec 01 '11 at 01:54
  • Yep except that the since T2 doesn't exist, the foreign key linkage between the id fields on T2 and T3 fails. So the generated sql looks something like this. insert into T1(...) insert into T3(...) and there's no T2 insert generated. – BadgerB Dec 01 '11 at 20:15

3 Answers3

1

It looks like you're using the SINGLE_TABLE inheritance strategy but explicitly splitting off some properties in the subclasses into separate tables, rather than using the joined-subclass strategy.

This seems to be the standard approach to mixing inheritance strategies

See: How to mix inheritance strategies with JPA annotations and Hibernate?

Mapping multi-Level inheritance in Hibernate with Annotations

The one thing you might be missing is specifying which fields go to which columns. @SecondaryTable is not tied to the inheritance hierarchy - you can use it in a single class - and therefore you have to explicitly specify that a field in the child class is mapped to the secondary table.

Thus in T2 you probably need

@Column(table="tbl2")
private String field3;

and in T3

@Column(table="tbl3")
private String field4;

Good luck!

Community
  • 1
  • 1
wrschneider
  • 17,913
  • 16
  • 96
  • 176
  • Thanks, I am basically using the single table, using secondary tables from the How to mix Inheritance article. I have @Column annotations on the relevant fields too. I am actually doing this with only one level of SecondaryTables in another part of the app and it works great, it's just when you have 2 SecondaryTables that the middle one disappears. Which actually kind of makes sense as I mention it now, since the annotation for SecondaryTable is probably getting overwritten by the T3 class... Problem is I'm not sure how to resolve the issue. – BadgerB Dec 01 '11 at 20:19
  • Oh and reason I'm doing it this way is there are a bunch of different classes subclassed from the main T1 class. Actual classes are Control - contains a bunch of data about controls Panel - contains a layout style field. Page - contains a page number. ... etc.  
    I may just drop the page table and put the page info into the panel table, it's a little less clear from the DB that way though since it looks like a panel has a page number on it.
    – BadgerB Dec 01 '11 at 20:23
0

Figured it out. If a secondary table has no data in any of the fields, then the row isn't inserted.

So in my case, field3 had no data so T2 didn't get inserted, and since the secondarytable in T3 was linked to the row in T2 the whole thing fell apart.

Oh and one other thing I'm finding is that you can't set a default value on a field in the middle table to try to force it to be created. I.E. doing either

private String field2 = "test";

or

public T2(){
  setField2("test");
}

doesn't work.

Thanks

BadgerB
  • 306
  • 3
  • 7
-2
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Project {
  @Id
  private long id;
}


@Entity
@Table(name="LARGEPROJECT")
public class LargeProject extends Project {
  private BigDecimal budget;
}
  • 1
    Welcome to SO. Please explain what your code does and how it solved the problem. http://stackoverflow.com/help/how-to-answer – Aziz Feb 01 '16 at 21:11
  • if want had more tables with same variables like (user,admin).You can create one other abstract class to share this values. – Chris Antonopoulos Feb 02 '16 at 16:24