简体   繁体   中英

hibernate named query error after updating to hibernate 5.0.7 and jpa 2.1

I have very simple named query that use to work before update but now I am getting runtime error.

this is the named query :

@Entity
@Table(name="FRA_HIER_NODE_TYPE", schema="FRA_DATA")
@NamedQuery(name="NodeType.FetchNodeTypes", query="FROM NodeType")
public class NodeType {
...

The exception i see :

Caused by: org.hibernate.HibernateException: Errors in named queries: NodeType.FetchNodeTypes
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:493)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:416)
    at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:401)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    ... 25 more

You should use something like this :

@Entity
@Table(name="FRA_HIER_NODE_TYPE", schema="FRA_DATA")
@NamedQuery(name="NodeType.FetchNodeTypes",query="SELECT * FROM NodeType") 

Hope this Helps.

Did you try this:

@Entity
@Table(name="FRA_HIER_NODE_TYPE", schema="FRA_DATA")
@NamedQuery(name="NodeType.FetchNodeTypes",query="SELECT n FROM NodeType n")

Are you declaring your own SessionFactory for Hibernate? I don't remember how Hibernate works with Spring.

But if you have with hibernate config you may have lines like this:

<mapping class="com.example.NodeType" />

This is removed in Hibernate 5. Instead you have to declare your entities in code when building SessionFactory . For example:

        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
            .configure()
            .build();

        Metadata metadata = new MetadataSources(standardRegistry)
            .addAnnotatedClass(NodeType.class)
            .getMetadataBuilder()
            .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
            .build();

        sessionFactory = metadata
            .getSessionFactoryBuilder()
            .build();

See more: http://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/#bootstrap-native-sessionfactory

Maybe there is some SessionFactory builder for Spring that you now have to configure similary. If so, please post your new config for others.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM