Hibernate mapping file into Jar,junit test failed to perform correctly analysis of problems

Source: Internet
Author: User

The Hibernate entity classes and their mapping files are also placed in the project, and because there are multiple projects using the same database, Hibernate entity classes and mapping files are duplicated, which is inconvenient for maintenance and upgrades. Therefore, it is extracted, made into a jar package, and then introduced back to the project.

In practice, there is a strange problem: The project runs fine, but the JUnit unit test does not run (the method uses the HQL statement). Always report org.hibernate.hql.ast.QuerySyntaxException error, the mapping file is not found, do not know why. The specific error information is as follows:

Org.springframework.orm.hibernate3.HibernateQueryException:Fsettlement is isn't mapped [select F from Fsettlement F where 1=1 and F.fsettlementid =: Fsettlementid];

Nested exception is org.hibernate.hql.ast.QuerySyntaxException:Fsettlement isn't mapped [select F from Fsettlement F whe Re 1=1 and Prepaytype= ' pre_pay ' and F.supplierid =: SupplierId and f.paytime >=: StartDate and f.paytime <=: endd Ate and F.stockelesupplierid =: Stockelesupplierid and F.paytype =:p Aytype and F.fsettlementid =: Fsettlementid]

...

caused by:org.hibernate.hql.ast.QuerySyntaxException:Fsettlement is not mapped [select F to Fsettlement F where 1=1 an  D prepaytype= ' pre_pay ' and F.supplierid =: SupplierId and f.paytime >=: StartDate and F.paytime <=: EndDate and F.stockelesupplierid =: Stockelesupplierid and F.paytype =:p Aytype and F.fsettlementid =: Fsettlementid]

...

The hibernate configuration is as follows:

<bean id= "Sessionfactory"

class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

<property name= "DataSource" ref= "DataSource"/>

<!--del use mappingjarlocations, no longer use mappingdirectorylocations, or it will be an error

<property name= "Mappingdirectorylocations" >

<list>

<value>classpath*:/com/allen/hbm/</value>

</list>

</property>

-->

<property name= "Mappingjarlocations" >

<list>

<value>WEB-INF/lib/model-core*.jar</value>

</list>

</property>

<property name= "Hibernateproperties" >

<props>

<prop key= "Hibernate.dialect" >${hibernate.stock.dialect}</prop>

<prop key= "Hibernate.show_sql" >${hibernate.stock.show_sql} </prop>

<prop key= "Hibernate.cache.provider_class" >${hibernate.stock.cache.provider_class}

</prop>

</props>

</property>

</bean>

Google found a way to try, perhaps the error is too low, no one will make this mistake. The workaround could not be found, but the project continues. Analyzed whether the file was in the jar, and for JUnit, it was read from classpath and nothing else.

So try to change back to the original configuration, found still not;

Is it a relative path, not an absolute path? So will "/" remove, or not;

Do you want to specify a file? Try again, even success! Do not understand, Hibernate will not automatically scan it, although successful, but to specify the filename, it is certainly unrealistic;

So the various attempts to record the results,

<property name= "Mappinglocations" >

<value>classpath*:com/allen/hbm/*.hbm.xml</value>

<!--<value>classpath:com/allen/hbm/*.hbm.xml</value>--> <!--This kind of writing error, can not identify-->

<!--<value>classpath*:/com/alenn/hbm/</value>--> <!--This kind of writing error, can not identify-->

<!--<value>classpath*:/com/allen/hbm</value>--> <!--This kind of writing error, can not identify-->

<!--<value>classpath*:com/allen/hbm/</value>--> <!--This kind of writing error, can not identify-->

<!--<value>classpath*:com/allen/hbm</value>--> <!--This kind of writing error, can not identify-->

</property>

Although the problem has been solved, but there is still a little understand, why the project operation is no problem?

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.