Hello guys i have a small issue with lazy fetching
i have two entities
Code:
@Entity
@Table(name = "users", schema = "public")
public class User implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -2847913650097002280L;
private Long userId;
private String username;
private String passwordHash;
private String passwordSalt;
private Merchant merchant;
@Id
@SequenceGenerator(name = "user_sequence", sequenceName = "users_user_id_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "user_sequence")
@Column(name = "user_id", unique = true, nullable = false)
public Long getUserId() {
return this.userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Column(name = "username", nullable = false, length = 50)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password_hash", nullable = false, length = 40)
public String getPasswordHash() {
return this.passwordHash;
}
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
@Column(name = "password_salt", nullable = false, length = 8)
public String getPasswordSalt() {
return this.passwordSalt;
}
public void setPasswordSalt(String passwordSalt) {
this.passwordSalt = passwordSalt;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
public Merchant getMerchant() {
return this.merchant;
}
public void setMerchant(Merchant merchant) {
this.merchant = merchant;
}
}
@Entity
@Table(name = "merchants_tree", schema = "public")
public class MerchantsTree implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 893980006703533469L;
private Long userIdChildren;
private User user;
private Long userIdParent;
private Integer lft;
private Integer rgt;
@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "user_id_children", unique = true, nullable = false)
public Long getUserIdChildren() {
return this.userIdChildren;
}
public void setUserIdChildren(Long userIdChildren) {
this.userIdChildren = userIdChildren;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public User getUser() {
return this.user;
}
public void setUser(User users) {
this.user = users;
}
@Column(name = "user_id_parent")
public Long getUserIdParent() {
return this.userIdParent;
}
public void setUserIdParent(Long userIdParent) {
this.userIdParent = userIdParent;
}
@Column(name = "lft")
public Integer getLft() {
return this.lft;
}
public void setLft(Integer lft) {
this.lft = lft;
}
@Column(name = "rgt")
public Integer getRgt() {
return this.rgt;
}
public void setRgt(Integer rgt) {
this.rgt = rgt;
}
}
and i have this query
Code:
Criteria criteria = session.createCriteria(MerchantsTree.class);
criteria = criteria.setFetchMode("user", FetchMode.JOIN);
criteria.add(Restrictions.between("lft", 1,18));
my problem is that when i get the results and see what queries have been called there are two many queries
First query is
Quote:
select
this_.user_id_children as user1_3_1_,
this_.lft as lft3_1_,
this_.rgt as rgt3_1_,
this_.user_id_parent as user4_3_1_,
user2_.user_id as user1_4_0_,
user2_.password_hash as password2_4_0_,
user2_.password_salt as password3_4_0_,
user2_.username as username4_0_
from
public.merchants_tree this_
inner join
public.users user2_
on this_.user_id_children=user2_.user_id
where
this_.lft between ? and ?
and other queries are
Quote:
select
merchant0_.merchant_id as merchant1_2_0_
from
public.merchants merchant0_
where
merchant0_.merchant_id=?
for each row that the first query returned.
What i don’t understand is why doest it loads merchant table since its marked as FetchType.LAZY in User.class?
Regards
Dimitris Zenios