Mapping a Component in Hibernate

A “component” in Hibernate is an object persisted as a value, not an entity reference. For example, suppose you have a Car class, like this:

//class Carpublic class Car {    private String id;    private String type;    private Skills skills;    public String getId() {        return id;    }    private void setId(String id) {        this.id=id;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public Name getSkills() {        return skills;    }    public void setSkills(Skills skills) {        this.skills = skills;    }    ......}

And the Skills class looks like this:

//class Skillspublic class Skills {    int speed;    String gearBox;    public int getSpeed() {        return speed;    }    void setSpeed(int speed) {        this.speed = speed;    }    public String getGearBox() {        return gearBox;    }    void setGearBox(String gearBox) {        this.gearBox = gearBox;    }}

Here is the Hibernate mapping for the Car class:

<class name="Car" table="CARS">                                            

Notice the tag. Now the CARS table will have the following columns: car_id, type, speed, and gearBox.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: