Exception in thread "main" org.hibernate.MappingException: Unknown entity: sys.model.User
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
at sys.dao.impl.HibernateDao.get(HibernateDao.java:40)
at sys.dao.impl.HibernateDao.get(HibernateDao.java:48)
at sys.dao.impl.HibernateDao$$FastClassByCGLIB$$abf5ef51.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at sys.dao.impl.UserDaoImpl$$EnhancerByCGLIB$$6951f724.get(<generated>)
at sys.service.impl.UserServiceImpl.getUser(UserServiceImpl.java:47)
at sys.service.impl.UserServiceImpl$$FastClassByCGLIB$$9ff87ad6.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at sys.service.impl.UserServiceImpl$$EnhancerByCGLIB$$8317f35e.getUser(<generated>)
at test.Test.main(Test.java:24)
applicationContext.xm配置文件
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<!-- 定义受环境影响易变的变量 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 标准配置 -->
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="sys" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- Hibernate配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="sys.model.*" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* sys.service..*Service.*(..))" advice-ref="txAdvice" />
<aop:advisor pointcut="execution(* sys.dao..*Dao.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="is*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
测试类代码
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sys.model.Role;
import sys.model.User;
import sys.service.RoleService;
import sys.service.UserService;
/**
*
* @author
* @version 1.0
*/
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userManager = (UserService)context.getBean("userService");
RoleService roleManager = (RoleService)context.getBean("roleService");
User user = userManager.getUser(1);
System.out.println("ID"+user.getId()+" 111"+user.getName());
Role role = roleManager.getRole(1);
System.out.println("ID"+role.getId()+" 222 "+role.getName());
}
}
目录结构
6 个解决方案
#1
LZ 自己不看 异常信息吗??? 多练习 自己看
Exception in thread "main" org.hibernate.MappingException: Unknown entity: sys.model.User
这句已经说的很明白了
Exception in thread "main" org.hibernate.MappingException: Unknown entity: sys.model.User
这句已经说的很明白了
#2
sys.model.User这个类应该有一个User.hbm.xml,将其放到sessionFactory的定义中。
#3
有用注解配置呀
package sys.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* 人员管理
* @author 菠萝大象
*/
@Entity
@Table(name = "user")
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private int id; //主键
@Column
private String name; //登录用户
@ManyToOne
@JoinColumn(name = "role_id")
private Role role; //角色ID
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
#4
<property name="packagesToScan" value="sys.model.*" />
改成
<property name="mappingResources" value="sys.model.User" /> 试试呢
#5
sys.model.User 还是不行
不过 改成 sys.* 就可以了 原因我也不知道 谢谢
#6
你确定改成我给你那个不行 注意 name 里有变化的
用你那个 name的 value 你改成 sys.model 应该就可以的
#1
LZ 自己不看 异常信息吗??? 多练习 自己看
Exception in thread "main" org.hibernate.MappingException: Unknown entity: sys.model.User
这句已经说的很明白了
Exception in thread "main" org.hibernate.MappingException: Unknown entity: sys.model.User
这句已经说的很明白了
#2
sys.model.User这个类应该有一个User.hbm.xml,将其放到sessionFactory的定义中。
#3
有用注解配置呀
package sys.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* 人员管理
* @author 菠萝大象
*/
@Entity
@Table(name = "user")
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private int id; //主键
@Column
private String name; //登录用户
@ManyToOne
@JoinColumn(name = "role_id")
private Role role; //角色ID
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
#4
<property name="packagesToScan" value="sys.model.*" />
改成
<property name="mappingResources" value="sys.model.User" /> 试试呢
#5
sys.model.User 还是不行
不过 改成 sys.* 就可以了 原因我也不知道 谢谢
#6
你确定改成我给你那个不行 注意 name 里有变化的
用你那个 name的 value 你改成 sys.model 应该就可以的