why remove function of Entitymanager doesn't remove the object by id?

Viewed 29

Basically what am trying to do is delete the object containing nick_name and userType by id using this request http://localhost:8080/messenger/api/user/1 class Entity

@XmlRootElement
@Entity
@Getter
@Setter
@Table(name="user_message")
public class User implements Serializable{

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

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

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


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id == user.id && Objects.equals(nick_name, user.nick_name) && Objects.equals(userType, user.userType);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, nick_name, userType);
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", nick_name='" + nick_name + '\'' +
                ", userType='" + userType + '\'' +
                '}';
    }
}

class Registrator

 @Stateless
    public class UserRegistrator {
    
        @Inject
        private Logger logger;
    
        @Inject
        private EntityManager entityManager;

         public User delete (long id){
         logger.info("User deleted by " + id);
         entityManager.remove(entityManager.find(User.class,id));
            return item;
        }

class UserRest

    @DELETE
    @Path("/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
        public User deleteUserById(@PathParam("id") long id , User item){
       return userRegistrator.delete(id,item);
    }

If you check in class Registrator you will notice i used entityManager.remove(entityManager.find(User.class,id)); which means once when this function is used it will search for the object with the id given inside the database and instantly removes the object but it doesn't work this way what am missing here ? Thanks in advance!

0 Answers
Related