Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 29, 2007

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 Car
public 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 Skills
public 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">
    <id name="id" column="car_id" type="string">
        <generator class="uuid.hex"/>
    </id>
    <property name="type" type="string"/>
    <component name="Skills" class="Skills">
        <property name="speed"/>
        <property name="gearBox"/>
    </component>
</class>
Notice the <component> tag. Now the CARS table will have the following columns: car_id, type, speed, and gearBox.

Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap