简体   繁体   English

如何编写基于当前用户查找类的CRUD存储库方法的自定义查询?

[英]How to write custom query for CRUD repository method that finds class based on current user?

I'm creating a Spring-boot application using jhipster where users have moneyAccounts. 我正在使用jhipster创建一个Spring-boot应用程序,其中用户有moneyAccounts。 Currently when a user logs onto the application they can see all moneyAccounts for all users, and I am adding a method that will find only accounts for the currently logged in user. 当前,当用户登录到应用程序时,他们可以看到所有用户的所有moneyAccounts,我正在添加一种方法,该方法将仅查找当前登录用户的帐户。 How do I formulate a query that will only find accounts based on the currently logged in user? 如何制定仅根据当前登录用户查找帐户的查询?

I have tried a query similar to the one in this post but my build would not run unless I used the native query format. 我曾尝试类似一个在此查询 ,但我的构建将无法运行,除非我用了本地查询格式。 I reformulated the query in native format as shown below. 我以本机格式重新构建了查询,如下所示。

@Query(value = "select * from MONEY_ACCOUNT where MONEY_ACCOUNT.USER_DETAILS_ID = ?#{principal?.id}", nativeQuery = true)
List<MoneyAccount> findByUserIsCurrentUser();

The above query generated a 500 error on my page. 上面的查询在我的页面上产生了500错误。 Any advise is welcome! 任何建议都欢迎!

Entity: 实体:

@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: 库:

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

You are missing # in front of principal, something like : 您在主体前面缺少#,例如:

"select * from MONEY_ACCOUNT where MONEY_ACCOUNT.USER_DETAILS_ID = #{principal.id}"

Read this tuto for more informations. 阅读本教程以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM