[英]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 ).在这里,我需要定义一个按名称查询的方法,该方法检索特定用户的特定钱包(我认为我可以通过User的id字段查询)并基于特定Coin并与之相关(我认为我可以通过Coin的id字段)。
How can I implement a behavior like this?我怎样才能实现这样的行为?
The following should work:以下应该有效:
public interface WalletRepository extends JpaRepository<Wallet, Integer> {
List<Wallet> findByUserIdAndCoinId();
}
You can read more about this at:您可以在以下位置阅读更多相关信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.