0

I am trying to soft delete row from a database by id. When I do DELETE localhost:8080/id (using postman) I get sql exception: org.postgresql.util.PSQLException: ERROR: column "deleted_at" is of type bigint but expression is of type timestamp with time zone

My RequestMapping looks like:

@RequestMapping (value = "/{id}", method = RequestMethod.DELETE)
public String delete (@PathVariable Long id){
    Class class = classRepository.findOne(id);
    class.setDeletedAt(System.currentTimeMillis()/1000);
    classRepository.delete(class);

    return "\"OK\"";
}

Entity:

@Entity
@SQLDelete(sql = "UPDATE class SET deleted_at = now() where id = ?")
@Where(clause="deleted_at is NULL")
public class Class extends SoftDeleteableEntity {


private String name;
private String address;

// setters and getters
}

SoftDelete:

public abstract class SoftDeleteableEntity extends BaseEntity {

protected Long deletedAt;

// setters and getters
}

BaseEntity:

@MappedSuperclass
public abstract class BaseEntity implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public void setId(Long id) {
    this.id = id;
}
} 

Class Repository interface extends JpaRepository with no code and @Component annotation. ClassService class is with @Service annotation that only @Autowires repository.

I have tried using classRepository.save(class) instead of delete but then i can't get the data to be saved to database.

I have been searching the forums for a solution but can't seem to find one. Any help or tips would be appreciated.

P.S. I'm new to programming (learning spring for 2 weeks) and I'm sorry if I have missed some existing answer on stackoverflow or asked something the wrong way.

Wocha
  • 33
  • 7
  • 1
    The error message is clear: you can not assign a timestamp (which is what `now()` returns) to an integer value (`long`). If `deleted_at` should store the point in time when the row was deleted, define it as `timestamp` in the database, not as `long` –  Apr 18 '17 at 08:55
  • Thank you for the answer. Changing delete_at from bigint to timestamp is not an option for me. What i really need to get done is have the int value of timestamp saved to database column delete_at when delete is called. I initially tried using classRepo.save(class), which inserted the int value to delete_at, but did not save the data to database (also tried saveAndFlush). – Wocha Apr 18 '17 at 08:58
  • Why isn't that an option? Using an integer for a timestamp is not a good idea. But if you insist on using the wrong data type see here: http://stackoverflow.com/q/27740363/330315 –  Apr 18 '17 at 08:59
  • That worked great, thank you! – Wocha Apr 18 '17 at 09:13

0 Answers0