java - Spring Boot JPA - JPA 的错误 JOIN

java - Spring Boot JPA - Wrong JOIN by JPA

提问人:seldonaari 提问时间:7/14/2023 最后编辑:seldonaari 更新时间:7/14/2023 访问量:79

问:

我正在尝试在两个表之间创建关联,例如: 表示可以有多个 Bookings, 预订只能有一个代表

我尝试使用我的 Booking.java 实体中的 Javax 持久性@ManyToOne。

但是,当我尝试访问我的预订视图时,我有一个错误返回:

java.sql.SQLSyntaxErrorException: Unknown column 'representation.booking' in 'on clause'

这个错误让我很敏感,但我不明白为什么它首先尝试调用列 representation.booking(不存在)。

这是我尝试过的:

我的预订内容 :

@Builder
@NoArgsConstructor(force = true)
@AllArgsConstructor
@Entity
public class Booking{

        @Id
        @javax.persistence.Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        Integer id;

        @NotEmpty
        @NotBlank
        String name;

        @NotNull
        @DecimalMin("1")
        Integer number;


        @NotEmpty
        @NotBlank
        @Column(value = "phoneNumber")
        String phoneNumber;

        @NotEmpty
        @Email
        String email;

        @ManyToOne(targetEntity = Representation.class)
        @JoinColumn(name = "fk_representation")
        Representation representation;

My Representation 实体 :

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Representation {

        @Id
        @javax.persistence.Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        Integer id;

        String place;

        String location;

        Integer price;

        String datetime;

我的控制器 :

           @GetMapping("/view")
    public String findAllView(Model model) {
        List<Booking> bookings = repository.findAll();
        model.addAttribute("bookings", bookings);
        return "bookings";
    }

我的预订视图 :

<table id="datatablesSimple">
                            <thead>
                            <tr>
                                <th>Id</th>
                                <th>Nom</th>
                                <th>Email</th>
                                <th>Numéro de téléphone</th>
                                <th>Nombre</th>
                                <th>Evènement</th>
                            </tr>
                            </thead>
                            <tfoot>
                            <tr>
                                <th>Id</th>
                                <th>Nom</th>
                                <th>Email</th>
                                <th>Numéro de téléphone</th>
                                <th>Nombre</th>
                                <th>Evènement</th>
                            </tr>
                            </tfoot>
                            <tbody>
                            <tr th:each="booking : ${bookings}">
                                <td th:utext="${booking.id}"></td>
                                <td th:utext="${booking.name}"></td>
                                <td th:utext="${booking.email}"></td>
                                <td th:utext="${booking.phoneNumber}"></td>
                                <td th:utext="${booking.number}"></td>
                                <td th:utext="${booking.representation.id}"></td>
<!--                                <td th:utext="${ticket.qrCodeSha}"></td>-->
                                <td><a  th:href="@{/admin/booking/delete/{id}(id=${booking.id})}">Delete</a></td>
                            </tr>
                            </tbody>
                        </table>

我的数据库结构:

CREATE TABLE IF NOT EXISTS booking (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    number INTEGER NOT NULL,
    phoneNumber VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    fk_representation INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS ticket (
    id SERIAL PRIMARY KEY,
    boo_id INTEGER NOT NULL,
    qrCodeSha VARCHAR(255) NOT NULL
    );

CREATE TABLE IF NOT EXISTS representation (
    id SERIAL PRIMARY KEY,
    place VARCHAR(255) NOT NULL,
    location VARCHAR(255) NOT NULL,
    price INTEGER NOT NULL,
    datetime VARCHAR(255)
    );

** 查询 JPA 尝试:**

SELECT `booking`.`id` AS `id`, `booking`.`name` AS `name`, `booking`.`email` AS `email`, `booking`.`number` AS `number`, `booking`.`phoneNumber` AS `phoneNumber`, `representation`.`id` AS `representation_id`, `representation`.`price` AS `representation_price`, `representation`.`place` AS `representation_place`, `representation`.`location` AS `representation_location`, `representation`.`datetime` AS `representation_datetime` FROM `booking` LEFT OUTER JOIN `representation` `representation` ON `representation`.`booking` = `booking`.`id`

当然,它返回: #1054 - on 子句中的字段“representation.booking”未知

我希望它发送的查询:

SELECT `booking`.`id` AS `id`, `booking`.`name` AS `name`, `booking`.`email` AS `email`, `booking`.`number` AS `number`, `booking`.`phoneNumber` AS `phoneNumber`, `representation`.`id` AS `representation_id`, `representation`.`price` AS `representation_price`, `representation`.`place` AS `representation_place`, `representation`.`location` AS `representation_location`, `representation`.`datetime` AS `representation_datetime` FROM `booking` LEFT OUTER JOIN `representation` `representation` ON `representation`.`id` = `booking`.`fk_representation`;

其结果:预期查询答案的屏幕截图

你能帮我了解我做错了什么吗?

java spring-boot spring-data-jpa 多对一

评论

0赞 Asgar 7/14/2023
你的桌子在哪里?也是一个保留关键字,尽量避免EventEVENT
0赞 seldonaari 7/14/2023
感谢您的回答!我忘了在我的代码段中添加事件表,我正在添加它,我也在重新尝试更改事件的保留名称
0赞 seldonaari 7/14/2023
嗨,我将事件表更改为“表示”表,问题保持不变。我将使用新的表名更新我的帖子
0赞 Asgar 7/14/2023
你的代码能正常工作吗? 在同一个地方?甚至允许吗?@Id@javax.persistence.Id
0赞 seldonaari 7/14/2023
在尝试将外键放入我的一个表之前,我的 crud 正在工作。如果我尝试删除@Id注释(从导入 org.springframework.data.annotation.Id),我无法删除条目并收到 thjis 错误: java.lang.IllegalStateException:找不到类 com.remyscreve.starmaniaticketing.model.Representation 所需的标识符属性 如果我尝试从 javax 持久性中删除 Id,它不会编译

答: 暂无答案