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).

@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:

@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):

<<<< columns from Entities >>>
TV_Inventory invento0_,
INVENTORY ainvento0_1_,
LOCATION inventoryl2_
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(
at oracle.jdbc.driver.T4CTTIoer.processError(
at oracle.jdbc.driver.T4CTTIoer.processError(
at oracle.jdbc.driver.T4C8Oall.receive(

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

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:


public class TVInventory extends Inventory {
                   @OneToMany(fetch = FetchType.EAGER)
                   private List tvFeatures;  
The TVFeature class is defined below:
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.


fair 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

4 Circles releases Word Frequency..

4 Circles released its application 'Word Frequency v1.1'.

This application counts the number of words in your essay and tells you the frequency of each word. This application is simple to use and helpful for bloggers, writers, journalists, students and speech writers. The application has 1 box for the text to be copied and pasted (left hand side) while clicking the button will count the total number of words in the essay.

This application is written in JDK 1.6, and runs on JRE 1.6.x or above. The user needs to copy the text and place it in the text box and click on the button to get the frequency in the grid. Please download the software here:
Word Frequency

Iphone & Ipad Tips

Tips for using iPhone & iPad - 1
1.One of the most useful tool of an Iphone or an Ipad is the ability to take a screenshot of your screen. One example of its usefulness is taking a screenshot of an image or article online so it can be view later without needing internet access. To take a ScreenShot of whatever is currently on your screen hold down the home button and the lock button at the same time for about 2 seconds. You should see a flash and a screenshot of your screen will be in your camera roll.

2. Double-clicking the home button brings up the Multitasking bar. From here you can switch between running apps or completely exit out of an app.

3. Creating folders on the home screen can help save some space. It also allows you to group similar apps together, like games. To create folders on the home screen press down on an app until the apps start shaking, then decide what two apps you what in the same folder and drag one of the apps onto the other one. The two apps will merg…