数据没有持久化Hibernate + Derby Embedded

时间:2016-06-01 04:19:04

标签: java hibernate derby

我正在尝试使用hibernate将数据保存到嵌入式Derby DB。但数据根本没有持续存在。

hibernate配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
       <property name="hibernate.connection.url">
        jdbc:derby:wso2FlightRecorder
       </property>
       <property name="hibernate.connection.driver_class">
        org.apache.derby.jdbc.EmbeddedDriver
       </property>
       <property name="hibernate.dialect">
        org.hibernate.dialect.DerbyTenSevenDialect
       </property>
       <property name="connection.username"/>
       <property name="connection.password"/>
       <!-- DB schema will be updated if needed -->
       <property name="hibernate.hbm2ddl.auto">create</property>
       <mapping class="org.wso2.esbMonitor.network.PassThruHTTPBean">
       </mapping>
    </session-factory>
</hibernate-configuration>

这是DAO课程

package org.wso2.esbMonitor.network;

import javax.annotation.Generated;
import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "HTTP_LOG")
public class PassThruHTTPBean {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

   @Column(name = "activeThreadCount")
   private int activeThreadCount;

   @Column(name = "avgSizeRecieved")
   private double avgSizeRecieved;

   @Column(name ="avgSizeSent")
   private double avgSizeSent;

   @Column(name ="faultsRecieving")
   private long faultsRecieving;

   @Column(name ="faultSending")
   private long faultSending;

   @Column(name ="messagesRecieved")
   private long messagesRecieved;

   @Column(name ="messageSent")
   private long messageSent;

   @Column(name ="queueSize")
   private int queueSize;

   @Column(name = "time")
   private Date date;
}

这是用于提交DB

