spring-data-jpa JPA错误:时间表实体星期二关系不存在

slmsl1lt  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(181)

我正在使用Sping Boot 创建一个学校日记应用程序,遇到了一个小问题。当我尝试向数据库添加一些数据时,JPA显示了这样的错误:

ERROR: relation "timetable_entity_tuesday" does not exist

我不知道为什么会这样,因为我把所有的设置都给了我的实体,它应该在那里寻找表。
这是我的代码。

时间表实体

@Entity
@Table(name = "timetables", schema = "working_schema")
public class TimetableEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Getter @Setter
    private long id;

    @Getter @Setter @ElementCollection
    private List<String> monday;

    @Getter @Setter @ElementCollection
    private List<String> tuesday;

    @Getter @Setter @ElementCollection
    private List<String> wednesday;

    @Getter @Setter @ElementCollection
    private List<String> thursday;

    @Getter @Setter @ElementCollection
    private List<String> friday;

    @Getter @Setter @ElementCollection
    private List<String> saturday;

    @Getter @Setter @ElementCollection
    private List<String> sunday;

    @OneToOne(mappedBy = "timetable")
    @Getter @Setter
    private ClassEntity schoolClass;

    public TimetableEntity() {
    }
}

时间表存储库

public interface TimetableRepository extends JpaRepository<TimetableEntity, Long> {
}

方法,该方法将数据添加到数据库,存储在服务中:

public TimetableEntity addTimetable(TimetableAddModel timetableData) throws SubjectNotFoundException {
        TimetableEntity timetable = new TimetableEntity();
        timetable.setTuesday(Arrays.asList("math"));
        timetableRepo.save(timetable);
        return timetable;
}

下面是“Hibernate如何进行查询:

Hibernate: 
    insert 
    into
        timetable_entity_tuesday
        (timetable_entity_id, tuesday) 
    values
        (?, ?)

它尝试向timetable_entity添加数据,尽管timetables表已提供给实体。
这是我的数据库:

如果你知道有什么问题,请告诉我,我会非常感激的!

2wnc66cl

2wnc66cl1#

JPA 2.0@ElementCollection和@CollectionTable注解允许储存Java类型的集合。请尝试修改Map,将@CollectionTable注解新增至您的实体。

@Entity
@Table(schema = "working_schema")
public class Timetables {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Getter @Setter
    private long id;

    @Getter @Setter @ElementCollection @CollectionTable(name = "Monday")
    private List<String> monday = new ArrayList<>();

    @Getter @Setter @ElementCollection @CollectionTable(name = "Tuesday")
    private List<String> tuesday = new ArrayList<>();
    ...
}
insrf1ej

insrf1ej2#

我想,Hibernate的查询应该像Hibernate一样:

insert into timetables (id, Monday, Tuesday) 
values (?, ?, ?);

希望能帮上忙!

相关问题