devxlogo

Mapping a Component in Hibernate

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist