1

I have some relations like the following:

@Entity
@Table(name="customer")
public class Customer
{
    @Id
    @Column(name="id")
    private int id;


    @OneToMany(fetch=FetchType.EAGER, mappedBy="customer", cascade=CascadeType.ALL)
    private List<OrderedItem> items;

    public List<OrderedItem> getItems ()
    {
        return items;
    }

    public void setItems (List<OrderedItem> _items)
    {
        items = _items;
    }
}

@Entity
@Table(name="ordereditem")
@IdClass(OrderedItemPK.class)
public class OrderedItem
{
    @Id
    @ManyToOne
    @JoinColumn(name="id_customer")
    private Customer customer;

    @Id
    @ManyToOne
    @JoinColumn(name="id_item")
    private Item item;

    public Customer getCustomer ()
    {
        return customer;
    }
    public void setCustomer (Customer _cust)
    {
        customer = _cust;
    }
    public Item getItem ()
    {
        return item;
    }
    public void setItem (Item _item)
    {
        item = _item;
    }
}

@Entity
@Table(name="item")
public class Item
{
    @Id
    @Column(name="id")
    private int id ;

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="itemcategory",
        joinColumns=
            @JoinColumn(name="id_item", referencedColumnName="id"),
        inverseJoinColumns=
            @JoinColumn(name="id_category", referencedColumnName="id")
    )
    private List<Category> categories ;



    public int getId ()
    {
        return id;
    }

    public void setId (int _id)
    {
        id = _id;
    }

    public List<Categories> getCategories ()
    {
        return categories;
    }

    public void setCategories (List<Category> _categories)
    {
        categories = _categories;
    }
}

@Entity
@Table(name="category")
public class Category
{
    @Id
    @Column(name="id")
    private int id;

    public int getId ()
    {
        return id;
    }

    public void setId (int _id)
    {
        id = _id;
    }
}

public class OrderedItemPK implements Serializable
{
    private int customer ;
    private int item ;

    public int getCustomer ()
    {
        return a;
    }
    public void setCustomer (int _cust)
    {
        customer = _cust;
    }
    public int getItem ()
    {
        return item;
    }
    public void setItem (int _item)
    {
        item = _item;
    }

    public int hashCode() {
        return customer + item;
    }

    public boolean equals(Object _o) {
        OrderedItemPK other = (OrderedItemPK) _o;
        return (customer == other.customer) && (item == other.item);
    }
}

The repository:

public interface CustomerRepo extends JpaRepository<Customer, Integer> {
}

the service:

@Service
@Transactional
public class CustomerSvc
{
    @Autowired
    CustomerRepo repo;

    public Customer findCustomer(int _id) {
        return repo.findOne(_id);
    }

    public List<Customer> findAllCustomer() {
        return repo.findAll();
    }
}

Here is the content in the database:

customer table:

id
-----
1
2
3

ordereditem table:

id_customer     id_item
-------------------------
1                1
1                2
1                3

item table:

id
-----
1
2
3

category table:

id
----
1
2
3

itemcategory table:

id_item     id_category
-----------------------
1            1
1            2
1            3
2            1
2            2
2            3
3            1
3            2
3            3

How I use the service:

Customer c = custSvc.findOne(1);

// this prints Number of ordered items: 9
System.out.println("Number of ordered items: " + c.getItems().size());

List<Customer> custs = custSvc.findAll();
c = custs.get(0);

// this prints Number of ordered items: 3
System.out.println("Number of ordered items: " + c.getItems().size());

The correct amount should be 3, a customer with the id 1 has 3 OrderedItems, these 3 OrderedItems have 1 Item for each of them and every Item has 3 categories.

I wonder why it only gives the wrong children if I use method like findOne from the Spring Data and find method from the EntityManager, it correctly gives the right children if I use method like findAll or findByAttribute from the Spring Data.

Mideel
  • 801
  • 7
  • 16

2 Answers2

1

I have found the answer for similar case in stackoverflow, but its not easy to find it because I needed to pass through around 40 results to get the answer in here that I was looking for. I hope the others who have similar problem can find this posting helpful.

JPA - EntityManager find method returns duplicates

Mideel
  • 801
  • 7
  • 16
0

remove fetch=FetchType.EAGER from @OneToMany in Customer entity. Rather use @Transactional where you want to fetch child records

Hamza Safdar
  • 113
  • 5