Properly deleting comments on a Post in JPA

spring data jpa delete example
spring data jpa bulk delete
hibernate delete not working
jpa repository delete not working
jpa delete and save in same transaction
jpa delete exceptions
delete and insert jpa
hibernate delete by id

I have a front-end application with a JPA/JAX-RS backend, where i want to make a request to delete a comment on a post.

I'm using the repository pattern, where i have made the following method to delete a comment:

public Comment delete(Comment comment)
return getEntityManager().createQuery("DELETE Comment c WHERE = :id ", Comment.class)
        .setParameter("id", comment.getId())

here i get the error:

Update/delete queries cannot be typed JPA

Here I'm a bit in doubt of what to do, i could just change the Comment.class and make it a generic query, but inside my facade, which calls the method

I also want to make a conditional check i there was a comment or not

 Comment commentToDelete = commentRepository.get(comment);
        if(commentToDelete == null){
            throw new ResourceNotFoundException(Comment.class, comment);

how can i solve this problem in the easiest way, can i return an Object and just cast that, or is there another way to check for the comment and delete it from the database?

When you create a DELETE OR UPDATE query, you should always use executeUpdate method

getEntityManager().createQuery("DELETE FROM Comment c WHERE = :id ", Comment.class)
        .setParameter("id", comment.getId())

The return type of this method is an integer telling you how many rows within your table have been affected

Delete Not Working with JpaRepository, I had the same problem. Perhaps your UserAccount entity has an @OneToMany with Cascade on some attribute. I've just remove the cascade,  Persisting and deleting objects in JPA requires a transaction, that's why we should use a @Transactional annotation when using these derived delete queries, to make sure a transaction is running. This is explained in detail in the ORM with Spring documentation. 5. Custom Delete Query

You can omit the conditional check because as per getSingleResult() documentation if there is no result it will throw NoResultException, you can just catch that exception and re-throw yours.

So solution would look something like this:

try {
    Comment deletedComment = commentRepository.delete(comment);
} catch (NoResultException nre) {
    throw new ResourceNotFoundException(Comment.class, comment)

Also make sure to add FROM to your JPQL query as Alien said.

Edit: I think you should call query.executeUpdate() rather than calling getSingleResult();

Updating POST and DELETE methods on UserResource to use JPA , Status: 200 OK denotes that record has been deleted successfully. Again send a DELETE request with the same URL http://localhost:8080/jpa/users/1. It returns a​  JPA Criteria API bulk update delete is a great feature that allows you do build bulk update and delete queries using the JPA 2.1 Criteria API support via CriteriaUpdate and CriteriaDelete. Because one of our community members asked me on the Hibernate forum about this topic , I decided it is a good opportunity to write about this lesser-known

You missed FROM in the query.

Change query to this DELETE FROM Comment c WHERE = :id by specifying FROM

Refer this

The best way to soft delete with Hibernate, While most of the time, deleting a record is the best approach, there are times The StackOverflow Posts table has a ClosedDate column which acts as a entityManager.persist(javaTag);. Tag jpaTag = new Tag();. jpaTag. When fetching the comments collection, Hibernate executes the following query:  This tutorial shows how we can use @Query to delete entities using a single JPQL query. Spring Data JPA Query method deleteBy approach (see Spring Data JPA – DeleteBy Example) do it in two steps (It first fetches the entities and then call delete on the fetched data to delete the data) 1) Dependencies. This examples needs the following

How to batch DELETE statements with Hibernate, Learn how the JDBC batch delete works when using JPA and Hibernate, and we are The Post entity has a one-to-many association to a Comment and a one-to-​one This time all DELETE statements are properly batched:  The Post entity has a one-to-many association to a Comment and a one-to-one relationship with the PostDetails entity: @OneToMany( mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true ) private List<Comment> comments = new ArrayList<>(); @OneToOne( mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true ) private PostDetails details;

JPA delete entity example - JPA tutorial, It will return successfully whether the department exists or not. 2. JPA delete multiple entities with JPQL Query. Another way to remove entity is  The following code example show you how to delete or remove entity object from database using JPA. The first class that we are going to create is ArtistDaoImpl which implements ArtistDao . This DAO class handles the delete process either by the entity Id or by the entity object itself.

JPA implementation patterns: Removing entities, post-date April 9th Just like retrieving an entity, removing an entity is pretty simple. In the meantime, please drop any remarks in the comment section below. @Colin: Have you set your transaction boundaries correctly? Introduction JPA translates entity state transitions to database DML statements. Because it’s common to operate on entity graphs, JPA allows us to propagate entity state changes from Parents to Child entities. This behavior is configured through the CascadeType mappings. JPA vs Hibernate Cascade Types Hibernate supports all JPA Cascade Types and some additional legacy cascading styles. The

  • It still gives the same error, do i need to make it a generic query?