简体   繁体   中英

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;

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM