PostgreSQL:错误:运算符不存在:整数=记录-Spring JPA数据

时间:2019-07-09 17:20:31

标签: java postgresql hibernate spring-data-jpa spring-data

我正在创建一个简单的应用程序。它包括3个表-其中2个-多对多。这表示类别 WordConfig Word 。下面,我将向您展示这些类的模型。

Category.java

@Entity
@Table(name = "categories")
@NoArgsConstructor
@Data
@AllArgsConstructor
public class Category {

    public interface CategoryValidation {}

    @Id
    @Column(name = "id_category")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    private int id;

    @Column(name = "name")
    @NotBlank(groups = {CategoryValidation.class})
    @Length(min = 1, max = 30, groups = {CategoryValidation.class})
    @Pattern(regexp = "^[a-zA-Z0-9_'/ ]+$", groups = {CategoryValidation.class})
    private String name;

    @Column(name = "mode")
    @NotNull(groups = {CategoryValidation.class})
    @Enumerated(EnumType.STRING)
    private Mode mode;

    @Column(name = "id_parent")
    private int idParent;

    @Column(name = "defaultCategory", updatable = false)
    private boolean defaultCategory;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_test")
    @JsonIgnore
    @ToString.Exclude
    private Test test;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories", cascade = CascadeType.ALL)
    private List<WordConfig> wordConfigs = new ArrayList<>();
}

WordConfig.java

@Entity
@Table(name = "wordConfigs")
@NoArgsConstructor
@Data
@AllArgsConstructor
public class WordConfig {
    public interface WordConfigValidation {}

    @Id
    @Column(name = "id_wordConfig")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    private int id;

    @Column(name = "done")
    private boolean done;

    @Column(name = "today")
    private boolean today;

    @Column(name = "repeated")
    private int repeated;

    @Column(name = "thisUnit")
    private boolean thisUnit;

    @Column(name = "dateTimeDone", updatable = false)
    private Timestamp dateTimeDone;

    @Column(name = "processNo")
    private int processNo;

    @Column(name = "lastPerfectNum")
    private int lastPerfectNum;

    @Column(name = "lastPerfect")
    private boolean lastPerfect;

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

    @Column(name = "mechanism")
    @NotNull(groups = {WordConfigValidation.class})
    @Enumerated(EnumType.STRING)
    private Mechanism mechanism;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "CategoryWordConfig",
            joinColumns = { @JoinColumn(name = "id_wordConfig") },
            inverseJoinColumns = { @JoinColumn(name = "id_category") }
    )
    List<Category> categories = new ArrayList<>();

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "wordConfig",cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    private List<Word> words;

}

Word.java

@Entity
@Table(name = "words")
@NoArgsConstructor
@Data
@AllArgsConstructor
public class Word {

    public interface WordValidation {}

    @Id
    @Column(name = "id_word")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    private int id;

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

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

    @Column(name = "seat")
    private int seat;

    @Column(name = "toSpeech")
    private boolean toSpeech;

    @Column(name = "answer")
    private boolean answer;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_wordConfig")
    @JsonIgnore
    @ToString.Exclude
    private WordConfig wordConfig;
}

为了描述我的问题,我必须向您展示WordConfig的存储库: WordConfigRepository.java

@Repository
public interface WordConfigRepository extends CrudRepository<WordConfig, Long> {

    List<WordConfig> findWordConfigsByCategories(List<Category> categories);
}

最后一个-使用我提供的存储库方法的服务:(略有简化)

public void testWithCategoryAndWords(Test test) {
    List<Category> categories = new ArrayList<>();

    //1. categories.add(test.getCategories().get(1));
    //2. categories.add(test.getCategories().get(2));

    wordConfigRepository.findWordConfigsByCategories(categories);
}

它如何工作,有什么问题? 我从测试中获取类别,将其添加到创建的列表中,然后使用存储库方法获取wordConfig列表。当我仅取消注释行之一时,一切正常。当我取消对两个提示行的注释时,就会出现问题。这是错误:

org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = record
  No operator matches the given name and argument types. You might need to add explicit type casts.
  Pozycja: 693
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143) ~[postgresql-42.2.5.jar:42.2.5]
...

编译器显示类型转换存在一些问题,但是为什么它只能处理类别数组列表中的一个元素,却不能处理2个或更多元素?我应该尝试使用本地查询创建一些自定义@Query吗?我的代码有问题吗?

0 个答案:

没有答案