0

I have three entities with the below relationship

FormEntity OnetoOne ValidationEntity 
FormEntity OnetoMany MszEntity 

msz entity has some messages based on language id like ENG, HIN, RSA, CHN. so I create JPQL query but the problem is it's returning all the languages records

@Query

@Query("select a From FormEntity As a join a.ValidationEntity As b join 
 a.MszEntity as c where a.formId =:formId and 
 c.validationLanguageId.languageId='ENG'")

Response

 {
        "formFieldId": 1,
        "formId": "Login",
        "formFields": "username",
        "ValidationEntity ": {
            "formFieldId": 1,
            "required": true,
            "numberMin": 2
        },
        "spdFormFieldsValidationMszInfo": [
            {
                "formFieldId": 1,
                "versionId": 1,
                "languageId": "ENG",
                "required": "ENG- Please Enter Username",
                "numberMin": "ENG- Min 2 Number Required"
            },
            {
                "formFieldId": 1,
                "versionId": 1,
                "languageId": "CHA",
                "required": "CHA- Please Enter Username",
                "numberMin": "CHA- Min 2 Number Required"
            }
        ]
 }

Expected result

     {
        "formFieldId": 1,
        "formId": "Login",
        "formFields": "username",
        "ValidationEntity ": {
            "formFieldId": 1,
            "required": true,
            "numberMin": 2
        },
        "spdFormFieldsValidationMszInfo": [
            {
                "formFieldId": 1,
                "versionId": 1,
                "languageId": "ENG",
                "required": "ENG- Please Enter Username",
                "numberMin": "ENG- Min 2 Number Required"
            }
        ]
 }
nitin verma
  • 616
  • 1
  • 6
  • 22
  • 1
    That's not a bug. It's a feature. A OneToMany association contains all the elements associated with the source entity. Always. If you have 3 brothers, and if I ask you the names of your brothers, you'll tell me 3 names. Even if I happened to have found you because I asked "who has one brother named John?" and you answered "I do". Same here: you asking "who has an MszEntity with language ENG", you find one, and then you ask "give me your MszEntities". – JB Nizet Sep 17 '18 at 13:12
  • ok plz correct me if i am wrong. in sql query basically we use where clause to filter the result same approach should we work in JPQL. then why its not filtering the result on the basis of language id – nitin verma Sep 18 '18 at 07:12
  • 1
    It is. The query only returns FormEntity instances that have a validation entity which has a MszEntity with the english language. But then you ask that entity: give me all your msz entities. If you want only the msz entity which has that language, then you need to query for it, i.e. it must be in the select clause. – JB Nizet Sep 18 '18 at 07:16
  • ok I updated my question with expected result so can you please answer how can i get on the basis of form id and languageId. – nitin verma Sep 18 '18 at 07:24
  • Possible duplicate of [How to filter child entities collections with predicate?](https://stackoverflow.com/questions/25627484/how-to-filter-child-entities-collections-with-predicate) – Jens Schauder Sep 18 '18 at 11:50

0 Answers0