I've got entity like this:
@Entity
@Table(name = "formula")
public class Formula {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "formula_id")
private Long formulaId;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "time")
private int time;
@OneToMany(mappedBy = "formula",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Product> productList = new ArrayList<>();
And another Entity:
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long productId;
@Column(name = "product_name")
private String productName;
@Column(name = "amount")
private Double amount;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "formula_id")
private Formula formula;
I want to ask Query
to DB which help me get every type of data (by key word). I've got all except List of <Product>
. It look like this:
public interface FormulaRepository extends JpaRepository<Formula, Long> {
@Query("SELECT f FROM Formula f WHERE " + "CONCAT(f.name, f.description,
f.time)" + "LIKE %?1%")
List<Formula> findFormulaBy(String word);
How can add productList
to Query and acomplished my searching? Is there any possibility to do this in findFormulaBy(String word);
method?
Change query to include LEFT JOIN FETCH to eagerly fetch productList. Also include DISTINCT to prevent duplicate Formula objects in List
@Query("SELECT DISTINCT f FROM Formula f " +
"LEFT JOIN FETCH f.productList " +
"WHERE " + "CONCAT(f.name, f.description,f.time)" + "LIKE %?1%")
List<Formula> findFormulaBy(String word);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.