1

I dont know why but when I import this project to Eclipse. This work well.

So, I think this is problem of eclipse project when import to InteliJ IDEA


This not easy such my imagine.

I have class Setting and Setting.hbm.xml for mapping hibernate. In this class:

<hibernate-mapping>
<class name="Setting" table="setting" lazy="false">
    <id name="id" column="id" type="integer">
        <generator class="increment" />
    </id>

    .....
</class>

<query name="select.setting">
    from Setting as s where s.id = ? order by s.name
</query>

Now, when I call function

this.getHibernateTemplate().findByNamedQuery("select.setting", params);

This return error

org.springframework.orm.hibernate4.HibernateSystemException: Named query not known: select.setting; nested exception is org.hibernate.MappingException: Named query not known: select.setting
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:218) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:343) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.orm.hibernate4.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:933) ~[spring-orm-4.1.6.RELEASE.jar:4.1.6.RELEASE]

Please give advice about it.

Tran Vinh Quang
  • 585
  • 2
  • 9
  • 30
  • Possible duplicate qtn. Check this link http://stackoverflow.com/questions/26084031/mappingexception-named-query-not-known – Killer Dec 02 '15 at 10:01
  • It not duplicate bro. Please check again – Tran Vinh Quang Dec 02 '15 at 10:03
  • Does the name of the query without dot give the same result ? (Say : "selectSetting" instead of "select.setting", for example). – Arnaud Dec 02 '15 at 10:06
  • Have you included the `hbm` in the session? – dan Dec 02 '15 at 10:14
  • Yes, I use spring boot and already set mapping @Bean public LocalSessionFactoryBean sessionFactory() throws Exception { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setHibernateProperties(hibernateProperties()); sessionFactory.setMappingDirectoryLocations(new ClassPathResource[] { new ClassPathResource("data/model") }); – Tran Vinh Quang Dec 02 '15 at 10:21
  • Refer the post: http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean – AnirbanDebnath Dec 02 '15 at 11:29

2 Answers2

1

You can give a try with this.

<query name="select.setting">
    <![CDATA[from Setting as s where s.id = ? order by s.name]]>
</query>
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28
0

The XML parser gets confused of you are not using CDATA tag. CDATA is way of telling the framework that its a data which should not be interpreted as a markup.

Hence as @Lovababu mentioned, include the query inside CDATA tags:

<query name="select.setting">
<![CDATA[from Setting as s where s.id = ? order by s.name]]>

AnirbanDebnath
  • 990
  • 16
  • 26