简体   繁体   中英

Understand sql query generated by Hibernate

Let's say i have a class A (mapped with table tbl_a ) and a class B (mapped with table tbl_b ). These 2 classes (tables) have a relation OneToMany relationship for example. The class B also have a relation with another class C (table tbl_c ). The relation is also OneToMany for example. I make a query (select query) on table tbl_a via Hibernate Criteria. When i check in the console the sql that hibernate generates, i see all the properties of class A , class B and even class C . Even if everything is working well, the query is to big and selecting all these properties (columns) may affect the performance. I don't want all the properties of class B and C . I just want the properties of class A . Is there a configuration in Hibernate, to not select all the properties of the related tables? Note: the default Lazy fetchType is used.

Would have been better if we could see what code you have written. However, will try to give heads up

 @Entity @Table(name="a") public class A{ @Id @column(name="id") @GeneratedValue(Strategy=GenerationType.AUTO) private int id; // suppose this class is mapped to class B as many to one @ManyToOne(Fetch=FetchType.EAGER) @JoinColumn(name="b_id") private B b; //Note that it is advisable to keep many to one relationships fetch type as eager. Though it depends on project architecture. Performance wise it fetches only one instance in memory this class is mapped to. //getter setters } @Entity @Table(name="b") public class B{ @Id @column(name="id") @GeneratedValue(Strategy=GenerationType.AUTO) private int id; @OneToMany(fetch=FetchType.LAZY, mappedBy="b",Cascade=CascadeType.ALL) private Set<A> allA = new HashSet<A>(); //this says that keep a onetomany relationship but do not fetch any of the associated entities until said so. Which is advisable as because If we keep FetchType.EAGER then it will fetch more than one entity for a single entity. Suppose B entity is related to 10 A entities then it will load all of them as soon as B is fetched in memory, so it will be a performance issue for a semi large application also. //getter setter } 

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