从Hibernate 4.2.6到4.3.1问题

时间:2014-09-15 13:39:08

标签: java hibernate jpa

  

编辑:我能够将问题归结为Hibernate,但我仍然需要帮助。

我对Hibernate有一个奇怪的问题,因为我将Netbeans从8.0.0更新到8.0.1,我们所有使用Hibernate的项目都不再工作了。 不幸的是,我们的项目缺少适当的依赖管理。所以,我们正在使用各自的Hibernate版本"发货"使用Netbeans(在NB 8.0.0中它是Hibernate 4.2.6和在NB 8.0.1中它是Hibernate 4.3.1)。

在摆弄缺失的依赖(Jandex 1.1.0 Final)后,我们的产品之一再次运行,但另一个产品没有运行,它给了我一个非常奇怪的例外,说某个列不存在(但它以前工作过。)

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458)
at ch.company.product2.disporesgp.logic.MyRouteLogic.getMyRouteFromToDateWithReservationsAndActive(MyRouteLogic.java:61)
at ch.company.product2.disposition.view.NormalWorkPanel.refreshQueryData(NormalWorkPanel.java:1300)
at ch.company.product2.disposition.view.NormalWorkPanel$1.run(NormalWorkPanel.java:119)
at ch.company.product2.util.CursorTemplate.doCursor(CursorTemplate.java:36)
at ch.company.product2.disposition.view.NormalWorkPanel.<init>(NormalWorkPanel.java:121)
at ch.company.product2.disposition.view.HomeView.<init>(HomeView.java:74)
at ch.company.product2.disposition.view.Login$7.run(Login.java:380)
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4120)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:502)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:467)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:212)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1066)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:985)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:673)
at org.hibernate.type.EntityType.resolve(EntityType.java:489)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:920)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2551)
at org.hibernate.loader.Loader.doList(Loader.java:2537)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
at org.hibernate.loader.Loader.list(Loader.java:2362)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
... 7 more
Caused by: org.postgresql.util.PSQLException: ERROR: column visitedstations.hours does not exist
Position: 2141
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
... 39 more

(对不起杂乱的例外。我们在用户登录时运行了一些查询,这些查询弥补了异常中的一些额外消息。)

检查release page for hibernate 4.3.1没有真正帮助

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
  <persistence-unit name="MYPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://ip:port/db"/>
        <property name="hibernate.connection.username" value="user"/>
        <property name="hibernate.connection.password" value="password"/>
        <property name="hibernate.cache.use_query_cache" value="false"/>
        <property name="hibernate.cache.use_second_level_cache" value="false"/>
        <property name="hibernate.format_sql" value="false"/>
        <property name="hibernate.show_sql" value="false"/>
    </properties>
  </persistence-unit>
</persistence>

异常中提到的方法没有改变,实际上只是在运行NamedQuery。

public static List<MyRoute>
        getMyRouteFromToDateWithReservationsAndActive(Date from,
        Date to) {
    EntityManager em = JavaPersistenceUtil.getAndOpenEntityManager();

    return em.createNamedQuery("MyRoute.findFromToDateWithReservationsAndActive")
        .setParameter("from", from, TemporalType.DATE)
        .setParameter("to", to, TemporalType.DATE)
        .getResultList();
}

命名查询:

@NamedQuery(name = "MyRoute.findFromToDateWithReservationsAndActive", 
 query = "SELECT DISTINCT r FROM MyRoute r LEFT JOIN FETCH r.section s
 LEFT JOIN FETCH s.from LEFT JOIN FETCH s.to LEFT JOIN FETCH s.reservation res
 LEFT JOIN FETCH res.contact WHERE r.datum >= :from AND r.datum <= :to AND
 (r.reservedTablePlaces > 0 OR r.reservedPlaces > 0) AND r.active = TRUE")

访问站实体:

@Entity
public class VisitedStation implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne
private Station station;
private int hours;
private int minutes;

...

表格:

CREATE TABLE db.visitedstation(
id integer NOT NULL DEFAULT nextval('db.visitedstation_id_seq'::regclass),
minutes integer NOT NULL,
hours integer NOT NULL,
station_id text,
CONSTRAINT visitedstation_pkey PRIMARY KEY (id),
CONSTRAINT visitedstation_station_id_fkey FOREIGN KEY (station_id)
    REFERENCES db.station (id) MATCH SIMPLE
    ON UPDATE NO ACTION ON DELETE NO ACTION
)

使用hibernate.show_sql进行调试除了失败的查询尝试按visitedstations.hours对结果进行排序这一事实之外,还没有给我任何进一步的结果,我并不理解为什么。< / p>

我真的没有想法如何进一步解决这个问题。 它适用于Hibernate 4.2.6并且如果它是唯一的选择我肯定会回到旧版本(我会告诉我的老板无论如何使用依赖管理)我会非常感谢一些提示,如果我在这里遗漏了一些东西。我花了几天时间试图理解并解决这个问题,这是我的最后一招。 :-D

我希望我足够具体......如果帖子错过了某些信息,请告诉我。

PS:我确实对代码和消息进行了清理,并希望我没有介绍一些拼写错误。

1 个答案:

答案 0 :(得分:0)

我实际上通过更仔细地查看错误消息找到了问题:

org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42703
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: column visited1_.hours does not exist
Position: 2141
org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet

再次查看查询(使用hibernate.show_sql

  

通过visited1_.hours asc,visited1_.minutes asc选择run0_.id作为id1_10_0_ [...]顺序

这引导我进入我的&#34; Run&#34;持有以下陈述的类

@JoinTable(
    name = "run_visitedstation",
    joinColumns = @JoinColumn(name = "run_id"),
    inverseJoinColumns = @JoinColumn(name = "visitedstation_id")
)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@OrderBy("hours ASC, minutes ASC")
private List<VisitedStation> visitedstations;

不幸的是,hoursminutes不是run_visitedstation加入表的直接属性,而是属于visitedstation

如下所述:Hibernate @OrderBy with referenced class

在这里:Hibernate Sort

  

“Hibernate目前在@ElementCollection上忽略了@OrderBy,例如名单。元素的顺序由数据库返回,未定义。“

这在版本4.3.0 Beta 3中得到修复,这就是为什么它之前有效(4.2.6),因为它被忽略了,现在它不是(4.3.1)。