MultiException stack of 1 java.lang.NoClassDefFoundError:org / hibernate / cfg / Configuration

时间:2018-03-13 17:31:05

标签: java hibernate jersey

我正在使用Jersey和Hibernate。我之前的代码是无效的主要工作获得会话工厂,但这有问题。我已经从整个代码中删除了一些部分来简化。

我知道问题与Session Factory有关,会根据请求添加完整的堆栈跟踪,这很长。

PS我松散地跟随https://www.youtube.com/watch?v=hNBPDHM-3RY

text.tag_add("marg", "1.0", "end")

hibernate.cfg.xml的段。额外的映射会导致问题吗?我应该只需要TradeMessage,仪式?

        INFO: Server startup in 13357 ms
        Mar 13, 2018 5:06:58 PM org.glassfish.jersey.internal.Errors logErrors
        WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
        MultiException stack 1 of 2
        java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
            at com.web.CurrencyFair.endpoints.service.TradeService.<init>(TradeService.java:28)
        ...
        Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
            at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1335)
            at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1163)
            ... 57 more
        MultiException stack 2 of 2
        java.lang.IllegalStateException: Unable to perform operation: create on com.web.CurrencyFair.endpoints.resources.JsonTradeEndpoint
            at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:392)
            at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)    
        ...

TradeService类

        <mapping class="com.web.CurrencyFair.endpoints.modules.TradeMessage"/> 
        <mapping class="com.web.CurrencyFair.endpoints.service.TradeService"/>
        <mapping class="com.web.CurrencyFair.endpoints.resources.JsonTradeEndpoint"/>

配置buildSessionFactory的行是错误的第一行。

        @Path("/")
        @Produces(MediaType.APPLICATION_JSON) // ensure moxy in pom is commented in to use JSON
        @Consumes(MediaType.APPLICATION_JSON)
        public class JsonTradeEndpoint {

            TradeService tradeService = new TradeService();//intit hibernate in constructor

            @GET
            public List<TradeMessage> getTradeMessage() {
            }
            //. . .
        }

2 个答案:

答案 0 :(得分:0)

您确定org.hibernate.cfg.Configuration位于类路径上且WebappClassLoaderBase可以看到它吗?

答案 1 :(得分:0)

我现在通过改变一些东西来修复它,但不是100%确定修复根本原因的是什么。

查看代码更改和CRUD操作

我引用了(hibernate4) org.hibernate.MappingException: Unknown entity

配置文件

        <!-- Names the annotated entity class -->
        <mapping class="com.web.CurrencyFair.endpoints.modules.TradeMessage"/> 
        <!--<mapping class="com.web.CurrencyFair.endpoints.service.TradeService"/>
        <mapping class="com.web.CurrencyFair.endpoints.resources.JsonTradeEndpoint"/>-->

以下是更新的代码

        public class TradeService {

        Configuration configuration = null;
        //StandardServiceRegistryBuilder ssrb = null;
        SessionFactory sessionFactory =  null;
        Session session = null;

        private void init() {
            configuration = new Configuration().configure("hibernate.cfg.xml");
            //ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory();
            session = sessionFactory.openSession();
        }

        public List<TradeMessage> getTradeMessages(TradeMessageFilterBean filterBean){
            List<TradeMessage> queryResult = new ArrayList<>();

            try {

                if(session == null) this.init();

                TradeMessage trademessage = new TradeMessage(654321);

                session = sessionFactory.openSession();
                session.beginTransaction();

                //C
                session.save(trademessage);
                //R
                TradeMessage newTM = (TradeMessage) session.get(TradeMessage.class, 1);
                System.out.println("TM = : " + newTM.toString());
                //U
                newTM.setId(100);
                session.update(newTM);
                newTM = (TradeMessage) session.get(TradeMessage.class, 1);
                System.out.println("TM = : " + newTM.toString());
                //D
                session.delete(newTM);

                session.getTransaction().commit();
                session.close();
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
            }

            return queryResult; 
        }