Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:45:13+00:00 2026-05-16T05:45:13+00:00

How to create mapping in hibernate that depends on the type property, insert/retrieve the

  • 0

How to create mapping in hibernate that depends on the type property, insert/retrieve the data into/from proper column.

Structure:

TABLE COLUMNS:

|TYPE | CHARACTER | DATE | TIME | NUMERIC|

POJO:

class Pojo {

 private int type;
 private Object data;

 ...

}

Examples:

  • Insert/Update
    If the type is 1 we input the value to column CHARACTER

  • Select
    If the type is 2 we get the value from column NUMERIC

TIP:
The structure where we have two columns and we PIVOT the result is not a option for this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:45:14+00:00Added an answer on May 16, 2026 at 5:45 am

    The solution that i have choose for this is table per class hierarchy mapping with an integer discriminator.

    I have created a base abstract generic class that store the object

     public abstract class ValueBase<T> {
    
       private Long id; 
       private int valueType;
       private T value;
    
       public T getValue(){
         return value;
       }
    
       public void setValue(T value){
         this.value = value;
       }
    
       protected void setValueType(int valueType) {
          this.valueType = valueType;
       }
    
       public int getValueType(){
         return this.valueType;
       }
    
       private void setID(Long id) {
          this.id = id;
       }
    
       public int getID(){
         return this.id;
       }
    
    
     }
    

    For Type management i have created and interface, but there is possible to enumeration using the encapsulation presented by Arthur.

    public interface IValueTypes() {
    
     public static final int NONE = -1;
     public static final int NUMERIC = 0;
     public static final int CHARACTER = 1;
     public static final int DATE = 2;
     public static final int TIME = 3;
     public static final int BOOLEAN = 4;
    
    }
    

    The for each type I have created class

     public class NumericType extedns ValueBase<BigDecinal> {
    
        public NumericType(){
          super();
          setValueType(IValueTypes.NUMERIC);
        }
    
     }
    
     public class CharacterType extedns ValueBase<String> {
    
        public NumericType(){
          super();
          setValueType(IValueTypes.CHARACTER);
        }
    
     }
    

    TIP: To allow use the a number as discriminator value we need to specify it first for the class that we are mapping, because by default is used class name [String] and i we try to use the numeric discriminator for subclass mapping we will get a hibernate error say that there is some type mismatch.

    <class name="TestValue" table="TestValues" schema="TEST" abstract="true" discriminator-value="-1"> 
      <id name="id" type="java.lang.Long">
        <column name="VALUE_ID" precision="22" scale="0" />
        <generator class="native"/>
      </id>
    
      <discriminator column="TYPE_VALUE" not-null="false" type="integer"/> 
    
      <subclass discriminator-value="0" name="NumericType">
        <property name="value" type="java.math.BigDecimal" column="NUMERIC" not-null="false"/>
      </subclass>   
    
      <subclass discriminator-value="1" name="CharacterType">
        <property name="value" type="java.lang.String" column="CHARACTER" not-null="false"/>
      </subclass>   
    
    </class>
    

    While we retrieve the object from database is always in given typed class that we can operate easily on it, and we use one place to save them by saving the base class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.