开发者

Would it be possible to just expose the foreign key instead of the entire object in a @ManyToOne interface

开发者 https://www.devze.com 2023-01-25 21:25 出处:网络
@Entity public class Organization {} @Entity public class User { @ManyToOne Organization org; } The @ManyToOne interface in the above code is modeled as \"Organization org\", even though only the o
@Entity public class Organization {}

@Entity public class User {
    @ManyToOne
    Organization org;
}

The @ManyToOne interface in the above code is modeled as "Organization org", even though only the organization id is stored in the database. Can't I just model this as Integer orgId so that I can avoiding loading the entire Organization object for persistence sake.

Sometimes when we bulk impo开发者_如何学JAVArt users for different Organization(s), more time is spent loading Organization(s) rather than user persistence itself. Would like to know, how others are handling this issue.


You should use @ManyToOne(fetch=FetchType.LAZY) for Hibernate lazy loading (FetchType.LAZY) so that the entire Organization object will not be loaded. If you are only accessing user.org.id hibernate will know not to join the whole table just to find the organization_id.

See http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-singleassoc-fetching

And yes you can just map Integer orgId if that's really the only field you will be using in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号