jantox jantox - 3 months ago 9
Java Question

Hibernate reference mapping

I have a table Marital Status with values "single", "married", etc.

I have a table Person with marital_status_id as foreign key.

How do I mapped this one? Any help will be appreciated as I am new in Hibernate. Or I dont need this one since there isn't of a relation between Person and Marital Status but just a reference?

Answer

First of all, a marital status does not need a separate table (does it? really?). It can be handled with a single character (very efficient)

However, in your case,

@Entity
@Table(name="PERSON")
Class Person(){
    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 15, scale = 0)
    private Long id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="MARITAL_STATUS_ID")
    MaritalStatus maritalStatus;
}

and

@Entity
@Table(name="MARITAL_STATUS")
Class MaritalStatus(){
    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 15, scale = 0)
    private Long id;

    @OneToMany(mappedBy="maritalStatus")
    Set<Person> persons;
}