Home Why mapped list is empty?
Reply: 1

Why mapped list is empty?

Turgay Mutlu
1#
Turgay Mutlu Published in 2017-12-07 18:09:22Z

I am new with hibernate and jpa. I need a little help. I'm developing a restful service application with spring boot. Using mysql and hibernate.

When i call my service "ingredients" array will be empty like that. Empty array

In database ingredient table and recipe table has one to many relationship so i tried to do same thing with JPA but i can't find correct way. When I try to get Recipe i can access but ingredient list is always empty.

My Database Design: DB Design

Recipe Entity:

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity()
@Table(name="recipe")
@Getter
@Setter
public class Recipe {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "recipe_id")
    private int recipeId;

    @Column(name ="name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "picture_id")
    private int pictureId;

    @OneToMany(mappedBy = "recipe",fetch = FetchType.EAGER)
    private List<Ingredient> ingredients = new ArrayList<>();


}

Ingredient Entitiy:

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;

@Entity()
@Table(name = "ingredient")
@Getter
@Setter
public class Ingredient implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ingredient_id")
    private int ingredientId;

    @Column(name = "scale")
    private String scale;

    @ManyToOne
    @JoinColumn(name = "recipe_id",nullable = false)
    private Recipe recipe;

    @ManyToOne(cascade =  CascadeType.ALL)
    @JoinColumn(name = "material_id",nullable = false)
    private Material material;

}

RecipeDAO:

import com.mutfak.dolapservice.dao.interfaces.IRecipeDAO;
import com.mutfak.dolapservice.entity.Recipe;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Transactional
@Repository
public class RecipeDAO implements IRecipeDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    @Override
    public List<Recipe> getRecipes() {
        String query = "FROM Recipe ORDER BY recipe_id";

        return (List<Recipe>) entityManager.createQuery(query).getResultList();
    }

    @Override
    public Recipe getRecipeByMaterial(int materialId) {
        return null;
    }

    @Override
    public Recipe getRecipeById(int id) {
        return null;
    }

    @Override
    public void addRecipe(Recipe recipe) {

    }

    @Override
    public void updateRecipe(Recipe recipe) {

    }

    @Override
    public void deleteRecipe(int id) {

    }
}
Turgay Mutlu
2#
Turgay Mutlu Reply to 2017-12-08 22:57:16Z

Finally I find solution. Lombok annotation's and JPA doesn't work correctly. JPA can't get List<> with Lombok.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308106 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO