1

I want to filter motherName and sisterName but I don't know how to write a filter for embedded class. Please help me to solve this issue. Thank you in advance!

Family1 class

@Entity
public class Family1 {
    
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id;
    
    @Embedded
    private FamilyNames familyNames;
    
    @Column(nullable=false,name="familyMemebers")
    private int familyMembers;
    
    @Column(nullable=false,name="contactNo")
    private long contactNo;
    
    @Embedded
    @Column(nullable=false, name="Address" )
    private Address address;
    
    public Family1() {}
        
       // getters,setters

FamilyNames class

@Embeddable
public class FamilyNames {
    private String yourName;
    private String fatherName;
    private String motherName;
    private String sisterName;

    public FamilyNames() {}

    //getters, setters

Family1Controller class

@RestController
@RequestMapping(value = "/family1")
public class Family1Controller {
    
    @Autowired
    private Family1Service family1Service; 

    @RequestMapping(method=RequestMethod.GET,value="/fam1")
    public List<Family1> listOfDetails(@RequestParam("keyword") String keyWord){
        return family1Service.listOfDetails(keyWord);
    }
}

Family1Service class

@Service
public class Family1Service {
    
    @Autowired
    private Family1Repository familyRepository;
    
    public List<Family1> listOfDetails(String keyWord) {
       return familyRepository.findAll();
    }
}

Family1Repository class:

@Repository
public interface Family1Repository extends JpaRepository<Family1,Integer>  {

   @Query("Select f from family1 f where f.familyNames.motherName LIKE %?1%"
        + "OR f.familyNames.sisterName %?1%")
    List<Family1> findAll(String keyWord);
}

please help me which way to filter particular name in embedded class using query annotation

SternK
  • 11,649
  • 22
  • 32
  • 46
Dhiya
  • 27
  • 2
  • 8

1 Answers1

0

Try to use the following query:

@Query(
  "select f from Family1 f where f.familyNames.motherName LIKE '%' || :name || '%'"
    +" OR f.familyNames.sisterName LIKE '%' || :name || '%'")
List<Family1> findAll(@Param("name") String keyWord);
SternK
  • 11,649
  • 22
  • 32
  • 46
  • It will throw an error like this.1. Ambiguous mapping. Cannot map 'family1Controller' method. 2.There is already the 'family1Controller' bean method. How can I overcome the issue? Do you have any suggestion – Dhiya Jan 17 '21 at 17:01
  • I do not see how these errors related to the query. – SternK Jan 17 '21 at 17:09
  • I changed value="/fam1" to value=" " it will normally now – Dhiya Jan 17 '21 at 17:18
  • See [this question](https://stackoverflow.com/questions/58356927/how-to-pass-an-argument-to-a-spring-boot-rest-controller). – SternK Jan 17 '21 at 17:30
  • I have another doubt if I give request param in the localhost:8082/family1?keyWord=xxx it will not show only xxx related details it will show the entire details of the list. can you please tell me what mistake I made? – Dhiya Jan 17 '21 at 17:35