【发布时间】:2021-08-03 07:23:39
【问题描述】:
我使用 WebFlux 响应式模块、H2 内存数据库和 R2DBC 响应式驱动程序创建了 Java Spring Boot 服务。这在端口 8081 上构建并运行良好。
我在 main/resources 下添加了一个 schema.sql 文件,其中包含以下内容:
CREATE TABLE contentitem ( contentItemId INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, localizedName VARCHAR(100) NOT NULL);
我用同一目录中的 data.sql 文件填充表:
INSERT INTO contentitem (contentItemId, localizedName) VALUES (0, 'Zero');
INSERT INTO contentitem (contentItemId, localizedName) VALUES (1, 'One');
INSERT INTO contentitem (contentItemId, localizedName) VALUES (2, 'Two');
我的 ContentItem 模型是:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table("contentitem")
public class ContentItem {
@Id
private Integer contentItemId;
private String localizedName;
我的 ContentItemController 是:
@RestController
@RequestMapping("/contentItems")
public class ContentItemController {
@Autowired
private ContentItemService contentItemService;
@GetMapping("/{contentItemId}")
public Mono<ResponseEntity<ContentItem>> getContentItemByUserId(@PathVariable Integer contentItemId){
Mono<ContentItem> contentItem = contentItemService.getContentItemById(contentItemId);
return contentItem.map( u -> ResponseEntity.ok(u))
.defaultIfEmpty(ResponseEntity.notFound().build());
}
我的 ContentItemService 是:
@Service
@Slf4j
@Transactional
public class ContentItemService {
@Autowired
private ContentItemRepository contentItemRepository;
public Mono<ContentItem> getContentItemById(Integer contentItemId){
return contentItemRepository.findByContentItemId(contentItemId);
}
}
我的 ContentItemRepository 是:
public interface ContentItemRepository extends ReactiveCrudRepository<ContentItem,Integer> {
Mono<ContentItem> findByContentItemId(Integer contentItemId);
}
当我使用 http://localhost:8081/contentItems/1 调用正在运行的服务时,我在 GET 上收到 500 服务器错误,并在日志中显示以下内容:
org.springframework.data.r2dbc.BadSqlGrammarException: executeMany; bad SQL grammar [SELECT contentitem.content_item_id, contentitem.localized_name FROM contentitem WHERE contentitem.content_item_id = $1]; nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [42122] [42S22] Column "CONTENTITEM.CONTENT_ITEM_ID" not found; SQL statement:
几个问题:
- 为什么我的简单、明确、一致的列名(例如“contentItemId”)会被分解成带有下划线的版本(例如“CONTENT_ITEM_ID”)?只是为了咯咯笑,我尝试在 'contentItemId' 数据成员上方添加一个 '@Column("content_item_id")' 注释,但得到了相同的结果。
- 为什么找不到“contentItemId”(或“CONTENT_ITEM_ID”)列?
使这一切复杂化的是,我在 application.properties 文件中使用 spring.h2.console.enabled=true 启用的 H2 控制台在我使用 http://localhost:8081/h2-console 调用它时失败并出现 404 Not Found 错误。
【问题讨论】:
标签: java spring-boot h2 spring-webflux r2dbc