5

I am fairly new to Hibernate and need some help with hibernate-mapping.

I have 4 different classes which I want to map into one table, of which the primary key consists of attributes from 2 different classes. At the same time, I want to map only selected attributes from each class into a local database. I wish to avoid JPA annotations and define the mapping style in a hbm.xml file instead. How do I do that?

Take the following example:

public class Tenant implements Serializable {
    private final static long serialVersionUID = 1L;
    protected List<Rack> rack;
    protected String type;
    //getters setters
}

public class Rack implements Serializable {
    private final static long serialVersionUID = 1L;        
    protected List<Circuit> circuit;
    protected String rackLabel;
    protected Boolean excludes;
    //getters setters
}

public class Circuit implements Serializable {
    private final static long serialVersionUID = 1L;
    protected List<CircuitReadings> circuitReadings;
    protected String circuitNo;
    protected Boolean excludes;
    //getters setters
}

public class CircuitReadings
    implements Serializable {
    private final static long serialVersionUID = 1L;
    protected String date;
    protected String kva;
    protected String current;
    protected String kwh;
    //getters setters
}

And the eventual table should consist of the following:

    type | rackLabel | circuitNo | date | kva | current | energy

"circuitNo" and "date" above should form the composite primary keys.

Can someone show me an example of how I should map this? Thanks!

2
  • 1
    Why add all four classes in a single table? From the way they look, all classes have a many to one relationship between them. Adding all of them in a single table would yield a very de-normalized table. This is usually a bad thing in relational databases. Commented Jan 5, 2012 at 7:07
  • This is because I don't want to store all attributes to the database, and want only selected ones. the attributes I want are highlighted in the table description above. My solution was to create a separate method that loops through the "Tenant" object and its sub classes, and consolidate only values I want stored into the database into a separate class e.g. ClassA. A hbm file is then created for ClassA. I know it's not ideal to do this, hence, i'm looking for a more optimal solution. Commented Jan 13, 2012 at 7:25

2 Answers 2

3

Hibernate provide a way to map subclass using discriminator keyword.

<class name="Payment" table="PAYMENT">
    <id name="id" type="long" column="PAYMENT_ID">
        <generator class="native"/>
    </id>
    <discriminator column="PAYMENT_TYPE" type="string"/>
    <property name="amount" column="AMOUNT"/>
    ...
    <subclass name="CreditCardPayment" discriminator-value="CREDIT">
        <join table="CREDIT_PAYMENT">
            <key column="PAYMENT_ID"/>
            <property name="creditCardType" column="CCTYPE"/>
            ...
        </join>
    </subclass>
    <subclass name="CashPayment" discriminator-value="CASH">
        <join table="CASH_PAYMENT">
            <key column="PAYMENT_ID"/>
            ...
        </join>
    </subclass>
    <subclass name="ChequePayment" discriminator-value="CHEQUE">
        <join table="CHEQUE_PAYMENT" fetch="select">
            <key column="PAYMENT_ID"/>
            ...
        </join>
    </subclass>
</class>
Sign up to request clarification or add additional context in comments.

Comments

0

There's nothing stopping you from doing that . Create 4 HBMs pointing to the same table but different Pojos . Though it can be done , as @Ioan Alexandru Cucu , it is not recommended .

   <!-- HBM1-->

<class name="com.myProject.Rack"
    table="My_Table">

   <!-- HBM2-->

   <class name="com.myProject.Rack"
    table="My_Table">

1 Comment

I'm trying to understand how this would work. Referring back to my example,, how do I specify in the hbm that I want to repeat the same "circuitNo" for each "date", "kva", "current", and "energy" in the "CircuitReadings" list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.