简体   繁体   English

InvalidDataAccessResourceUsageException:org.hibernate.exception.SQLGrammarException:

[英]InvalidDataAccessResourceUsageException: org.hibernate.exception.SQLGrammarException:

I have the following problem I have been stuck for a while:我有以下问题我已经卡了一段时间:

I get this error: org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement;我收到此错误: org.springframework.dao.InvalidDataAccessResourceUsageException:无法准备语句; SQL [call next value for hibernate_sequence]; SQL [调用休眠序列的下一个值]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement嵌套异常是 org.hibernate.exception.SQLGrammarException:无法准备语句

I found out that people with this error get it because they use reserve words for table names but I do not think this is my issue.我发现有这个错误的人会得到它,因为他们使用保留字作为表名,但我认为这不是我的问题。

My two model classes are as follows.我的两个 model 类如下。 I am skipping the getters/setters and consturctors我正在跳过 getter/setter 和 constructors

@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;

Then in the main class I try to put some data like this:然后在主要的 class 中,我尝试输入一些数据,如下所示:

@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);
    };

I am guessing that are is some issue because of the OneToMany Relationship in my model data.我猜这是一些问题,因为我的 model 数据中的 OneToMany 关系。

I bit more from the stack trace我从堆栈跟踪中获得了更多信息

call next value for hibernate_sequence 2020-06-26 08:34:53 - SQL Error: 90036, SQLState: 90036 2020-06-26 08:34:53 - Sequence "HIBERNATE_SEQUENCE" not found;调用 hibernate_sequence 的下一个值 2020-06-26 08:34:53 - SQL 错误:90036,SQLState:90036 2020-06-26 08:34:53 - 找不到序列“HIBERNATE_SEQUENCE”; SQL statement: call next value for hibernate_sequence [90036-200] 2020-06-26 08:34:53 - SQL 语句:调用 hibernate_sequence [90036-200] 的下一个值 2020-06-26 08:34:53 -

Do you have any idea how to resolve this or why it is not working.您是否知道如何解决此问题或为什么它不起作用。 Thanks谢谢

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

Update: After making GenerationType.IDENTITY I got the following error now:更新:在生成 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

I changed 2 things(besides the few grammar/typo errors in your code):我更改了 2 件事(除了代码中的一些语法/错字错误):

  1. Added cascade=CascadeType.ALL as follows:添加cascade=CascadeType.ALL如下:

     @JsonIgnore @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "gateway") private GatewayModel gateway;
  2. You can't add an entity, which has columns with nullable = false :您不能添加具有nullable = false列的实体:
    devices.setGateway(new GetewayModel()); devices.setGateway(新 GetewayModel());

    devices.setGateway(gateway);


Otherwise, it's working fine on H2 .否则,它在H2上工作正常。

UPDATE:更新:
Grammar/typo errors to look for:要查找的语法/错字错误:

  • @Table(name = "PERIPHERAL_DIVICES_MODEL") needs to be @Table(name = "PERIPHERAL_DEVICES_MODEL") @Table(name = "PERIPHERAL_DIVICES_MODEL")需要是@Table(name = "PERIPHERAL_DEVICES_MODEL")
  • public class GetewayModel needs to be public class GatewayModel public class GetewayModel需要是public class GatewayModel
  • private GetewayModel gateway; needs to be private GatewayModel gateway;需要是private GatewayModel gateway;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Hibernate:org.hibernate.exception.SQLGrammarException - Spring hibernate: org.hibernate.exception.SQLGrammarException JPA 异常:org.hibernate.exception.SQLGrammarException - JPA Exception: org.hibernate.exception.SQLGrammarException 获取异常:org.hibernate.exception.sqlgrammarexception - Getting Exception : org.hibernate.exception.sqlgrammarexception 获取异常org.hibernate.exception.SQLGrammarException: - getting exception org.hibernate.exception.SQLGrammarException: org.hibernate.exception.SQLGrammarException:无法插入,使用mysql休眠 - org.hibernate.exception.SQLGrammarException: could not insert, hibernate with mysql 带有MySQL的Hibernate错误org.hibernate.exception.SQLGrammarException - Hibernate error org.hibernate.exception.SQLGrammarException with MySQL 嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行查询 - nested exception is org.hibernate.exception.SQLGrammarException: could not execute query Spring MVC JPA异常org.hibernate.exception.SQLGrammarException - Spring MVC JPA exception org.hibernate.exception.SQLGrammarException org.hibernate.exception.SQLGrammarException:休眠不会加入列 - org.hibernate.exception.SQLGrammarException: hibernate will not join column 通过Hibernate使用MySQL:org.hibernate.exception.SQLGrammarException - using MySQL by Hibernate : org.hibernate.exception.SQLGrammarException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM