将软删除记录视为已删除,除非按ID查询

时间:2017-10-17 01:34:53

标签: hibernate jpa

如何在按ID查询记录时不应用@Where子句,以便我可以检索软删除的记录?

删除某个用户后,它会在其删除的字段中存储时间戳。我们希望将软删除的用户视为通常删除的记录,除非在端点中通过其ID查询它。 (GET / users /:Id)。

当我们查询所有用户时,当前代码已经解决了不应该返回已删除记录的要求之一。但是当GET / users /:Id

查询时,这不会返回软删除的记录

实体

POST

控制器

<!DOCTYPE html>
<html>

<head>
  <title>User Media Recording</title>
</head>

<body>
  <input type="button" value="Start/resume recording audio" id="start">
  <input type="button" value="Pause recording audio" id="pause">
  <input type="button" value="Stop recording audio" id="stop">
  <script>
    navigator.mediaDevices.getUserMedia({
        audio: true
      })
      .then(stream => {
        const recorder = new MediaRecorder(stream);

        recorder.ondataavailable = async(e) => {
          if (stream.active) {
            try {
              const blobURL = URL.createObjectURL(e.data);
              const request = await fetch(blobURL);
              const ab = await request.arrayBuffer();
              // do stuff with `ArrayBuffer` of recorded audio
              console.log(blobURL, ab);
              // we do not need the `Blob URL`, we can revoke the object
              // URL.revokeObjectURL(blobURL);
            } catch (err) {
              throw err
            }
          }
        }
        recorder.onpause = e => {
          console.log("recorder " + recorder.state);
          recorder.requestData();
        }

        stream.oninactive = () => {
          console.log("stream ended");
        }

        document.getElementById("start")
          .onclick = () => {

            if (recorder.state === "inactive") {
              recorder.start();
            } else {
              recorder.resume();
            }
            console.log("recorder.state:", recorder.state);
          }

        document.getElementById("pause")
          .onclick = () => {

            if (recorder.state === "recording") {
              recorder.pause();
            }
            console.log("recorder.state:", recorder.state);
          }

        document.getElementById("stop")
          .onclick = () => {

            if (recorder.state === "recording" || recorder.state === "paused") {
              recorder.stop();
            }

            for (let track of stream.getTracks()) {
              track.stop();
            }

            document.getElementById("start").onclick = null;
            document.getElementById("pause").onclick = null;
            console.log("recorder.state:", recorder.state
            , "stream.active", stream.active);
          }

      })
      .catch(err => {
        console.error(err)
      });
  </script>
</body>

</html>

存储库

@Entity(name = "user")
@Where(clause = "deleted IS NULL")
public class User {

    @Id
    @GeneratedValue
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    //getters and setters
}

1 个答案:

答案 0 :(得分:0)

我建议你阅读一下hibernate中的过滤器。在那里你可以有条件你要么返回用户将被删除的ID。 让我分享一下代码。

    @Before("@annotation(com.talentica.talentpool.core.applicant.aop.Applicant)")
public void enableUserFilter() {
Session session = (Session) entityManager.getDelegate();
if (session.isConnected() && yourCondition) {
    session.enableFilter("userFilter");
}

并将其应用于您的用户实体

@FilterDef(name = "userFilter")
@Filters({ @Filter(name = "userFilter", condition = "is_deleted = null") })
public class User implements Serializable {
}

你可以这样使用它,它必须帮助你。