3

Derived DeleteBy Query does not work. But findBy and delete with @Query works.

I tried to add @Modifying and @Transactional, i changed return type to Long, List<OrderItem>, void.

This is my CrudRepository interface, Order and OrderItem classes:

public interface OrderItemRepository extends CrudRepository<OrderItem, Long> {
    List<OrderItem> findByOrderAndItem_ItemGroup(Order order, ItemGroup itemGroup);

    @Transactional
    Long deleteByOrder(Order order);

    @Modifying
    @Query("delete from OrderItem o where o.order = ?1")
    Integer deleteByOrderQuery(Order order);
}

@Entity
@Table(name = "t_order_items")
public class OrderItem {
    @GeneratedValue
    @Id
    private Integer id;

    @Column(name="itemcount")
    private Integer itemCount;

    @OneToOne()
    @JoinColumn(name="item_id")
    private Item item;

    @ManyToOne
    @JoinColumn(name = "oder_id")
    private Order order;
}

@Entity
@Table(name = "t_orders")
public class Order implements Serializable {
    @GeneratedValue
    @Id
    private Integer id;

    @Column(name = "orderdate")
    private LocalDate orderDate;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, mappedBy = "order")
    private List<OrderItem> orderItems;
}

I have read the documentation http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ but there are nothing special about deleteBy.

Slava Babin
  • 708
  • 2
  • 12
  • 30
  • 1
    Possible duplicate of [Spring Data: "delete by" is supported?](http://stackoverflow.com/questions/23723025/spring-data-delete-by-is-supported) – xenteros Sep 27 '16 at 11:43
  • 1
    I read this and i mentioned that i read the documentations. I know that it must work, but i do not know why it is not working. – Slava Babin Sep 27 '16 at 11:46
  • 1
    Will you please specify that what is "OrderItem" and "Order" in your case – Riddhi Gohil Sep 27 '16 at 13:29
  • 1
    Your question prompted the idea that problem might be in `@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "order") private List orderItems;` and i found this http://stackoverflow.com/a/32745045/3001523 . So if i need CascadeType.ALL i can use only @Query ? – Slava Babin Sep 27 '16 at 13:50

2 Answers2

1

The problem was in CascadeType.ALL. When i changed it to CascadeType.REMOVE everything worked out. The right answer is:

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, mappedBy = "order")
private List<OrderItem> orderItems;
Slava Babin
  • 708
  • 2
  • 12
  • 30
0

In the URL you have provided one can read:

public interface UserRepository extends CrudRepository<User, Long> {

  Long deleteByLastname(String lastname);
  List<User> removeByLastname(String lastname);
}

Your problem might be, that you have wrong return type of the method. Delete method returns the ID of the removed item!

The code might be:

public interface OrderItemRepository extends JpaRepository<OrderItem, Long> {
    List<OrderItem> findByOrderAndItem_ItemGroup(Order order, ItemGroup itemGroup);

    Long deleteByOrder(Order order);

    @Modifying
    @Query("delete from OrderItem o where o.order = ?1")
    Integer deleteByOrderQuery(Order order);
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • 1
    As i said i changed return type to `Long`, `List`, `void` and it doesn't work. – Slava Babin Sep 27 '16 at 11:58
  • Why don't you use `CrudRepository<>`? – xenteros Sep 27 '16 at 11:59
  • @SlavaBabin could you please add more details to your question? Do you receive any error? Do you have other methods in the repo? – xenteros Sep 27 '16 at 12:07
  • 1
    As said in http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ `We also provide persistence technology-specific abstractions like e.g. JpaRepository or MongoRepository. Those interfaces extend CrudRepository and expose the capabilities of the underlying persistence technology in addition to the rather generic persistence technology-agnostic interfaces like e.g. CrudRepository.`. But i did tried to use CrudRepository and nothing has changed. – Slava Babin Sep 27 '16 at 12:08
  • 1
    And as return value one can choose between the number or a list of removed entities. http://stackoverflow.com/a/23731038/3001523 – Slava Babin Sep 27 '16 at 12:09
  • and you have a void. Try removing other methods. Make CrudRepo with `Long deleteByOrder(Order order);` only. No annotations. Tell me what you have then. Try both: `Long` and `Integer`. – xenteros Sep 27 '16 at 12:10