尝试 CrudRepository - H2 - TableNotFound

Posted

技术标签:

【中文标题】尝试 CrudRepository - H2 - TableNotFound【英文标题】:Trying CrudRepository - H2 - TableNotFound 【发布时间】:2015-07-21 03:57:53 【问题描述】:

下面是我的配置

Pom.xml

`<dependency>
    <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
 </dependency>`

控制器

@RestController
public class SendSmsController 

    @Autowired
    private DataBaseService dataBaseService;
    @Autowired
    private SendSmsService sendSmsService;

    @RequestMapping(value="/sendSms", method=RequestMethod.POST)
    public ResponseEntity<Sms> sendSms(@RequestBody Sms sms,@RequestParam(required=false) String expirationDate) 

        if(sms.getBody().length() > 160)
            return new ResponseEntity<Sms>(HttpStatus.UNPROCESSABLE_ENTITY);
        

        dataBaseService.saveSms(sms);

        return sendSmsService.sendRequest(sms);
    

数据库服务

@Data
@Service
public class DataBaseService 

    @Autowired
    private SmsRepository smsRepository;

    public void saveSms(Sms sms) 
        //I try smsRepository.save(sms) too
        smsRepository.save(new Sms(sms.getTo(),sms.getTo(),sms.getBody()));
    

短信库

public interface SmsRepository extends CrudRepository<Sms, Long> 


短信类

@Data
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Sms 

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @JsonProperty
    private String from;
    @JsonProperty
    private String to;
    @JsonProperty
    private String body;

    public Sms(String from, String to, String body)
        this.from = from;
        this.to = to;
        this.body = body;
    ;

    public Sms()
    


配置

@Configuration
public class Config 

    @Bean
    public SendSmsService sendSmsService()
        return new SendSmsService();
    

    @Bean
    public DataBaseService dataBaseService()
        return new DataBaseService();
    

当代码到达“smsRepository.save(sms)”时会出现此问题。我遵循本教程https://spring.io/guides/gs/accessing-data-jpa/,但我认为我缺少配置。感谢您的帮助!

又报错了:org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into sms (id, body, from, to) values (null, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

Caused by: org.h2.jdbc.JdbcSQLException: Table "SMS" not found; SQL statement:
insert into sms (id, body, from, to) values (null, ?, ?, ?) [42102-187]

【问题讨论】:

你可以在你的问题中添加 spring config xml 吗? 【参考方案1】:

问题出在我的课堂上。

这项工作之后

public class Sms 

    @Id
    private String id;
    @JsonProperty
    @Column(name="fromNumber")
    private String from;
    @JsonProperty
    @Column(name="toNumber")
    private String to;
    @JsonProperty
    @Column(name="bodyMessage")
    private String body;

【讨论】:

以上是关于尝试 CrudRepository - H2 - TableNotFound的主要内容,如果未能解决你的问题,请参考以下文章

Groovy 和 JPA 阻止 Spring Boot CrudRepository 进行插入

Spring CrudRepository deleteAll()什么都不做

Spring Data CrudRepository 作为方法 Bean

我的自定义存储库实现中的 CrudRepository

无法使用弹簧数据 jpa CrudRepository

Spring CrudRepository .orElseThrow()