Java:20.0
Springboot:3.0.1
表结构
posts | CREATE TABLE `posts` (
`id` binary(16) NOT NULL,
`posts` varchar(5000) DEFAULT NULL,
`created_at` datetime(6) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`user_id` binary(16) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK8omq0tc18jd43bu5tjh6jvra7taq` (`user_id`),
CONSTRAINT `FK8omq0tc18jd43bu5tjh6jvra7taq` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
post_reports
post_reports | CREATE TABLE `post_reports` (
`id` binary(16) NOT NULL,
`reported_at` datetime(6) DEFAULT NULL,
`reported_by` varchar(255) DEFAULT NULL,
`reported_reason` varchar(255) DEFAULT NULL,
`updated_at` datetime(6) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
`post_id` binary(16) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKj9so403o1bquehozscdqxpyijjma` (`post_id`),
CONSTRAINT `FKj9so403o1bquehozscdqxpyijjma` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
查询
@Query(
value =
"SELECT DISTINCT p.* FROM posts AS p LEFT JOIN post_reports AS pr ON p.id=pr.post_id WHERE (pr.reported_by IS NULL OR pr.reported_by!=:reportedBy) order by p.created_at",
countQuery =
"SELECT COUNT(DISTINCT p.*) FROM posts AS p LEFT JOIN FETCH post_reports AS pr ON p.id= pr.post_id WHERE (pr.reported_by IS NULL OR pr.reported_by!=:reportedBy)",
nativeQuery = true)
Page<Post> findAllByStatusTrueAndReportedByNullOrReportedByNot(
@Param("reportedBy") String reportedBy, Pageable pageable);
*这会产生未知字段
p.createdAt.*
错误。因为在表中名称是 created_at
它无法识别该列。
请分享您的实体类以获得更好的答案。
您需要设置以下任意一项
作为以下示例中显示的属性或通过创建自定义策略。
spring:
jpa:
hibernate:
naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl