简体   繁体   English

如何通过基于两个属性检索特定 object 的方法名称来实现此 Spring 数据 JPA 查询?

[英]How can I implement this Spring Data JPA query by method name that retrieve a specific object based on two properties?

I am working on a Spring Boot project using Spring Data JPA trying to adopt the "query by method name" style in order to define my queries into repositories.我正在使用 Spring 数据 JPA 尝试采用“按方法名称查询”样式来将我的查询定义到存储库中的 Spring 引导项目。

I am finding some difficulties trying to implement a select query retrieving the list of objects based on two different "where condition".我在尝试实现基于两个不同“where 条件”检索对象列表的 select 查询时发现了一些困难。 I will try to explain what I have to do.我将尝试解释我必须做什么。

First of all this is my main entity class named Wallet :首先这是我的主要实体 class 名为Wallet

@Entity
@Table(name = "wallet")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Wallet implements Serializable {

    private static final long serialVersionUID = 6956974379644960088L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;


    @Column(name = "address")
    private String address;

    @Column(name = "notes")
    private String notes;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "fk_user_id", referencedColumnName = "id")
    @JsonBackReference(value = "user-wallets")
    private User user;

    @ManyToOne
    @EqualsAndHashCode.Exclude          // Needed by Lombock in "Many To One" relathionship to avoid error
    @JoinColumn(name = "fk_coin_id", referencedColumnName = "id")
    private Coin coin;


    @ManyToOne
    @JoinColumn(name = "type", referencedColumnName = "id")
    private WalletType walletType;


    public Wallet(String address, String notes, User user, Coin coin, WalletType walletType) {
        super();
        this.address = address;
        this.notes = notes;
        this.user = user;
        this.coin = coin;
        this.walletType = walletType;
    }
    
    
}

As you can see a wallet is directly binded to a specific User object and to a specific Coin object.如您所见,钱包直接绑定到特定用户object 和特定硬币object。

For completeness this is the code of my User entity class:为了完整起见,这是我的用户实体 class 的代码:

@Entity
@Table(name = "portal_user")
@Getter
@Setter
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User implements Serializable {
     
    private static final long serialVersionUID = 5062673109048808267L;
    
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    @Column(name = "first_name")
    @NotNull(message = "{NotNull.User.firstName.Validation}")
    private String firstName;
    
    @Column(name = "middle_name")
    private String middleName;
    
    @Column(name = "surname")
    @NotNull(message = "{NotNull.User.surname.Validation}")
    private String surname;
    
    @Column(name = "sex")
    @NotNull(message = "{NotNull.User.sex.Validation}")
    private char sex;
    
    @Column(name = "birthdate")
    @NotNull(message = "{NotNull.User.birthdate.Validation}")
    private Date birthdate;
    
    @Column(name = "tax_code")
    @NotNull(message = "{NotNull.User.taxCode.Validation}")
    private String taxCode;
    
    @Column(name = "e_mail")
    @NotNull(message = "{NotNull.User.email.Validation}")
    private String email;
    
    @Column(name = "pswd")
    @NotNull(message = "{NotNull.User.pswd.Validation}")
    private String pswd;
    
    @Column(name = "contact_number")
    @NotNull(message = "{NotNull.User.contactNumber.Validation}")
    private String contactNumber;
    
    @Temporal(TemporalType.DATE)
    @Column(name = "created_at")
    private Date createdAt;
    
    @Column(name = "is_active")
    private boolean is_active;
    
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    @JsonManagedReference(value = "address")
    private Set<Address> addressesList = new HashSet<>();
    
    @ManyToMany(cascade = { CascadeType.MERGE })
    @JoinTable(
        name = "portal_user_user_type", 
        joinColumns = { @JoinColumn(name = "portal_user_id_fk") }, 
        inverseJoinColumns = { @JoinColumn(name = "user_type_id_fk") }
    )
    private Set<UserType> userTypes;


    @ManyToOne(fetch = FetchType.LAZY)
    @JsonProperty("subagent")
    private User parent;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    @JsonManagedReference(value = "user-wallets")
    private Set<Wallet> wallets = new HashSet<>();


    public User() {
        super();
        // TODO Auto-generated constructor stub
    }


    public User(String firstName, String middleName, String surname, char sex, Date birthdate, String taxCode,
            String email, String pswd, String contactNumber, Date createdAt, boolean is_active) {
        super();
        this.firstName = firstName;
        this.middleName = middleName;
        this.surname = surname;
        this.sex = sex;
        this.birthdate = birthdate;
        this.taxCode = taxCode;
        this.email = email;
        this.pswd = pswd;
        this.contactNumber = contactNumber;
        this.createdAt = createdAt;
        this.is_active = is_active;
    }
    

}

and this is the code of my Coin entity class:这是我的硬币实体 class 的代码:

@Entity
@Table(name = "coin")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Coin implements Serializable {

    private static final long serialVersionUID = 6956974379644960088L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name = "name")
    @NotNull(message = "{NotNull.Coin.name.Validation}")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "code", unique = true)
    @NotNull(message = "{NotNull.Coin.code.Validation}")
    private String code;

    @Type(type="org.hibernate.type.BinaryType")
    @Column(name = "logo")
    private byte[] logo;


}

Then I have this WalletRepository interface:然后我有这个WalletRepository接口:

public interface WalletRepository extends JpaRepository<Wallet, Integer> {

}

Here I need to define a query by name method that retrieve a specific wallet of a specific User (I think that I can query by the id field of the User ) and based and related to a specific Coin (I think that I can query by the id fied of the Coin ).在这里,我需要定义一个按名称查询的方法,该方法检索特定用户的特定钱包(我认为我可以通过Userid字段查询)并基于特定Coin并与之相关(我认为我可以通过Coinid字段)。

How can I implement a behavior like this?我怎样才能实现这样的行为?

暂无
暂无

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

相关问题 如何使用带有“从方法名创建查询”策略的Spring数据JPA来实现这两个简单查询? - How can I implement these 2 simple queries using Spring data JPA with the “query creation from method names” strategy? 如何才能正确实现涉及两个表之间的JOIN的Spring Data JPA命名查询? - How can I correctly implement this Spring Data JPA named query that involves a JOIN between 2 tables? 如何将这3个JOIN查询转换为Spring Data JPA命名查询方法? - How can I convert this 3 JOIN query into a Spring Data JPA named query method? 如何设置Spring Data JPA查询方法必须始终设置为true的参数值? - How can I set that a Spring Data JPA query method have to be a parameter value always set as true? 如何在 Spring Data jpa 中应用本机连接查询? - How can i apply native join query in spring data jpa? 如何使用Spring Data JPA以Paginated方式检索特定列? - How to retrieve specific columns in Paginated way using Spring Data JPA? 如何使用spring-data-jpa检索聚合函数查询 - how to retrieve an aggregation function query with spring-data-jpa 如何使用 spring-data-jpa 2.1 实现长/复杂查询 - How to implement long/complex query with spring-data-jpa 2.1 Spring数据JPA的查询方法如何生成查询? - How query method of Spring Data JPA generate query? 有没有办法在弹簧数据 JPA 查询中使用带有“And”的属性? - Is there a way to use properties with "And" in a spring data JPA query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM