Hi,
I created an application in which spring and hibernate is used.
I generate pojo java code with hbm2java(Company.java,CompanyDAO.java).
For using in my spring code I create jar(package classes Company.java,CompanyDAO.java) and then in spring I add this jar in my classpath.
But I am still unable to use Company.java.
My applicationcontext.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for DispatcherServlet. -->
<beans>
<!-- Controller for the initial "Hello" page -->
<bean id="infoFormController" class="web.InfoFormController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>info</value></property>
<property name="commandClass"><value>info.Company</value></property>
<property name="successView"><value>view.htm</value></property>
<property name="companyDAO">
<ref bean="companyDAO"/>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>info/Company.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="companyDAO" class="info.CompanyDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url">
<value>jdbc:mysql://localhost:3306/javatest</value>
</property>
<property name="username"><value>javauser</value></property>
<property name="password"><value>javadude</value></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/info.htm">infoFormController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
</beans>
Company.hbm.xml file:
Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="info.Company" table="COMPANY">
<id name="id" type="long" column="COMPANY_ID">
<generator class="native"/>
</id>
<property name="name" type="string" column="COMPANY_NAME"/>
<property name="description" type="string" column="DESCRIPTION"/>
</class>
</hibernate-mapping>
I create DAORegistry.java code in which I register applicationcontext.xml file:
Code:
package cxt;
import java.io.FileNotFoundException;
import java.net.URL;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import info.CompanyDAO;
public class DAORegistry {
private ApplicationContext ctx;
public DAORegistry()throws Exception{
ctx = new ClassPathXmlApplicationContext("CompanyInfo-servlet.xml");
}
public SessionFactory getSessionFactory(){
return (SessionFactory) ctx.getBean("factory", SessionFactory.class);
}
public CompanyDAO getCompanyDAO(){
return (CompanyDAO)ctx.getBean("companyDAO", CompanyDAO.class);
}
public String getFilePath(String fileName) throws FileNotFoundException{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader == null)
{
loader = getClass().getClassLoader();
}
URL fileURL = loader.getResource(fileName);
if(fileURL == null)
{
throw new FileNotFoundException("Could not find "+fileName);
}
return fileURL.getPath();
}
}
Then for testing I use TestMain.java
Code:
package cxt;
import java.util.Date;
import java.util.List;
import info.Company;
import info.CompanyDAO;;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
try
{
// hibernate code start
CompanyDAO companyDAO = null;
Company company = new Company();
company.setName("mycom");
company.setDescription("software");
DAORegistry daoRegister = new DAORegistry();
companyDAO = daoRegister.getCompanyDAO();
companyDAO.saveOrUpdate(company);
List<Company> info = companyDAO.findAll(Company.class);
System.out.println("Number of events - "+info.size());
Company compant = info.get(1);
System.out.println("Company Name - "+company.getName());
}catch(Exception exp)
{
exp.printStackTrace();
}
// hibernate code end
}
}
Exception in console:
Code:
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: info.Company; nested exception is org.hibernate.MappingException: Unknown entity: info.Company
org.hibernate.MappingException: Unknown entity: info.Company
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:557)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1342)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:512)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:80)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:511)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:503)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:693)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:690)
at info.CompanyDAO.saveOrUpdate(Unknown Source)
at cxt.TestMain.main(TestMain.java:27)
Please! give your valuable suggestions.
Thanks
Code: