Hibernate(映射)ClassNotFoundException

时间:2013-12-26 22:11:29

标签: java spring hibernate

我只是想让Hibernate第一次工作......我觉得我很接近,但我不确定为什么我的映射不起作用。

我有一个类,User,我有以下User.hbm.xml映射。

<hibernate-mapping>
    <class name="User" table="users">
        <composite-id name="id" class="User">
            <key-property name="id" type="java.lang.Integer">
                <column name="id" />
            </key-property>
            <key-property name="fname" type="string">
                <column name="fname" />
            </key-property>
            <key-property name="lname" type="string">
                <column name="lname" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

当我尝试以下操作时:

User user = null;
try {
    session.beginTransaction();         
    user = (User) session.get(User.class, id);
    session.getTransaction().commit();
}
catch (HibernateException e) {
    e.printStackTrace();
    session.getTransaction().rollback();
}

我得到了:

java.lang.ClassNotFoundException: User
    java.net.URLClassLoader$1.run(Unknown Source)
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    org.springframework.boot.loader.LaunchedURLClassLoader.findClass(LaunchedURLClassLoader.java:148)
    java.lang.ClassLoader.loadClass(Unknown Source)
    org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:131)
    org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:103)
    java.lang.ClassLoader.loadClass(Unknown Source)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Unknown Source)
    org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:193)
    org.hibernate.mapping.Component.getComponentClass(Component.java:138)
    org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:155)
    org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:64)
    org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:59)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98)
    org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:122)
    org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:81)
    org.hibernate.mapping.Component.getType(Component.java:180)
    org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310)
    org.hibernate.mapping.RootClass.validate(RootClass.java:271)
    org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
    org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
    com.cloudfordev.controlpanel.HibernateUtil.configureSessionFactory(HibernateUtil.java:16)
    com.cloudfordev.controlpanel.GetController.greeting(GetController.java:19)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:947)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:878)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:946)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

我是否认为这是我的User.hbm.xml的问题?

如果是,解决方案是什么?

谢谢,

1 个答案:

答案 0 :(得分:4)

您必须在

中为您的班级指定完全限定的班级名称
<class name="User" table="users">

因此,如果您的User课程在com.entities包中,则需要

<class name="com.entities.User" table="users">

如果你的班级在默认包装中,请把它放在一个实际的包装中,让你自己避免头痛。