Exception in thread "main" org.hibernate.MappingException: Unknown entity: select * from Port

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/87978337

错误背景

在使用sessionFactory来操作数据库的时候,报了如下的错误


错误信息

Exception in thread "main" org.hibernate.MappingException: Unknown entity: select * from Port
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1141)
    at org.hibernate.internal.SessionImpl.getOuterJoinLoadable(SessionImpl.java:1738)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1643)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
    at cn.com.compent.SessionFactoryDemo.query(SessionFactoryDemo.java:23)
    at cn.com.compent.SessionFactoryDemo.main(SessionFactoryDemo.java:30)


错误代码


错误原因

1、缺少如下的代码(我用的是注解的方式,如果是bean配置的方式,需要给属性添加set和get方法)

     SessionFactory sefa;
    Session se=sefa.getCurrentSession();

2、这里发生错误;应该是获取SessionFactory所在类的name名称

3、主要是对sessionFactory的不了解,不知道怎么使用;

     对注解知识点概念比较模糊


 贴上完整的代码

SessionFactoryDemo.java

对数据库的crud

package cn.com.compent;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.com.bean.Port;
@Repository(value="sessionFactoryDemo")
public class SessionFactoryDemo {
@Autowired
SessionFactory sefa;
public void demo(){
	Session se=sefa.getCurrentSession();
	org.hibernate.Query query=se.createQuery("from Port");
	List<Port> port=query.list();
	for (Port p : port) {
		System.out.println(p.toString());
	}
}
}

xml位置文件

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="cn.com.compent" >
</context:component-scan>
<!--配置文件的位置  -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 给底层数据源设置属性 -->	
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
</bean>
<!-- 设置一个会话工厂一些属性,如显示sql语句,等等 -->
<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="basicDataSource"></property>
<property name="hibernateProperties">
			<props>			
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
		     	<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
			<prop key="current_session_context_class">thread</prop>    
			</props>
		</property>
<!-- 添加映射条件 -->
<property name="mappingDirectoryLocations" value="classpath:/cn/com/bean"></property>
</bean>
<!-- 使用jdbcTemplate来操控数据 -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" name="jdbcTemplate">
<property name="dataSource" ref="basicDataSource"></property>
</bean>
</beans>

测试类

package cn.com.compent;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;

public class Test {
public static void main(String[] args) {
	ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
	SessionFactoryDemo se=(SessionFactoryDemo) ioc.getBean("sessionFactoryDemo");
	se.demo();
}
}

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/87978337