gpt4 book ai didi

hibernate - Spring Data Jpa 延迟加载未按预期工作

转载 作者:行者123 更新时间:2023-12-02 02:55:03 24 4
gpt4 key购买 nike

嘿,我正在使用 Spring Data jpa,我只想延迟获取数据。

我在我的 spring 项目中使用 lombook

我想通过电子邮件获取用户及其角色。

我的课很少

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private String name;

@Column(columnDefinition = "char(50)")
private String email;

@Column(columnDefinition = "char(15)")
private String phone;

private String password;

@Column(columnDefinition = "bit")
private boolean isActive;

@Column(columnDefinition = "ENUM('MALE', 'FEMALE', 'OTHER')")
@Enumerated(EnumType.STRING)
private Gender gender;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private List<UserRole> userRoles;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
private UserProfile userProfile;

}
@Entity
@Table(name = "user_profile")
@Data
public class UserProfile extends DateAudit {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private String picture;
private float avgRating;

@Column(columnDefinition = "char(13)")
private String cnic;

@Temporal(TemporalType.DATE)
private Date dateOfBirth;

//relations

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "city_id")
private City city;
}
@Entity
@Data
@Table
public class UserRole {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
private Role role;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "hospital_id")
private Hospital hospital;
}

这是我的用户存储库

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {

@EntityGraph(attributePaths = {"userRoles"})
User findByEmail(String email);
}

下面是我用来获取用户的操作

    @GetMapping("/test")
public String test(){

userRepository.findByEmail("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea8b8e878384aa8e85878b8384c4898587" rel="noreferrer noopener nofollow">[email protected]</a>");
return "test";
}

我在 application.properties 中添加了以下属性

spring.jpa.properties.hibernate.show_sql=true

所以当我点击 postman 的/test 操作时我看到在我的应用程序控制台中执行了两个查询。

Hibernate: select user0_.id as id1_21_0_, userroles1_.id as id1_23_1_, user0_.created_at as created_2_21_0_, user0_.updated_at as updated_3_21_0_, user0_.email as email4_21_0_, user0_.gender as gender5_21_0_, user0_.is_active as is_activ6_21_0_, user0_.name as name7_21_0_, user0_.password as password8_21_0_, user0_.phone as phone9_21_0_, userroles1_.hospital_id as hospital2_23_1_, userroles1_.role_id as role_id3_23_1_, userroles1_.user_id as user_id4_23_1_, userroles1_.user_id as user_id4_23_0__, userroles1_.id as id1_23_0__ from user user0_ left outer join user_role userroles1_ on user0_.id=userroles1_.user_id where user0_.email=?
Hibernate: select userprofil0_.id as id1_22_0_, userprofil0_.created_at as created_2_22_0_, userprofil0_.updated_at as updated_3_22_0_, userprofil0_.avg_rating as avg_rati4_22_0_, userprofil0_.city_id as city_id8_22_0_, userprofil0_.cnic as cnic5_22_0_, userprofil0_.date_of_birth as date_of_6_22_0_, userprofil0_.picture as picture7_22_0_, userprofil0_.user_id as user_id9_22_0_ from user_profile userprofil0_ where userprofil0_.user_id=?

我对用户个人资料不感兴趣。我在这里做错了什么。

最佳答案

发生这种情况是因为这是一个双向关联,Hibernate 需要知道是否应使用 null 或代理类来初始化 userProfile 属性,并且只能通过查询 user_profile 表来找到这一点。

您可以避免删除外键列并为两个关联实体使用相同的主键值,因为您必须在拥有方使用 MapsId 注释,如下所示:

@Entity
@Table(name = "user_profile")
@Data
public class UserProfile extends DateAudit {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne
@MapsId
@JoinColumn(name = "id")
private User user;
.
.
.
}

在这里您可以查看此问题的完整指南 https://thoughts-on-java.org/hibernate-tip-lazy-loading-one-to-one/


如果您不想使用相同的 key ,另一个选项是将其映射为 OneToMany 关系。

关于hibernate - Spring Data Jpa 延迟加载未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61294991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com