spring – 在JHipster中获取当前登录用户
作者:互联网
我正在构建一个jhipster应用程序.我试图根据当前登录用户获取对象列表.有一些简单的在线示例,如jhipster blog demo,描述了如何通过当前登录用户获取当前用户的博客 – 我想模仿这一点.在演示中有一个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抛出了这个错误:
nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract 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()将为您提供当前登录用户的登录信息.使用此登录名从db获取用户实体. (findOneByLogin)
标签:jhipster,spring,hibernate 来源: https://codeday.me/bug/20190927/1824964.html