session.save不保存到数据库

时间:2015-09-26 12:17:57

标签: java database hibernate

这是JUnit代码:

package com.learn.hibernate.entities;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HibernateTest {

    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void init() {
        System.out.println("before");
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = 
                new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                                            .buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }

    @After
    public void destory() {
        System.out.println("after");
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void testSessionFlush() {
        News news = new News("Java2", "Sun2", new Date());
        session.save(news);
        System.out.println("end");
    }
}

结果如下:

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
before
September 26, 2015 8:10:08 Afternoon org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
September 26, 2015 8:10:08 Afternoon org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
September 26, 2015 8:10:08 Afternoon org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/learn/hibernate/entities/News.hbm.xml
September 26, 2015 8:10:08 Afternoon org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
September 26, 2015 8:10:08 Afternoon org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///hibernate5]
September 26, 2015 8:10:08 Afternoon org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
September 26, 2015 8:10:09 Afternoon org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
September 26, 2015 8:10:09 Afternoon org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
September 26, 2015 8:10:09 Afternoon org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
September 26, 2015 8:10:09 Afternoon org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate5.NEWS
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [date, author, id, title]
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
September 26, 2015 8:10:09 Afternoon org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
after

正如您所看到的,虽然我要求在end之后输出save(),但结果并未包含它。数据库没有变化,为什么?

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

@Test
public void test() {
    Configuration configuration = new Configuration().configure();
    ServiceRegistry serviceRegistry = 
            new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                                        .buildServiceRegistry();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    try {
        System.out.println("before");
        News news = new News("Java2", "Sun2", new Date());
        session.save(news);
        System.out.println("after");
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace(); 
    } finally {
        System.out.println("end");
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}