使用JPA时查询单个表而不是所有关系表

时间:2015-01-17 04:31:32

标签: hibernate jpa spring-data-jpa

我们需要通过userid查询用户来执行每个Web请求以确保用户存在。每次查询时,它都会与其他关系表连接。怎么避免呢?我只想查询用户表。我为附件表设置了懒惰的Fetz类型,但它仍然加入了附件表。我错过了什么?

日志

Hibernate: select user0_.id as id1_6_, user0_.time_created as time_cre2_6_, user0_.uuid as uuid3_6_, user0_.version as version4_6_, user0_.email_address as email_ad5_6_, user0_.first_name as first_na6_6_, user0_.hashed_password as hashed_p7_6_, user0_.is_verified as is_verif8_6_, user0_.last_name as last_nam9_6_, user0_.photo_id as photo_i10_6_, user0_.public_id as public_11_6_, user0_.role as role12_6_, user0_.user_status as user_st13_6_ from user user0_ where user0_.email_address=?
Hibernate: select socialuser0_.user_id as user_id15_6_1_, socialuser0_.id as id1_5_1_, socialuser0_.id as id1_5_0_, socialuser0_.time_created as time_cre2_5_0_, socialuser0_.uuid as uuid3_5_0_, socialuser0_.version as version4_5_0_, socialuser0_.access_token as access_t5_5_0_, socialuser0_.display_name as display_6_5_0_, socialuser0_.expire_time as expire_t7_5_0_, socialuser0_.image_url as image_ur8_5_0_, socialuser0_.profile_url as profile_9_5_0_, socialuser0_.provider_id as provide10_5_0_, socialuser0_.provider_user_id as provide11_5_0_, socialuser0_.rank as rank12_5_0_, socialuser0_.refresh_token as refresh13_5_0_, socialuser0_.secret as secret14_5_0_, socialuser0_.user_id as user_id15_5_0_ from social_user socialuser0_ where socialuser0_.user_id=?
Hibernate: select sessions0_.user_id as user_id6_6_1_, sessions0_.id as id1_4_1_, sessions0_.id as id1_4_0_, sessions0_.is_keep_logged_in as is_keep_2_4_0_, sessions0_.last_updated as last_upd3_4_0_, sessions0_.time_created as time_cre4_4_0_, sessions0_.token as token5_4_0_, sessions0_.user_id as user_id6_4_0_ from session_token sessions0_ where sessions0_.user_id=?
Hibernate: select user0_.id as id1_6_1_, user0_.time_created as time_cre2_6_1_, user0_.uuid as uuid3_6_1_, user0_.version as version4_6_1_, user0_.email_address as email_ad5_6_1_, user0_.first_name as first_na6_6_1_, user0_.hashed_password as hashed_p7_6_1_, user0_.is_verified as is_verif8_6_1_, user0_.last_name as last_nam9_6_1_, user0_.photo_id as photo_i10_6_1_, user0_.public_id as public_11_6_1_, user0_.role as role12_6_1_, user0_.user_status as user_st13_6_1_, attachment1_.user_id as user_id18_6_3_, attachment1_.id as id1_0_3_, attachment1_.id as id1_0_0_, attachment1_.time_created as time_cre2_0_0_, attachment1_.uuid as uuid3_0_0_, attachment1_.version as version4_0_0_, attachment1_.attachment_date as attachme5_0_0_, attachment1_.attachment_size as attachme6_0_0_, attachment1_.attachment_type as attachme7_0_0_, attachment1_.attachment_url as attachme8_0_0_, attachment1_.file_id as file_id9_0_0_, attachment1_.images_generated as images_10_0_0_, attachment1_.mime_type as mime_ty11_0_0_, attachment1_.playback_formats as playbac12_0_0_, attachment1_.ready_for_playback as ready_f13_0_0_, attachment1_.secured as secured14_0_0_, attachment1_.title as title15_0_0_, attachment1_.total_pages as total_p16_0_0_, attachment1_.user_id as user_id18_0_0_, attachment1_.virus_status as virus_s17_0_0_ from user user0_ left outer join attachment attachment1_ on user0_.id=attachment1_.user_id where user0_.id=?
Hibernate: select pages0_.attachment as attachme1_0_3_, pages0_.pages as pages2_2_3_, attachment1_.id as id1_1_0_, attachment1_.attachment_id as attachme6_1_0_, attachment1_.file_id as file_id2_1_0_, attachment1_.mime_type as mime_typ3_1_0_, attachment1_.page_number as page_num4_1_0_, attachment1_.page_type as page_typ5_1_0_, attachment2_.id as id1_0_1_, attachment2_.time_created as time_cre2_0_1_, attachment2_.uuid as uuid3_0_1_, attachment2_.version as version4_0_1_, attachment2_.attachment_date as attachme5_0_1_, attachment2_.attachment_size as attachme6_0_1_, attachment2_.attachment_type as attachme7_0_1_, attachment2_.attachment_url as attachme8_0_1_, attachment2_.file_id as file_id9_0_1_, attachment2_.images_generated as images_10_0_1_, attachment2_.mime_type as mime_ty11_0_1_, attachment2_.playback_formats as playbac12_0_1_, attachment2_.ready_for_playback as ready_f13_0_1_, attachment2_.secured as secured14_0_1_, attachment2_.title as title15_0_1_, attachment2_.total_pages as total_p16_0_1_, attachment2_.user_id as user_id18_0_1_, attachment2_.virus_status as virus_s17_0_1_, user3_.id as id1_6_2_, user3_.time_created as time_cre2_6_2_, user3_.uuid as uuid3_6_2_, user3_.version as version4_6_2_, user3_.email_address as email_ad5_6_2_, user3_.first_name as first_na6_6_2_, user3_.hashed_password as hashed_p7_6_2_, user3_.is_verified as is_verif8_6_2_, user3_.last_name as last_nam9_6_2_, user3_.photo_id as photo_i10_6_2_, user3_.public_id as public_11_6_2_, user3_.role as role12_6_2_, user3_.user_status as user_st13_6_2_ from attachment_pages pages0_ inner join attachment_has_pages attachment1_ on pages0_.pages=attachment1_.id left outer join attachment attachment2_ on attachment1_.attachment_id=attachment2_.id left outer join user user3_ on attachment2_.user_id=user3_.id where pages0_.attachment=?
Hibernate: select sessiontok0_.id as id1_4_0_, sessiontok0_.is_keep_logged_in as is_keep_2_4_0_, sessiontok0_.last_updated as last_upd3_4_0_, sessiontok0_.time_created as time_cre4_4_0_, sessiontok0_.token as token5_4_0_, sessiontok0_.user_id as user_id6_4_0_ from session_token sessiontok0_ where sessiontok0_.id=?
Hibernate: select sessiontok0_.id as id1_4_0_, sessiontok0_.is_keep_logged_in as is_keep_2_4_0_, sessiontok0_.last_updated as last_upd3_4_0_, sessiontok0_.time_created as time_cre4_4_0_, sessiontok0_.token as token5_4_0_, sessiontok0_.user_id as user_id6_4_0_ from session_token sessiontok0_ where sessiontok0_.id=?
Hibernate: select sessiontok0_.id as id1_4_0_, sessiontok0_.is_keep_logged_in as is_keep_2_4_0_, sessiontok0_.last_updated as last_upd3_4_0_, sessiontok0_.time_created as time_cre4_4_0_, sessiontok0_.token as token5_4_0_, sessiontok0_.user_id as user_id6_4_0_ from session_token sessiontok0_ where sessiontok0_.id=?
sessiontok0_.user_id as user_id6_4_0_ from session_token sessiontok0_ where sessiontok0_.id=?
Hibernate: insert into session_token (is_keep_logged_in, last_updated, time_created, token, user_id) values (?, ?, ?, ?, ?)
Hibernate: select sessions0_.user_id as user_id6_6_1_, sessions0_.id as id1_4_1_, sessions0_.id as id1_4_0_, sessions0_.is_keep_logged_in as is_keep_2_4_0_, sessions0_.last_updated as last_upd3_4_0_, sessions0_.time_created as time_cre4_4_0_, sessions0_.token as token5_4_0_, sessions0_.user_id as user_id6_4_0_ from session_token sessions0_ where sessions0_.user_id=?

用户实体

@Entity
@Table(name = "user")
public class User extends BaseEntity {

    private String firstName;

    private String lastName;

    private String emailAddress;

    private String hashedPassword;

@Enumerated(EnumType.STRING)
private Role role;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", targetEntity = SessionToken.class, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.TRUE)
@Sort(type = SortType.NATURAL)
private SortedSet<SessionToken> sessions = Collections
        .synchronizedSortedSet(new TreeSet<SessionToken>(Collections
                .<SessionToken> reverseOrder()));

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", targetEntity = Attachment.class, orphanRemoval = true, fetch = FetchType.LAZY)
@LazyCollection(LazyCollectionOption.TRUE)
@Sort(type = SortType.NATURAL)
private List<Attachment> attachment = new ArrayList<Attachment>();

@OneToMany(mappedBy = "user", targetEntity = VerificationToken.class, cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.TRUE)
private List<VerificationToken> verificationTokens = new ArrayList<VerificationToken>();

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER)
private Set<SocialUser> socialUsers = new HashSet<SocialUser>();


....
}

UserRepo类

public interface UserRepository extends CrudRepository<User, Long> {

    @Query("select u from User u where uuid = ?")
    User findByUuid(String uuid);

}

0 个答案:

没有答案