我正在构建一个jhipster应用程序。我试图根据当前登录的用户获取对象列表。在线上有一些简单的示例,例如jhipster博客演示,描述了如何通过当前登录用户获取当前用户的博客,而我想对此进行模仿。在演示中,有一个repo方法:
@Query("select blog from Blog blog where blog.user.login = ?#{principal.username}") List<Blog> findByUserIsCurrentUser();
我试图用以下方法来模仿:
@Query("select userWorkoutTemplate from WorkoutTemplate workoutTemplate where workoutTemplate.userDemographic.user.login = ?#{principal.username}") List<WorkoutTemplateDTO> findByUserIsCurrentUser();
但是我的IDE抛出此错误:
嵌套异常为java.lang.IllegalArgumentException:方法公共抽象java.util.List com.thefitnation.repository.WorkoutTemplateRepository.findByUserIsCurrentUser()的查询验证失败!
我对hibernate查询不太熟悉,但是该参数在我的IDE中似乎有误。最后是否需要在某处配置参数?我使用DTO对象开箱即用的普通jhipster 4。
以下是相关实体:
User.java
@Entity @Table(name = "jhi_user") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class User extends AbstractAuditingEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") @SequenceGenerator(name = "sequenceGenerator") private Long id; @NotNull @Pattern(regexp = Constants.LOGIN_REGEX) @Size(min = 1, max = 50) @Column(length = 50, unique = true, nullable = false) private String login; @JsonIgnore @NotNull @Size(min = 60, max = 60) @Column(name = "password_hash",length = 60) private String password; @Size(max = 50) @Column(name = "first_name", length = 50) private String firstName; @Size(max = 50) @Column(name = "last_name", length = 50) private String lastName; @Email @Size(max = 100) @Column(length = 100, unique = true) private String email; @NotNull @Column(nullable = false) private boolean activated = false; @Size(min = 2, max = 5) @Column(name = "lang_key", length = 5) private String langKey; @Size(max = 256) @Column(name = "image_url", length = 256) private String imageUrl; @Size(max = 20) @Column(name = "activation_key", length = 20) @JsonIgnore private String activationKey; @Size(max = 20) @Column(name = "reset_key", length = 20) private String resetKey; @Column(name = "reset_date") private ZonedDateTime resetDate = null; @JsonIgnore @ManyToMany @JoinTable( name = "jhi_user_authority", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")}) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @BatchSize(size = 20) private Set<Authority> authorities = new HashSet<>(); ....
UserDemographic.java
@Entity @Table(name = "user_demographic") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class UserDemographic implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") @SequenceGenerator(name = "sequenceGenerator") private Long id; @NotNull @Column(name = "created_on", nullable = false) private LocalDate createdOn; @NotNull @Column(name = "last_login", nullable = false) private LocalDate lastLogin; @Enumerated(EnumType.STRING) @Column(name = "gender") private Gender gender; @NotNull @Column(name = "date_of_birth", nullable = false) private LocalDate dateOfBirth; @Column(name = "height") private Float height; @NotNull @Enumerated(EnumType.STRING) @Column(name = "unit_of_measure", nullable = false) private UnitOfMeasure unitOfMeasure; @OneToOne(optional = false) @NotNull @JoinColumn(unique = true) private User user; @ManyToMany @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @JoinTable(name = "user_demographic_gym", joinColumns = @JoinColumn(name="user_demographics_id", referencedColumnName="id"), inverseJoinColumns = @JoinColumn(name="gyms_id", referencedColumnName="id")) private Set<Gym> gyms = new HashSet<>(); @OneToMany(mappedBy = "userDemographic") @JsonIgnore @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) private Set<UserWeight> userWeights = new HashSet<>(); @OneToMany(mappedBy = "userDemographic") @JsonIgnore @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) private Set<WorkoutTemplate> workoutTemplates = new HashSet<>(); @OneToMany(mappedBy = "userDemographic") @JsonIgnore @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) private Set<UserWorkoutTemplate> userWorkoutTemplates = new HashSet<>(); @ManyToOne(optional = false) @NotNull private SkillLevel skillLevel;
UserWorkoutTemplate.java
@Entity @Table(name = "user_workout_template") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class UserWorkoutTemplate implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator") @SequenceGenerator(name = "sequenceGenerator") private Long id; @NotNull @Column(name = "created_on", nullable = false) private LocalDate createdOn; @NotNull @Column(name = "last_updated", nullable = false) private LocalDate lastUpdated; @Column(name = "notes") private String notes; @ManyToOne(optional = false) @NotNull private UserDemographic userDemographic; @ManyToOne private WorkoutTemplate workoutTemplate; @OneToMany(mappedBy = "userWorkoutTemplate", fetch = FetchType.EAGER) @JsonIgnore @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) private Set<UserWorkoutInstance> userWorkoutInstances = new HashSet<>();
您可以从SecurityUtils获取当前登录的用户ID 。SecurityUtils.getCurrentUserLogin()将为您提供当前登录用户的登录名。使用此登录名从数据库获取用户实体。(findOneByLogin)
SecurityUtils.getCurrentUserLogin()
findOneByLogin