1

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?

4 Answers 4

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, I just use character to do away with unnecessary tables.
1

Marital Status should be expressed as enum in Java to exclude unnecessary joins since there are not so many options and they will never change.
Check @Enumerated(EnumType.STRING) annotation: http://docs.oracle.com/javaee/6/api/javax/persistence/Enumerated.html

1 Comment

It must work because basically all it does is saves value as a string - from the point of database there is no difference between java's string variable and enum - this concerns only persistent provider. Be sure to select postresql dialect in your persistence provider settings.
0

You have not specified whether you insist to have one-to-one relationship or you don't care , and also if you prefer using Enum instead of a Class.

This Link provides some useful information on one-to-one mapping in hibernate, if you want to have such relationship between your tables: one-to-one example

However hibernate one-to-one mapping is not much recommended, and you could simply use a one-to-many mapping just like this:

@Entity
@Table(name = "Person")
public class Person {

    @Id
    @GeneratedValue
    int id; 

    @OneToMany(mappedBy = "Person", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    Private MaritalStatus;
}

@Entity
@Table(name = "MaritalStatus")
public class Person {

    @Id
    @GeneratedValue
    int id; 

    @ManyToOne
    Person person = new Person();
}

This approach is much simple , but you may prefer to have an Enum instead of class and map your tables using enums. That approach is a bit more difficult to implement and this post provides you all the stuff you need : Mapping enum to a table

1 Comment

I think you have your @OneToMany and @ManyToOne annotations reversed.
0

I would do like below

Person entity:

@Entity
@Table(schema = "Person")
public class Person {
    @Id
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "marital_status_id")
    private MaritalStatus  maritalStatus;

}

Marital status entity:

@Entity
@Table(schema = "MaritalStatus")
public class MaritalStatus {
    @Id
    private String id;

    @Column(name = "status")
    private String status;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.