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!
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 .
Hibernate provide a way to map subclass using discriminator keyword.