@Formula依靠ManyToMany

时间:2019-04-24 15:23:23

标签: spring-boot spring-data-jpa

我试图在我的Song实体上添加一个like计数字段,但是由于我不太精通SQL,所以我不断收到语法错误。

我的like系统可以运行,但是我想在歌曲视图中显示计数。它应以歌曲ID在“ user_likes_song”表中出现的次数计数。

  

错误:“ SELECT”处或附近的语法错误         位置:295

公式注释:

@Entity
@Data
@NoArgsConstructor
@Table(name = "song")
public class Song {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false, columnDefinition = "serial")
    private Long id;

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

    @Column(name = "author", nullable = false)
    private String author;

    @Column(name = "content", nullable = false)
    private String content;

    @Formula("SELECT COUNT(i.id) FROM user_likes_song i WHERE song_id = i.id")
    private long likeCount;

    @ManyToOne
    @JoinColumn(name = "band_id", nullable = false)
    private Band band;

表格:

    CREATE TABLE IF NOT EXISTS user_acc (
      id SERIAL NOT NULL PRIMARY KEY,
      username text NOT NULL UNIQUE,
      password text NOT NULL,
      first_name text NULL,
      last_name text NULL,
      age INT NULL,
      phone text NULL,
      email text NOT NULL UNIQUE,
      status active_status NOT NULL DEFAULT 'active',
      create_date TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
      update_date TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL
    );

    -- -----------------------------------------------------
    -- Table song
    -- -----------------------------------------------------
    CREATE TABLE IF NOT EXISTS song (
      id SERIAL NOT NULL PRIMARY KEY,
      name text NOT NULL,
      author text NULL,
      content text NOT NULL,
      status song_status NOT NULL DEFAULT 'inactive',
      create_date TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
      update_date TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
      band_id integer NOT NULL,
      user_id integer NOT NULL,
      CONSTRAINT fk_song_band
        FOREIGN KEY (band_id)
        REFERENCES band (id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT fk_song_user1
        FOREIGN KEY (user_id)
        REFERENCES user_acc (id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION);

    -- -----------------------------------------------------
    -- Table user likes song
    -- -----------------------------------------------------

    CREATE TABLE IF NOT EXISTS user_likes_song (
      id SERIAL NOT NULL PRIMARY KEY,
      user_id integer NOT NULL,
      song_id integer NOT NULL,
      CONSTRAINT fk_user_likes_song_user1
        FOREIGN KEY (user_id)
        REFERENCES user_acc (id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT fk_user_likes_song_song1
        FOREIGN KEY (song_id)
        REFERENCES song (id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION);

编辑:

通过添加括号并将表连接在一起来解决此问题:

@Formula("(SELECT COUNT(s.id) FROM user_acc u INNER JOIN user_likes_song us on u.id = us.user_id " +
            "INNER JOIN song s on us.song_id = s.id WHERE us.song_id = s.id )")
    private Long likeCount;

1 个答案:

答案 0 :(得分:1)

您的问题是由于hibernate @Formula上的语法错误所致,所以让我深入解释一下,当您对公式进行查询时,在Hibernate中添加了一个子查询,当查找歌曲时执行以下查询,例如:

  

选择ID,名称,作者,内容,   (选择COUNT(i.id)FROM user_likes_song我在哪里song_id = i.id)作为公式FROM ....

然后,您需要在子查询中添加括号

@Formula("(SELECT COUNT(i.id) FROM user_likes_song i WHERE song_id = i.id)")
private long likeCount;