我刚刚开始探索 Spring Data JDBC。我有一个简单的数据库(postgres),其中包含我要查询的问卷表:CREATE TABLE IF NOT EXISTS foster.questionnaires ( questionnaire_id SERIAL PRIMARY KEY, version varchar(10), name text, title text, description text, status questionnaire_status_types);和 Java 类:(配置灵感来自这里)@SpringBootApplication(scanBasePackages= {"com.app.foster"})@EnableJdbcRepositories("com.app.foster.dal.repositories")public class FosterApplication extends JdbcConfiguration { // Config @Bean NamedParameterJdbcOperations operations() { return new NamedParameterJdbcTemplate(dataSource()); } @Bean PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } @Value("${spring.datasource.url}") private String url; @Bean @ConfigurationProperties("spring.datasource") public HikariDataSource dataSource() { //User name and password is fetched from some other data storage HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setJdbcUrl(url); hikariConfig.setUsername("postgres"); //TODO: abstract these hikariConfig.setPassword("password"); //The data source created here doesn't have connection timeout value //set by me return new HikariDataSource(hikariConfig); } @Bean public ApplicationListener<?> loggingListener() { return (ApplicationListener<ApplicationEvent>) event -> { if (event instanceof RelationalEvent) { System.out.println("Received an event: " + event); } }; } public static void main(String[] args) { SpringApplication.run(FosterApplication.class, args); } }(实体)@Entity@Table(name="questionnaires")public class Questionnaire { @Id Long questionnaire_id; String version; String name; String title; String description; }
2 回答
慕容3067478
TA贡献1773条经验 获得超3个赞
通过一些实验找到了修复:
事实证明我收到了错误
entity is marked @NonNull but is null
因为我试图查询的实体有一个 postgresql 枚举类型列,我用 java 代码中的枚举表示(我猜因此实体没有解析?)...将字段更改为 String 列解决了问题。也许这里的错误信息可以细化...
BIG阳
TA贡献1859条经验 获得超6个赞
我想,你必须为你的实体实现默认构造函数。 https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8.9
添加回答
举报
0/150
提交
取消