码迷,mamicode.com
首页 > 其他好文 > 详细

一个奇怪的异常,SSH使用注解方式

时间:2015-03-31 13:08:55      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

异常信息如下

Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: cn.pb.jbit.tcjc034.entity.Director; nested exception is org.hibernate.MappingException: Unknown entity: cn.pb.jbit.tcjc034.entity.Director
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:512)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:506)
at cn.pb.jbit.tcjc034.dir.dao.impl.DirDaoImpl.queryDirectorById(DirDaoImpl.java:36)
at cn.pb.jbit.tcjc034.test.Test.main(Test.java:15)
Caused by: org.hibernate.MappingException: Unknown entity: cn.pb.jbit.tcjc034.entity.Director
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:519)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 5 more

附上源码

package cn.pb.jbit.tcjc034.test;


import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.pb.jbit.tcjc034.dir.dao.DirDao;


public class Test {
	public static void main(String[] args) {
		BeanFactory factory = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		DirDao dao=(DirDao)factory.getBean("ddao");
		System.out.println(dao.queryDirectorById(2));
	}

}
package cn.pb.jbit.tcjc034.entity;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="Director")
@SequenceGenerator(name="dir",sequenceName="seq_dir_dirid")
public class Director implements java.io.Serializable {

	// Fields

	private int dirid;
	private String dirname;

	

	// Property accessors
	@Id
	@GeneratedValue(generator="dir")
	public int getDirid() {
		return this.dirid;
	}

	public void setDirid(int dirid) {
		this.dirid = dirid;
	}

	public String getDirname() {
		return this.dirname;
	}

	public void setDirname(String dirname) {
		this.dirname = dirname;
	}

}
package cn.pb.jbit.tcjc034.dir.dao;

import java.util.List;

import cn.pb.jbit.tcjc034.entity.Director;

public interface DirDao {
	public void addDirector(Director director);

	public void deleteDirector(int id);

	public void updateDirector();

	public Director queryDirectorById(int id);

	public List<Director> queryAllDirector();
	

}
package cn.pb.jbit.tcjc034.dir.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.pb.jbit.tcjc034.dir.dao.DirDao;
import cn.pb.jbit.tcjc034.entity.Director;
@Repository("ddao")
public class DirDaoImpl extends HibernateDaoSupport implements DirDao {
	@Resource  
    public void setSessionFacotry(SessionFactory sessionFacotry) {  
        super.setSessionFactory(sessionFacotry);  
    }  
	public void addDirector(Director director) {
		// TODO Auto-generated method stub

	}

	public void deleteDirector(int id) {
		// TODO Auto-generated method stub

	}

	public List<Director> queryAllDirector() {
		// TODO Auto-generated method stub
		return null;
	}

	public Director queryDirectorById(int id) {
		// TODO Auto-generated method stub
		return (Director) this.getHibernateTemplate().get(Director.class, id);
	}

	public void updateDirector() {
		// TODO Auto-generated method stub

	}
	
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
	 <context:annotation-config />  
	 <context:component-scan base-package="cn.pb.jbit.tcjc034"></context:component-scan> 

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
		</property>
		<property name="url" value="jdbc:oracle:thin:@172.25.24.214:1521:orcl">
		</property>
		<property name="username" value="tuzki"></property>
		<property name="password" value="orcl"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>
					cn.pb.jbit.tcjc034.entity.Director
				</value>
			</list>
		</property>
	</bean>
</beans>

最奇怪的地方就是

<property name="annotatedClasses">
	<list>
	    <value>
		cn.pb.jbit.tcjc034.entity.Director
    	    </value>
	</list>
/property>

name属性试了好几个annotatedClasses,annotatedPackages,packagesToScan

只有annotatedClasses的时候,value给类名称,不出异常。

annotatedPackages,packagesToScan给包名cn.pb.jbit.tcjc034.entity都提示找不到类,报了异常

= =哪里出的错啊= =


一个奇怪的异常,SSH使用注解方式

标签:

原文地址:http://my.oschina.net/eviltuzki/blog/393961

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!