org.hibernate.exception.SQLGrammarException 不正确的postgresql s



我在从休眠实体获取数据时遇到错误。我已经建立了两个具有关系的实体(OneToMany和ManyToOne)。

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

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

休眠.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);
}
}

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

最新更新