Zeus
Zeus

Reputation: 6566

OnetoOne mapping issue

I have two tables Location and LocationCode as mentioned below

Relationship of the tables

'code' column is a FK of the locationCode table.

The following are the mapping files

locationCode.java

@Entity
@Table(name="locationCode")
public class locationCodeNode {
    @Id
    @Column(name="code")
    public String code;
    @Column(name="description")
    public String description;
}

locationNode.java

@Entity
@Table(name="location")
public class locationNode {

    @Id
    @Column(name="ID")
    public String id;

    @Column(name="value1")
    public String value1;

    @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="code",insertable=false,updatable=false,nullable=true)
    public locationCode code;
}

Some where in one of the service, i'm trying to set the 'code' value in the location table to the one of the row in the locationCode table**(the values in the locationCode table do not change, the are domain values)**

//fetch of hibernate this will not have code assigned to it yet
location location = locationDao.findById(20);

locationCode code = codeDao.findById(3);
location.setCode(code);

locationDao.saveOrUpdate(code);

//commit session.

the value of the code in the location table does not change.

Question is, am i doing it right for the hibernate mapping? Anything wrong with the code?

Upvotes: 0

Views: 55

Answers (2)

CompSciPsu
CompSciPsu

Reputation: 1

You should just have the locationCodeNode as the property on locationNode.

@Entity
@Table(name="location")
public class locationNode {

    @Id
    @Column(name="ID")
    public String id;

    @Column(name="value1")
    public String value1;

    @OneToOne
    public locationCodeNode code;
}

Upvotes: 0

gipinani
gipinani

Reputation: 14398

Try to remove

 insertable=false,updatable=false

From code field joincolumn definition

Upvotes: 1

Related Questions