Search code examples
springspring-bootspring-data-jpaspring-webfluxspring-repositories

Repository not deleting resources in H2 in-memory database


I have set up a simple User model and repository and a controller as follows:

import jakarta.persistence.*;

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "username", unique = true, nullable = false, length = 25)
    private String username;

    @Column(name = "password", nullable = false, length = 255)
    private String password;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;

        this.password = password;
    }
    // overloaded functions include: equals, toString, hashCode.
}
...
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface UserRepository extends JpaRepository<User, Long> {

    @Query(value = "SELECT * FROM users u WHERE u.username = :username", nativeQuery = true)
    User findByUserName(@Param("username") String username);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserRepository userRepository;

    @PostMapping("/new")
    public User routeCreateUser(@RequestBody User user) {
        User newUser = userRepository.save(new User(user.getUsername(), user.getPassword()));
        logger.info("NEW: "+newUser);
        return newUser;
    }

    @DeleteMapping("/forget")
    public User routeDeleteUser(@RequestBody User user) {
        User stored_user = getUser(user.getUsername(), user.getPassword());
        if (stored_user != null) {

            userRepository.delete(stored_user);
//            userRepository.deleteById(stored_user.getId());
//            userRepository.deleteAllById(List.of(stored_user.getId()));
//            userRepository.deleteAll();
            logger.info("DELETE: "+ stored_user);
            return user;
        }
        return null;
    }
}

application.properties

spring.datasource.url=jdbc:h2:mem:db0
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

But for some reason, the deleteById and delete methods do not work at all. The entry/row still persists after deleting.

The deleteAll method works just as expected.

Looking around this kind of problem is usually expected for OneTo[One|Many] relationships. But this example has none.

Did I miss something?

Edit: The solution was to replace long with Long in the User model and other places where it might exist.


Solution

  • The problem was using a primitive type to refer an object type. Change private long id to private Long id;