JPA/Hibernate inheritance with multiple levels and tables.

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

Answers


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!


Need Your Help

How do I make a webpart's settings configurable in Sharepoint?

c# .net asp.net sharepoint web-parts

I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise. I am publishing a portal template.

Eclipse plugin to open zip files

eclipse eclipse-plugin

I want to open my resource files which are in zip format, Is there any good eclipse plugin out there which allows to open and edit zip files?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.