Spring Boot 无效数据访问资源使用异常:org.hibernate.exception.SQLGrammarException:

bttbmeg0  于 2022-11-23  发布在  Spring
关注(0)|答案(2)|浏览(179)

我有以下问题我已经卡住了一段时间:
我得到这个错误:数据访问资源使用异常:无法准备语句; SQL [调用休眠序列的下一个值];嵌套的异常错误为org. hib。无法准备语句***
我发现有这个错误的人得到它,因为他们使用保留字的表名,但我不认为这是我的问题。
我的两个模型类如下所示。

@Entity
@Table(name = "GATEWAY_MODEL")
public class GetewayModel implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "serial_number", nullable = false, length = 12, updatable = true)
    private String serialNumber;
    @Column(name = "name", nullable = false, length = 12, updatable = true)
    private String name;
    @Column(name = "ipFour", nullable = false, length = 12, updatable = true)
    private String ipFour;
    @Column(name = "peripheral_devices", updatable = true)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "gateway")
    private Set<PeripheralDevicesModel> peripheralDevices = new HashSet<PeripheralDevicesModel>();

@Entity
@Table(name = "PERIPHERAL_DIVICES_MODEL")
public class PeripheralDevicesModel {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "uID", nullable = false)
    private String uID;
    @Column(name = "vendor", nullable = false)
    private String vendor;
    @Column(name = "date_created", nullable = false)
    private Date dateCreated;
    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private Status status;
    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "gateway")
    private GetewayModel gateway;

然后在主类中我试着放一些这样的数据:

@Bean
CommandLineRunner initDatabase(GatewayRepository gatewayRepo, PeripheralDevicesRepository devicesRepo) {

    Set<PeripheralDevicesModel> devicesSet = new HashSet<>();

    GetewayModel gateway = new GetewayModel();
    gateway.setId(123l);
    gateway.setSerialNumber("1123");
    gateway.setName("gateway");
    gateway.setIpFour("1.160.10.240");
    
    PeripheralDevicesModel devices = new PeripheralDevicesModel();
    devices.setId(1234l);
    devices.setuID("2");
    devices.setDateCreated(new Date());
    devices.setGateway(gateway);
    devices.setStatus(Status.OFFLINE);
    devices.setVendor("vendor");
    devicesSet.add(devices);
    
    gateway.setPeripheralDevices(devicesSet);
    return args -> {
        gatewayRepo.save(gateway);
        devicesRepo.save(devices);
    };

我猜这是因为我的模型数据中的一对多关系而引起的一些问题。
我从堆栈跟踪中获得了更多信息

调用休眠序列的下一个值2020-06-26 08:34:53 - SQL错误:90036,SQL状态:90036 2020-06-26 08:34:53 -未找到序列“休眠_序列”; SQL语句:调用休眠序列的下一个值[90036-200] 2020-06-26 08:34:53 -

你有任何想法如何解决这个问题或为什么它不工作。谢谢
configuration.properties:

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

# Enabling H2 Console
spring.h2.console.enabled=true

# Custom H2 Console URL
spring.h2.console.path=/h2

spring.jpa.hibernate.ddl-auto=none
 
#Turn Statistics on and log SQL stmts
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=false
#logging.level.org.hibernate.type=trace
#logging.level.org.hibernate.stat=debug
 
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

更新:在生成GenerationType.IDENTITY之后,我现在得到了以下错误:

Hibernate: 
    select
        getewaymod0_.id as id1_0_1_,
        getewaymod0_.ip_four as ip_four2_0_1_,
        getewaymod0_.name as name3_0_1_,
        getewaymod0_.serial_number as serial_n4_0_1_,
        peripheral1_.gateway as gateway5_1_3_,
        peripheral1_.id as id1_1_3_,
        peripheral1_.id as id1_1_0_,
        peripheral1_.date_created as date_cre2_1_0_,
        peripheral1_.gateway as gateway5_1_0_,
        peripheral1_.uid as uid3_1_0_,
        peripheral1_.vendor as vendor4_1_0_ 
    from
        gateway_model getewaymod0_ 
    left outer join
        peripheral_divices_model peripheral1_ 
            on getewaymod0_.id=peripheral1_.gateway 
    where
        getewaymod0_.id=?
2020-06-26 16:42:04 - SQL Error: 42102, SQLState: 42S02
2020-06-26 16:42:04 - Table "GATEWAY_MODEL" not found; SQL statement:
select getewaymod0_.id as id1_0_1_, getewaymod0_.ip_four as ip_four2_0_1_, getewaymod0_.name as name3_0_1_, getewaymod0_.serial_number as serial_n4_0_1_, peripheral1_.gateway as gateway5_1_3_, peripheral1_.id as id1_1_3_, peripheral1_.id as id1_1_0_, peripheral1_.date_created as date_cre2_1_0_, peripheral1_.gateway as gateway5_1_0_, peripheral1_.uid as uid3_1_0_, peripheral1_.vendor as vendor4_1_0_ from gateway_model getewaymod0_ left outer join peripheral_divices_model peripheral1_ on getewaymod0_.id=peripheral1_.gateway where getewaymod0_.id=? [42102-200]
2020-06-26 16:42:04 - HHH000327: Error performing load command
org.hibernate.exception.SQLGrammarException: could not prepare statement
ckocjqey

ckocjqey1#

我更改了2件事(除了代码中的一些语法/打字错误):
1.添加了cascade=CascadeType.ALL,如下所示:

@JsonIgnore
 @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
 @JoinColumn(name = "gateway")
 private GatewayModel gateway;

1.不能添加包含nullable = false列的实体:
(新的GetewayModel());
devices.setGateway(gateway);
除此之外,它在H2上运行良好。

**更新:**要查找得语法/拼写错误:

  • @Table(name = "PERIPHERAL_DIVICES_MODEL")必须为@Table(name = "PERIPHERAL_DEVICES_MODEL")
  • public class GetewayModel必须为public class GatewayModel
  • private GetewayModel gateway;必须为private GatewayModel gateway;
qyzbxkaa

qyzbxkaa2#

遇到了同样的问题。不同的是我没有在@GeneratedValues中指定strategy。结果默认策略是GeneratedType.AUTO
因此Simon Martinelli的注解解决了我的问题,它为id列/字段显式指定了@GeneratedValue(strategy = GenerationType.IDENTITY)
article很好地总结了这些策略。

*GenerationType.Identity不会创建任何其他序列表(如GenerationType.AUTO / GenerationType.SEQUENCE),并且还会维护从0开始的每个表中的序列,而不是像GenerationType.AUTO那样跨表维护序列号。
*GenerationType.AUTO会再生成一个名为hib_sequences的表,用于维护序列。
*GenerationType.SEQUENCE是完全可自定义的,可能每个自动生成字段都配置了单独的序列。

相关问题