Hibernate+Jersey, missing data in service response

67 Views Asked by At

Im tryin' to face some problem with one service, which should return user data + list of all recipies(assigned to user id), but the result that im getting is only user data, without recipies:

person_id: 3,
firstName: "Marek",
lastName: "Bawski",
email: "[email protected]",
password: "##@$#FDSgsdG",
address: {
street: "Street",
houseNumber: 4,
apartmentNumber: 0,
zipCode: "13-200",
province: "XSXSXSX"
}

SQL genereted by hibernate:

Hibernate: 
    select
        person0_.person_id as person_i1_2_0_,
        person0_.apartment_number as apartmen2_2_0_,
        person0_.house_number as house_nu3_2_0_,
        person0_.province as province4_2_0_,
        person0_.street as street5_2_0_,
        person0_.zip_code as zip_code6_2_0_,
        person0_.email as email7_2_0_,
        person0_.firstName as firstNam8_2_0_,
        person0_.lastName as lastName9_2_0_,
        person0_.user_pwd as user_pw10_2_0_ 
    from
        Person person0_ 
    where
        person0_.person_id=?
Hibernate: 
    select
        recipe0_.recipe_id as recipe_i1_4_,
        recipe0_.aprox_price as aprox_pr2_4_,
        recipe0_.calories as calories3_4_,
        recipe0_.calories_cat_id as calories4_4_,
        recipe0_.person_id as person_i9_4_,
        recipe0_.raiting as raiting5_4_,
        recipe0_.recipe_name as recipe_n6_4_,
        recipe0_.recipe_cat_id as recipe_c7_4_,
        recipe0_.weight as weight8_4_ 
    from
        recipe_table recipe0_ 
    left outer join
        Person person1_ 
            on recipe0_.person_id=person1_.person_id 
    where
        person1_.person_id=?
Hibernate: 
    select
        person0_.person_id as person_i1_2_0_,
        person0_.apartment_number as apartmen2_2_0_,
        person0_.house_number as house_nu3_2_0_,
        person0_.province as province4_2_0_,
        person0_.street as street5_2_0_,
        person0_.zip_code as zip_code6_2_0_,
        person0_.email as email7_2_0_,
        person0_.firstName as firstNam8_2_0_,
        person0_.lastName as lastName9_2_0_,
        person0_.user_pwd as user_pw10_2_0_ 
    from
        Person person0_ 
    where
        person0_.person_id=?
Hibernate: 
    select
        components0_.recipe_id as recipe_i6_1_0_,
        components0_.component_id as componen1_1_0_,
        components0_.component_id as componen1_1_1_,
        components0_.component_description as componen2_1_1_,
        components0_.component_kcal as componen3_1_1_,
        components0_.component_name as componen4_1_1_,
        components0_.component_weight as componen5_1_1_,
        components0_.recipe_id as recipe_i6_1_1_ 
    from
        component components0_ 
    where
        components0_.recipe_id=?

Main entity class - Recipe has relationship:

@Entity
@Table(name="recipe_table")
public class Recipe{
    @ManyToOne() 
    @JoinColumn(name="person_id")
    @JsonManagedReference
    private Person person; //getters/setters + other fields

}

Second, which is Person entity:

@Entity
public class Person  {

 @OneToMany(mappedBy = "person",fetch = FetchType.EAGER))
 @JsonBackReference
 private List<Recipe> recipes;//getters/setters+other fields

and method to obtain all the recipes of the user, looks:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Person getUserRecipeWithComponents(long id) {
    Person person = findOne(id);
    List<Recipe> recipes = recipeRepository.findByPerson(person);
    person.setRecipes(recipes);

    return person;
}

in RecipeRepository extended by JpaRepository:

List<Recipe> findByPerson(Person person);

And also theres one wierd thing, which i didnt expect i create simple method to retrieve one recipe(only recipe data):

 @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Recipe findRecipeById(long id){
        Recipe recipe = recipeRepository.findOne(id);

        return recipe;
    }

and after this im getting recipies data which contains also user data:

{
recipe_id: 3,
recipeName: "Pasta",
weight: 453,
calories: 1500,
aprox_price: 12,
raiting: 7,
person: {
person_id: 3,
firstName: "Marek",
lastName: "Bawski",
email: "[email protected]",
password: "Karol123",
address: {
street: "Polna",
houseNumber: 4,
apartmentNumber: 0,
zipCode: "13-200",
province: "Pomorskie"
}
},
calories_cat_id: 1,
recipe_cat_id: 2
}

SQL generated by hibernate:

Hibernate: 
    select
        recipe0_.recipe_id as recipe_i1_4_0_,
        recipe0_.aprox_price as aprox_pr2_4_0_,
        recipe0_.calories as calories3_4_0_,
        recipe0_.calories_cat_id as calories4_4_0_,
        recipe0_.person_id as person_i9_4_0_,
        recipe0_.raiting as raiting5_4_0_,
        recipe0_.recipe_name as recipe_n6_4_0_,
        recipe0_.recipe_cat_id as recipe_c7_4_0_,
        recipe0_.weight as weight8_4_0_,
        components1_.recipe_id as recipe_i6_1_1_,
        components1_.component_id as componen1_1_1_,
        components1_.component_id as componen1_1_2_,
        components1_.component_description as componen2_1_2_,
        components1_.component_kcal as componen3_1_2_,
        components1_.component_name as componen4_1_2_,
        components1_.component_weight as componen5_1_2_,
        components1_.recipe_id as recipe_i6_1_2_,
        person2_.person_id as person_i1_2_3_,
        person2_.apartment_number as apartmen2_2_3_,
        person2_.house_number as house_nu3_2_3_,
        person2_.province as province4_2_3_,
        person2_.street as street5_2_3_,
        person2_.zip_code as zip_code6_2_3_,
        person2_.email as email7_2_3_,
        person2_.firstName as firstNam8_2_3_,
        person2_.lastName as lastName9_2_3_,
        person2_.user_pwd as user_pw10_2_3_ 
    from
        recipe_table recipe0_ 
    left outer join
        component components1_ 
            on recipe0_.recipe_id=components1_.recipe_id 
    left outer join
        Person person2_ 
            on recipe0_.person_id=person2_.person_id 
    where
        recipe0_.recipe_id=?

did i mess up with annotations?

i'll be grateful for your help!

0

There are 0 best solutions below