的方法
public synchronized static void addNetworkTrafficDetailsToDB(){

        try {
            if (scheduledList.size() > 0){
                logger.info("Started persisting");
                Session session = HibernateSessionCreator.getSession();

                for(PassThruHTTPBean passThruHTTPBean : scheduledList){
                    Transaction tx;
                    tx = session.beginTransaction();
                    session.save(passThruHTTPBean);
                    tx.commit();
                }
                session.flush();
                session.close();
                scheduledList.clear();
            }

堆栈跟踪

2016-06-01 09:31:08 DEBUG AbstractTransactionImpl:158 - begin
2016-06-01 09:31:08 DEBUG LogicalConnectionImpl:212 - Obtaining JDBC   
       connection
2016-06-01 09:31:08 TRACE DriverManagerConnectionProviderImpl:175 - Total  
       checked-out connections: 0
2016-06-01 09:31:08 TRACE DriverManagerConnectionProviderImpl:181 - Using
       pooled JDBC connection, pool size: 0
2016-06-01 09:31:08 DEBUG LogicalConnectionImpl:218 - Obtained JDBC 
       connection 
2016-06-01 09:31:08 DEBUG JdbcTransaction:69 - initial autocommit status: false
2016-06-01 09:31:08 TRACE AbstractServiceRegistryImpl:146 - Initializing service [role=org.hibernate.event.service.spi.EventListenerRegistry]
2016-06-01 09:31:08 TRACE DefaultSaveOrUpdateEventListener:177 - Saving transient instance
    2016-06-01 09:31:08 TRACE AbstractSaveEventListener:167 - Saving [org.wso2.esbMonitor.network.PassThruHTTPBean#<null>]
    2016-06-01 09:31:08 TRACE ActionQueue:177 - Adding an EntityIdentityInsertAction for [org.wso2.esbMonitor.network.PassThruHTTPBean] object
2016-06-01 09:31:08 TRACE ActionQueue:185 - Executing inserts before finding non-nullable transient entities for early insert: [EntityIdentityInsertAction[org.wso2.esbMonitor.network.PassThruHTTPBean#<null>]]
2016-06-01 09:31:08 TRACE ActionQueue:193 - Adding insert with no non-nullable, transient entities: [EntityIdentityInsertAction[org.wso2.esbMonitor.network.PassThruHTTPBean#<null>]]
2016-06-01 09:31:08 TRACE ActionQueue:211 - Executing insertions before resolved early-insert
2016-06-01 09:31:08 DEBUG ActionQueue:213 - Executing identity-insert immediately
2016-06-01 09:31:08 TRACE AbstractEntityPersister:2960 - Inserting entity: org.wso2.esbMonitor.network.PassThruHTTPBean (native id)
2016-06-01 09:31:08 DEBUG SQL:104 - insert into HTTP_LOG (id, activeThreadCount, avgSizeRecieved, avgSizeSent, time, faultSending, faultsRecieving, messageSent, messagesRecieved, queueSize) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:319 - Registering statement [c065801d-0155-0a1f-282f-000004235ae8]
2016-06-01 09:31:08 TRACE AbstractEntityPersister:2780 - Dehydrating entity: [org.wso2.esbMonitor.network.PassThruHTTPBean#<null>]
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [1] as [INTEGER] - 0
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [2] as [DOUBLE] - 0.0
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [3] as [DOUBLE] - 0.0
2016-06-01 09:31:08 TRACE BasicBinder:72 - binding parameter [4] as [TIMESTAMP] - <null>
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [5] as [BIGINT] - 0
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [6] as [BIGINT] - 0
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [7] as [BIGINT] - 0
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [8] as [BIGINT] - 0
2016-06-01 09:31:08 TRACE BasicBinder:84 - binding parameter [9] as [INTEGER] - 0
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:358 - Releasing statement [c065801d-0155-0a1f-282f-000004235ae8]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:472 - Closing prepared statement [c065801d-0155-0a1f-282f-000004235ae8]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
2016-06-01 09:31:08 DEBUG SQL:104 - values identity_val_local()
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:319 - Registering statement [787c0020-0155-0a1f-282f-000004235ae8]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:374 - Registering result set [org.apache.derby.impl.jdbc.EmbedResultSet42@1718dbaa]
2016-06-01 09:31:08 DEBUG IdentifierGeneratorHelper:93 - Natively generated identity: 1
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:401 - Releasing result set [org.apache.derby.impl.jdbc.EmbedResultSet42@1718dbaa]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:515 - Closing result set [org.apache.derby.impl.jdbc.EmbedResultSet42@1718dbaa]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:358 - Releasing statement [787c0020-0155-0a1f-282f-000004235ae8]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:472 - Closing prepared statement [787c0020-0155-0a1f-282f-000004235ae8]
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
2016-06-01 09:31:08 TRACE UnresolvedEntityInsertActions:214 - No unresolved entity inserts that depended on [[org.wso2.esbMonitor.network.PassThruHTTPBean#1]]
2016-06-01 09:31:08 TRACE UnresolvedEntityInsertActions:121 - No entity insert actions have non-nullable, transient entity dependencies.
2016-06-01 09:31:08 DEBUG AbstractTransactionImpl:173 - committing
2016-06-01 09:31:08 TRACE SessionImpl:403 - Automatically flushing session
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:82 - Flushing session
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:144 - Processing flush-time cascades
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:185 - Dirty checking collections
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:200 - Flushing entities and processing referenced collections
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:242 - Processing unreferenced collections
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:254 - Scheduling collection removes/(re)creates/updates
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:118 - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:125 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2016-06-01 09:31:08 DEBUG EntityPrinter:114 - Listing entities:
2016-06-01 09:31:08 DEBUG EntityPrinter:121 - org.wso2.esbMonitor.network.PassThruHTTPBean{date=null, faultsRecieving=0, queueSize=0, activeThreadCount=0, avgSizeRecieved=0.0, messagesRecieved=0, id=1, avgSizeSent=0.0, faultSending=0, messageSent=0}
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:327 - Executing flush
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:359 - Post flush
2016-06-01 09:31:08 TRACE SessionImpl:612 - before transaction completion
2016-06-01 09:31:08 DEBUG JdbcTransaction:113 - committed JDBC Connection
2016-06-01 09:31:08 TRACE TransactionCoordinatorImpl:136 - after transaction completion
2016-06-01 09:31:08 TRACE SessionImpl:624 - after transaction completion
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:82 - Flushing session
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:144 - Processing flush-time cascades
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:185 - Dirty checking collections
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:200 - Flushing entities and processing referenced collections
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:242 - Processing unreferenced collections
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:254 - Scheduling collection removes/(re)creates/updates
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:118 - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2016-06-01 09:31:08 DEBUG AbstractFlushingEventListener:125 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2016-06-01 09:31:08 DEBUG EntityPrinter:114 - Listing entities:
2016-06-01 09:31:08 DEBUG EntityPrinter:121 - org.wso2.esbMonitor.network.PassThruHTTPBean{date=null, faultsRecieving=0, queueSize=0, activeThreadCount=0, avgSizeRecieved=0.0, messagesRecieved=0, id=1, avgSizeSent=0.0, faultSending=0, messageSent=0}
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:327 - Executing flush
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:249 - Starting after statement execution processing [ON_CLOSE]
2016-06-01 09:31:08 TRACE AbstractFlushingEventListener:359 - Post flush
2016-06-01 09:31:08 TRACE SessionImpl:342 - Closing session
2016-06-01 09:31:08 TRACE JdbcCoordinatorImpl:171 - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@4e6454a]
2016-06-01 09:31:08 TRACE LogicalConnectionImpl:164 - Closing logical connection
2016-06-01 09:31:08 DEBUG LogicalConnectionImpl:232 - Releasing JDBC connection
2016-06-01 09:31:08 TRACE DriverManagerConnectionProviderImpl:233 - Returning connection to pool, pool size: 1
2016-06-01 09:31:08 DEBUG LogicalConnectionImpl:250 - Released JDBC connection
2016-06-01 09:31:08 TRACE LogicalConnectionImpl:176 - Logical connection closed

会话工厂

public class HibernateSessionCreator {
    private static SessionFactory ourSessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static void init() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            ourSessionFactory = new AnnotationConfiguration()
                    .addAnnotatedClass(PassThruHTTPBean.class)
                    .configure()
                    .buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

请帮我解决问题:)提前谢谢

0 个答案:

没有答案