gpt4 book ai didi

java - org.hibernate.exception.SQLGrammarException 不正确的 postgresql sql 查询

转载 作者:行者123 更新时间:2023-11-28 23:12:07 25 4
gpt4 key购买 nike

我在从我的 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>

持久性.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>

构造函数.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);
}
}

从数据库中获取构造函数的方法(在我添加模型类之前使用)

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;

关于java - org.hibernate.exception.SQLGrammarException 不正确的 postgresql sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com