序列不存在,hibernate和JPA 2.1

时间:2018-04-18 16:33:45

标签: spring-boot jpa spring-data-jpa

我收到错误说

`Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist`

当我尝试创建用户时会发生此错误。

  @RequestMapping(method = POST)
    public UserDto createUser(@RequestBody userDto user) {
        Preconditions.checkNotNull(user);

        return Preconditions.checkNotNull(service.create(user));
    }

但我可以deleteget而不是createupdate。同样令人沮丧的是,在尝试update时我没有收到任何错误,但事实并非如此。

我没有真正领先于哪里看。我已经尝试了许多不同的方法来解决这个问题,但没有用。

我发现了一篇帖子:

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQUENCE1")
@SequenceGenerator(name="SEQUENCE1", sequenceName="SEQUENCE1", allocationSize=1)
private int user_id;

点击此链接:SOF link

它抱怨我用netbeans生成的这个实体,我目前正在使用Intellij。任何意见,将不胜感激。

3 个答案:

答案 0 :(得分:3)

创建新Campaign实体的代码似乎不正确。

public CampaignDto create(CampaignDto campaignDto) {
    Campaign campaign = mapper.mapReverse(campaignDto);

    System.out.println(campaign.toString());

    // Following 2 lines must be added to obtain and use managed Shop entity
    Shop existingShop = shopRepository.findOne(campaignDto.getShopId());
    campaign.setShop(existingShop);

    campaign = campaignRepository.save(campaign);
    CampaignDto createdCampaign = mapper.map(campaign);

    return createdCampaign;
}

答案 1 :(得分:1)

看起来您在创建新Campaign.shopId时可能没有设置Campaign字段。

@JoinColumn(name = "SHOP_ID", referencedColumnName = "SHOP_ID")
@ManyToOne(optional = false)
private Shop shopId;

您可能希望将此字段重命名为shop,以明确其所包含的内容,因为它不仅仅是标识符。

根据您保存新对象的方式,您可能需要在CascadeType.ALL上添加@ManyToOne,以确保新Shop与新Campaign一起保留。< / p>

@ManyToOne(optional = false, cascade = CascadeType.ALL)

答案 2 :(得分:1)

转到您的应用程序属性文件并放入 hibernate.hbm2ddl.auto = true; Hibernate创建此序列并添加新行可能会有所帮助