简体   繁体   English

org.hibernate.exception.SQLGrammarException错误的postgresql sql查询

[英]org.hibernate.exception.SQLGrammarException Incorrect postgresql sql query

I'm getting an error while fetching data from my Hibernate entities. 我从Hibernate实体获取数据时遇到错误。 I have set up two entities with relationship (OneToMany and ManyToOne). 我已经建立了两个关系实体(OneToMany和ManyToOne)。

I have tried to change the hibernate.dialect value in the hibernate.cfg.xml file to org.hibernate.dialect.PostgreSQL82Dialect (since the value I was using was deprecated). 我试图将hibernate.cfg.xml文件中的hibernate.dialect值更改为org.hibernate.dialect.PostgreSQL82Dialect(因为我使用的值已被弃用)。 But no luck there. 但那里没有运气。

I took the query from the exception and ran it into a SQL console. 我从异常中获取了查询并将其运行到SQL控制台中。 The query does not work. 查询不起作用。 Although if I remove the parenthesis, it does. 虽然如果我删除括号,它确实如此。

hibernate.cfg.xml 的hibernate.cfg.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>

      <property name = "hibernate.dialect">
         org.hibernate.dialect.PostgreSQL82Dialect
      </property>

      <property name = "hibernate.connection.driver_class">
         org.postgresql.Driver
      </property>

      <property name = "hibernate.connection.url">
         jdbc:postgresql://localhost:5432/udev-mesi
      </property>

      <property name = "hibernate.connection.username">
         JavaRESTfulAPI
      </property>

      <property name = "hibernate.connection.password"></property>

      <property name = "hibernate.hbm2ddl.auto">update</property>

   </session-factory>
</hibernate-configuration>

persistence.xml persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
    <persistence-unit name="udevmesi" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>main.java.com.udev.mesi.entities.Constructor</class>
        <class>main.java.com.udev.mesi.entities.Model</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/udev-mesi" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="JavaRESTfulAPI" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

Constructor.java Constructor.java

package main.java.com.udev.mesi.entities;

import com.udev.mesi.models.WsConstructor;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "constructor")
public class Constructor implements IEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "constructor_id", updatable = false, nullable = false)
    public Long id;

    @Column(name = "name", nullable = false, length = 50, unique = true)
    public String name;

    @Column(name = "is_active", nullable = false, columnDefinition = "bool default true")
    public boolean isActive;

    @OneToMany(fetch=FetchType.EAGER, mappedBy = "constructor")
    public List<Model> models;

    @Override
    // I use this function to convert the entity class into a serializable class for my API
    public Object toWs() {
        return new WsConstructor(id, name, isActive, models);
    }
}

Model.java Model.java

package main.java.com.udev.mesi.entities;

import com.udev.mesi.models.WsConstructor;
import com.udev.mesi.models.WsModel;

import javax.persistence.*;

@Entity
@Table(name = "model")
public class Model implements IEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "model_id", updatable = false, nullable = false)
    public Long id;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "constructor_id", nullable = false)
    public Constructor constructor;

    @Column(name = "name", nullable = false, length = 50, unique = true)
    public String name;

    @Column(name = "is_active", nullable = false, columnDefinition = "bool default true")
    public boolean isActive;

    @Column(name = "count_eco_slots", nullable = false, columnDefinition = "int default 0")
    public int countEcoSlots;

    @Column(name = "count_business_slots", nullable = false, columnDefinition = "int default 0")
    public int countBusinessSlots;

    @Override
    public Object toWs() {
        return new WsModel(id, (WsConstructor) constructor.toWs(), name, isActive, countEcoSlots, countBusinessSlots);
    }
}

The method that gets the constructors from the database (used to work before I added the Model class) 从数据库中获取构造函数的方法(在添加Model类之前用于工作)

public static WsGetConstructors read() throws JSONException {

        // Initialisation de la réponse
        String status = "KO";
        String message = null;
        int code = 500;

        List<Constructor> constructors = null;

        try {
            // Création du gestionnaire d'entités
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(Database.UNIT_NAME);
            EntityManager em = emf.createEntityManager();

            // Récupération des constructeurs depuis la base de données
            Query query = em.createQuery("FROM Constructor WHERE isActive = true");
            constructors = query.getResultList();

            // Création de la réponse JSON
            status = "OK";
            code = 200;

            // Fermeture du gestionnaire d'entités
            em.close();
            emf.close();
        } catch (Exception e) {
            message = e.getMessage();
        }

        return new WsGetConstructors(status, message, code, constructors);
    }

Here's the error I'm getting: 这是我得到的错误:

org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL [select c1_0.constructor_id, c1_0.is_active, m1_0.model_id, c3_0.constructor_id, c3_0.is_active, m3_0.model_id, m3_0.constructor_id, m3_0.count_business_slots, m3_0.count_eco_slots, m3_0.is_active, m3_0.name, c3_0.name, m1_0.count_business_slots, m1_0.count_eco_slots, m1_0.is_active, m1_0.name, m1_0.constructor_id, c1_0.name from constructor as c1_0 left outer join (model as m1_0) on c1_0.constructor_id=m1_0.constructor_id left outer join (constructor as c2_0) on m1_0.constructor_id=c2_0.constructor_id inner join (constructor as c3_0) on m1_0.constructor_id=c3_0.constructor_id left outer join (model as m3_0) on c3_0.constructor_id=m3_0.constructor_id where c1_0.is_active=?]

Ok, I figured it out. 好的,我明白了。 I simply had to replace the fetch type to LAZY for my relationship: 为了我的关系,我只需要将获取类型替换为LAZY:

@OneToMany(fetch=FetchType.LAZY, mappedBy = "constructor")
public List<Model> models;

暂无
暂无

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

相关问题 嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行查询 - nested exception is org.hibernate.exception.SQLGrammarException: could not execute query javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 无法使用 heroku PostgreSQL 执行查询 - javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query with heroku PostgreSQL 引起:org.hibernate.exception.SQLGrammarException: JDBC 异常执行 SQL - Caused by: org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL 如何解决 org.hibernate.exception.SQLGrammarException: 无法执行查询 - How to solve org.hibernate.exception.SQLGrammarException: could not execute query RuntimeException:org.hibernate.exception.SQLGrammarException:无法执行查询 - RuntimeException: org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLGrammarException:无法执行查询 - org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLGrammarException:无法再次执行查询 - org.hibernate.exception.SQLGrammarException: could not execute query again 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM