提问人:seldonaari 提问时间:7/14/2023 最后编辑:seldonaari 更新时间:7/14/2023 访问量:79
java - Spring Boot JPA - JPA 的错误 JOIN
java - Spring Boot JPA - Wrong JOIN by JPA
问:
我正在尝试在两个表之间创建关联,例如: 表示可以有多个 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`;
其结果:预期查询答案的屏幕截图
你能帮我了解我做错了什么吗?
答: 暂无答案
评论
Event
EVENT
@Id
@javax.persistence.Id