我正在使用jhipster创建一个Spring-boot应用程序,其中用户拥有moneyAccounts。当前,当用户登录到应用程序时,他们可以看到所有用户的所有moneyAccounts,我正在添加一种方法,该方法将仅查找当前登录用户的帐户。如何制定仅根据当前登录用户查找帐户的查询?
我已经尝试过与该post中的查询类似的查询,但是除非使用本机查询格式,否则我的构建将无法运行。我以本机格式重新构建了查询,如下所示。
@Query(value = "select * from MONEY_ACCOUNT where MONEY_ACCOUNT.USER_DETAILS_ID = ?#{principal?.id}", nativeQuery = true)
List<MoneyAccount> findByUserIsCurrentUser();
以上查询在我的页面上产生了500错误。任何建议都欢迎!
实体:
@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, nullable = false)
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(min = 5, max = 254)
@Column(length = 254, unique = true)
private String email;
@NotNull
@Column(nullable = false)
private boolean activated = false;
@Size(min = 2, max = 6)
@Column(name = "lang_key", length = 6)
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)
@JsonIgnore
private String resetKey;
@Column(name = "reset_date")
private Instant 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<>();
//getters and setters etc.
存储库:
@Repository
public interface MoneyAccountRepository extends JpaRepository<MoneyAccount, Long> {
@Query(value = "select * from MONEY_ACCOUNT where MONEY_ACCOUNT.USER_DETAILS_ID = #{principal.id}", nativeQuery = true)
List<MoneyAccount> findByUserIsCurrentUser();
}
答案 0 :(得分:1)
您在主体前面缺少#,例如:
"select * from MONEY_ACCOUNT where MONEY_ACCOUNT.USER_DETAILS_ID = #{principal.id}"
阅读此tuto以获得更多信息。