【问题标题】:org.hibernate.exception.SQLGrammarException Incorrect postgresql sql queryorg.hibernate.exception.SQLGrammarException 不正确的 postgresql sql 查询
【发布时间】:2019-06-15 09:54:02
【问题描述】:

从我的 Hibernate 实体获取数据时出错。我已经设置了两个具有关系的实体(OneToMany 和 ManyToOne)。

我尝试将 hibernate.cfg.xml 文件中的 hibernate.dialect 值更改为 org.hibernate.dialect.PostgreSQL82Dialect(因为我使用的值已被弃用)。但是没有运气。

我从异常中获取查询并将其运行到 SQL 控制台中。 查询不起作用。 虽然如果我删除括号,它确实。

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

<?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

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

模型.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);
    }
}

从数据库中获取构造函数的方法(在我添加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);
    }

这是我得到的错误:

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=?]

【问题讨论】:

  • Hibernate 真的不支持当前版本的 Postgres 吗?至少一个PostgreSQL9Dialect?但是left outer join (model as m1_0) 甚至不是有效的标准 SQL,更不用说对 Postgres 进行更正了。
  • 我只是尝试将方言更改为较新的方言(9.5 是我能找到的最新方言),但我仍然遇到同样的错误。

标签: java postgresql hibernate tomcat


【解决方案1】:

好的,我想通了。 为了我的关系,我只需要将 fetch 类型替换为 LAZY:

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

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2018-09-07
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多