簡體   English   中英

org.hibernate.exception.SQLGrammarException錯誤的postgresql sql查詢

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

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

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

從數據庫中獲取構造函數的方法(在添加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=?]

好的,我明白了。 為了我的關系,我只需要將獲取類型替換為LAZY:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM