在JHipster中获取当前登录的用户 [英] getting the current logged in user in JHipster

查看:119
本文介绍了在JHipster中获取当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个jhipster应用程序.我试图根据当前登录的用户获取对象列表.网上有一些简单的示例,例如

I am building a jhipster application. and I am trying to get a list of objects based on the current logged in user. there are a few simple examples online such as jhipster blog demo that describe how to get the current user's blogs by the current logged in user--and I wanted to mimic this. in the demo there isa repo method:

@Query("select blog from Blog blog where blog.user.login = ?#{principal.username}")
List<Blog> findByUserIsCurrentUser();

我试图用以下方式模仿它:

I have tried to mimic this with the following:

@Query("select userWorkoutTemplate from WorkoutTemplate workoutTemplate where workoutTemplate.userDemographic.user.login = ?#{principal.username}")
List<WorkoutTemplateDTO> findByUserIsCurrentUser();

但是我的IDE抛出此错误:

but my IDE throws this error:

嵌套异常为java.lang.IllegalArgumentException:方法公共抽象java.util.List com.thefitnation.repository.WorkoutTemplateRepository.findByUserIsCurrentUser()的查询验证失败!

nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.thefitnation.repository.WorkoutTemplateRepository.findByUserIsCurrentUser()!

我对休眠查询不太熟悉,但是我的IDE中的参数似乎有误.最后是否需要在某处配置参数?我使用DTO对象开箱即用的普通jhipster 4.

I am not too familiar with hibernate queries, but the parameter seems to be in error in my IDE. Does the parameter on the end need to be configured somewhere? I am using plain jhipster 4 out of the box with DTO objects.

以下是相关实体:

User.java

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

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

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<>();

推荐答案

您可以从

You can get the current logged in user id from SecurityUtils. SecurityUtils.getCurrentUserLogin() will give you the login of current logged in user. Use this login to fetch the user entity from db. (findOneByLogin)

这篇关于在JHipster中获取当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