Skip to main content

Hibernate-Secondary Table Annotation..

When using Hibernate with annotation, SecondaryTable needs to be used carefully while using the Discriminator. We assume that there is a table called Inventory which is the parent table that has common elements including Id, createDate, modifiedDate, SKU#, and productType.
The other tables, which are children of the Inventory table, are TV_Inventory, DVD_inventory etc.. In this example, we consider TV_Inventory which inherits from Inventory (in terms of objects).

@Entity
@Table(name = "INVENTORY")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "PRODUCT_TYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
@SequenceGenerator(name = "InventorySequence", sequenceName = "INV_SEQ", allocationSize = 1)



public abstract class Inventory {
 // getters and setters with other annotations for the columns and relevant methods.
}
 
Consider the child object associated with the table TV_INVENTORY. The outline is shown below:
 
@Entity
@DiscriminatorValue("TV")

@SecondaryTable(name = "TV_INVENTORY")

public class TVInventory extends Inventory {
              private Location loc;      //LOCATION Table entity
           // Other relevant columns, getters setters and methods.
}


In using the above definition and annotations, we will get the following error (assuming we are using Oracle DB):

SELECT
<<<< columns from Entities >>>
from
TV_Inventory invento0_,
INVENTORY ainvento0_1_,
LOCATION inventoryl2_
where
invento0_.LOCATION_SEQ=inventoryl2_.LOCATION_SEQ(+)
and ainvento0_.INVENTORY_ID=ainvento0_1_.INVENTORY_ID
and ainvento0_.INVENTORY_ID=?

[10:55:46:218] org.hibernate.jdbc.AbstractBatcher DEBUG about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[10:55:46:218] org.hibernate.util.JDBCExceptionReporter DEBUG could not load an entity: [com.hibernate.inv.model.aInventory#365746]
[The generated hibernate QUERY......]
java.sql.SQLException: ORA-00942: table or view does not exist
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)


This can be fixed by NOT using SecondaryTable. So now the TVInventory class is re-defined as:

@Entity
@DiscriminatorValue("TV")
@Table(name="TV_INVENTORY")
public class TVInventory extends Inventory {

            private Location loc; //LOCATION Table entity

}

...and it works fine without errors. Now assume that for each TV in our inventory we have features of TV stored in another table, say TV_FEATURES. Then we can use the SecondaryTable annotation as follows:

@Entity

@DiscriminatorValue("TV")
@Table(name="TV_INVENTORY")
@SecondaryTable(name="TV_FEATURES")
public class TVInventory extends Inventory {
                   @OneToMany(fetch = FetchType.EAGER)
                  @JoinColumn(name="INVENTORY_ID")
                   private List tvFeatures;  
}
 
The TVFeature class is defined below:
 
@Entity
@Table(name="TV_FEATURES")
public class TVFeature {
       ///getters and setters for columns
}
 
In the above scenarios, we are not using any inheritance. Instead we are joining the tables together on the inventory_id and defining the relationship between the TVInventory and TVFeature objects. Therefore when using inheritance, SecondaryTable codes can get a little complex in Hibernate.

Comments

Unknown said…
Critical thinkers such as yourself are a refrshing respite from the self-indulgent drudgery of narcissistic bloggers that overun the internet.
Rajeev said…
Thank you for your comment. I do have another blog where my narcissism takes over... ;)

Thanks also for visiting the blog.

Popular posts from this blog

Databases - Basics

In the previous post ( Databases Introduction )  we looked at different types of databases and simple concepts of tables. In this section, we will look at some basics of databases- tables, inner joins and outer joins. The syntax of SQL used here is for Oracle. However, it is very similar in all other databases such as MySQL, MS SQLServer, Postgres, etc.. Tables We will create two tables by using the SQL statements below:   create table customer (cust_id number(4,0) not null, customer_name varchar2(60) not null); create table invoice (invoice_id number(4) not null, cust_id number(4) not null , invoice_total number(10,2) default 0.00, paid_ind char(1)); In the first SQL we create the table called Customer with the column names cust_id and customer_name. Observe that we do not want to store null values in each of the columns in the customer table. In the second SQL we create the table invoice, which has its columns and also the cust_id column from the customer ...

Databases - Introduction

Database In the information age (which started in 1940 or so when the US census used the first computer), a database is referred to as a silo of information which has data stored in a particular way. This makes stored data relevant to employees in order to make business decisions. Databases are a specific type of model. Models are any storage repository (server) which stores data whether it is a relational database, object-oriented database, flat or just files. In this article, we confine ourselves to relational databases like MySQL, MS SQLServer20xx, Oracle, Sybase, MS Access and HSQLDB. These are the most common relational databases currently in use. Non-Relational Databases Before we move onto relational databases we will first discuss non-relational ones. These demanded a lot of sort to find code by programmers in order to retrieve the data to the users. Consider the US phone book and the number 307-456-7892. In order to search the phone book to retrieve who is associated to ...