Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

When do I use @OneToOne annotation in Hibernate?

I have two tables table1, table2. Both have a one to one relation.

table2 contains foreign key of table1.

If I use @OneToOne(cascade=CascadeType.ALL) or @ManyToOne(fetch=FetchType.LAZY) for the method below. Then what will be the effect of it?

@Column( name = "table1_id" )
public Long getTable1Id() {
    return this.table1Id;

}

Upvotes: 0

Views: 521

Answers (1)

dbf
dbf

Reputation: 6499

If you use OneToOne than you need to define not id variable in class, but object of another class, like in these examples: http://docs.oracle.com/javaee/5/api/javax/persistence/OneToOne.html.

Lazy means that row from other table will not be fetched until accessed. CascadeType.ALL means that all operations (like delete) will be propagated to associated object.

Upvotes: 1

Related Questions