简体   繁体   中英

JPA:ownership vs direction

In JPA what is the difference between the ownerhisp and direction of a relation.

I know that in manyToOne relations you always declare the many side as the owner to let JPA implementation know you want a foreign key constraint on that table.

Bidirectional relations let both entities of the relation know about each other.

But as you can see I'm a little confused,can you help ?

You already described the direction . Associations can be unidirectional and bidirectional.

The ownership in a bidirectional association tells Hibernate which side to check when storing association to database. The inverse side is not checked. If you associate two entities on the inverse side only, Hibernate will not flush the association to the database, so, next time when you read them (in a new or cleared session) they will not be associated on the inverse side either.

"I know that in manyToOne relations you always declare the many side as the owner to let JPA implementation know you want a foreign key constraint on that table. "

Ownership has really nothing to do with foreign keys (although declaring the ownership on the foreign key side is a good practice and helps avoid an additional update in some situations) and in rare cases the owner of the bidirectional association can actually be the one-to-many side. In many-to-one relation the foreign key column must always be in the source entity, otherwise the association would not make sense.

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