1

I have hibernate configuration file as follows

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">postgres</property>
        <property name="hibernate.connection.url">jdbc:postgresql://10.162.9.130:5432/mining</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

Mapping files are written and included in mams-hibern.xml file as follows

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource" /></property>
<property name="mappingResources">
<list>
<value>nic/mams/model/ApplicantHistory.hbm.xml</value>
<value>nic/mams/model/ApplicantAddressHistory.hbm.xml</value>
<value>nic/mams/model/ApplicantContactHistory.hbm.xml</value>
<value>nic/mams/model/ApplicantLeases.hbm.xml</value>
<value>nic/mams/model/GeneratedChallan.hbm.xml</value>
<value>nic/mams/model/OffenderType.hbm.xml</value>
<value>nic/mams/model/OffenceDetailRule.hbm.xml</value>
<value>nic/mams/model/TemperoryAppNotes.hbm.xml</value>
<value>nic/mams/model/TemperoryFilePath.hbm.xml</value>
<value>nic/mams/model/OtherDocuments.hbm.xml</value>
<value>nic/mams/model/OffenceSubactsRules.hbm.xml</value>
<value>nic/mams/model/OffenceActRules.hbm.xml</value>
<value>nic/mams/model/Vehicles.hbm.xml</value>
<value>nic/mams/model/ApprovedMajorMineral.hbm.xml</value>
<value>nic/mams/model/GoogleImage.hbm.xml</value>
<value>nic/mams/model/PasswordHistory.hbm.xml</value>
<value>nic/mams/model/LeaseTransfer.hbm.xml</value>
<value>nic/mams/model/TransferApplicantCompany.hbm.xml</value>
<value>nic/mams/model/MailSettings.hbm.xml</value>
<value>nic/mams/model/LeaseCompanyAddress.hbm.xml</value>
<value>nic/mams/model/FinancialYear.hbm.xml</value>
<value>nic/mams/model/IncomeTax.hbm.xml</value>
<value>nic/mams/model/AdditionalCharge.hbm.xml</value>
<value>nic/mams/model/MenuPrivileges.hbm.xml</value>
<value>nic/mams/model/AddressType.hbm.xml</value>
<value>nic/mams/model/AdvaloramRoyalty.hbm.xml</value>
<value>nic/mams/model/Agent.hbm.xml</value>
<value>nic/mams/model/AppJawsize.hbm.xml</value>
<value>nic/mams/model/Applicant.hbm.xml</value>
<value>nic/mams/model/ApplicantAddress.hbm.xml</value>
<value>nic/mams/model/ApplicantCompany.hbm.xml</value>
<value>nic/mams/model/ApplicantContact.hbm.xml</value>
<value>nic/mams/model/ApplicantLogin.hbm.xml</value>
<value>nic/mams/model/ApplicantStatus.hbm.xml</value>
<value>nic/mams/model/Application.hbm.xml</value>
<value>nic/mams/model/ApplicationDocument.hbm.xml</value>
<value>nic/mams/model/ApplicationHistory.hbm.xml</value>
<value>nic/mams/model/ApplicationMineral.hbm.xml</value>
<value>nic/mams/model/ApplicationSpecificQuery.hbm.xml</value>
<value>nic/mams/model/ApplicationSpecificQueryValues.hbm.xml</value>
<value>nic/mams/model/ApplicationStatus.hbm.xml</value>
<value>nic/mams/model/ApplicationSurvey.hbm.xml</value>
<value>nic/mams/model/ApplicationType.hbm.xml</value>
<value>nic/mams/model/ApplnSecurityFee.hbm.xml</value>
<value>nic/mams/model/AreaSpecification.hbm.xml</value>
<value>nic/mams/model/Bank.hbm.xml</value>
</property>
</beans>

The following code is executed successfully,

Applicant applicant=null;
Object[] ob={leaseNo,Integer.parseInt(appType)};
Session session=getHibernateTemplate().getSessionFactory().openSession();
Transaction tx=session.beginTransaction();
if(session!=null){
    Query query=session.createQuery("select a from Applicant as a inner join a.applications as b inner join " +
    "b.applicationType as c inner join b.permits as d where d.permitNo=? and c.applicationTypeId=?");
    query.setParameters(ob,  new Type []{Hibernate.STRING,Hibernate.INTEGER});
    @SuppressWarnings("unchecked")
    List<Applicant> apllApplicants=query.list();

}
tx.commit();

My problem is when executing the following code,an exception is occurred

    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session =  sessionFactory.getCurrentSession();              
Transaction tx=session.beginTransaction();
Query query=session.createQuery("select a from Applicant as a inner join a.applications as b inner join " +
        "b.applicationType as c inner join b.permits as d where d.permitNo=? and c.applicationTypeId=?");
query.setParameters(ob,  new Type []{Hibernate.STRING,Hibernate.INTEGER});
@SuppressWarnings("unchecked")
List<Applicant> apllApplicants=query.list();
tx.commit();

The exception is

org.hibernate.hql.ast.QuerySyntaxException: Applicant is not mapped d [select a from Applicant as a inner join a.applications as b inner join b.applicationType as c inner join b.permits as d where d.permitNo=? and c.applicationTypeId=?]

What is the solution for this.Any one know please help me

1 Answer 1

1

You are working in two different context:

  1. first example - Spring-context: Session is built via LocalSessionFactoryBean, responsible of loading hibernate.cfg.xml AND all mappings as described in spring-context.xml
  2. second example - Native Hibernate: Session is built using Hibernate's Configuration responsible of loading ONLY hibernate.cfg.xml which doesn't contains class mappings; add <mapping resource="path.to.your.mappedClass.hbm.xml"/> to your hibernate.cfg.xml to solve the problem.
Sign up to request clarification or add additional context in comments.

2 Comments

My question is that,how we can reuse mapping files declared in Spring-context in hibernate.cfg.xml
you have to use an Hibernate Mapping file as described at tutorialspoint.com/hibernate/hibernate_mapping_files.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.