hibernate的小测试用例 数据库中能建表单但无法录入数据。控制台输出十二月 01, 2017 3:40:37 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
十二月 01, 2017 3:40:37 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
十二月 01, 2017 3:40:37 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十二月 01, 2017 3:40:37 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十二月 01, 2017 3:40:37 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十二月 01, 2017 3:40:37 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
十二月 01, 2017 3:40:37 下午 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!
十二月 01, 2017 3:40:37 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: student.hbm.xml
十二月 01, 2017 3:40:38 下午 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!
十二月 01, 2017 3:40:38 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
十二月 01, 2017 3:40:38 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十二月 01, 2017 3:40:38 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
十二月 01, 2017 3:40:38 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
十二月 01, 2017 3:40:38 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8]
十二月 01, 2017 3:40:38 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****, autocommit=true}
十二月 01, 2017 3:40:38 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
十二月 01, 2017 3:40:38 下午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
十二月 01, 2017 3:40:38 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
十二月 01, 2017 3:40:38 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十二月 01, 2017 3:40:39 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: 
    drop table if exists STUDENT
Hibernate: 
    create table STUDENT (
        SID integer not null,
        SNAME varchar(255),
        GENDER varchar(255),
        BIRTHDAY datetime,
        ADDRESS varchar(255),
        primary key (SID)
    )
十二月 01, 2017 3:40:39 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

解决方案 »

  1.   

    <property name="hbm2ddl.auto">update</property> ????
      

  2.   

    StudentTest.javaimport 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 StudentTest {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;


    @Test
    public void testSaveStudent()
    {

    student s = new student(1,"张三丰","男",new Date(),"武当山");
    session.save(s);
    }

    @After
    public void destroy(){

    transaction.commit();
    session.close();
    sessionFactory.close();

    }

    @Before
    public void init()
    {

    Configuration config = new Configuration().configure();
    ServiceRegistry serviceregistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
    sessionFactory = config.buildSessionFactory(serviceregistry);
    session = sessionFactory.openSession();
    transaction =session.getTransaction();
    }
    }
      

  3.   

    student.hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 2017-12-1 15:55:56 by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="student" table="STUDENT">
            <id name="sid" type="int">
                <column name="SID" />
                <generator class="assigned" />
            </id>
            <property name="sname" type="java.lang.String">
                <column name="SNAME" />
            </property>
            <property name="gender" type="java.lang.String">
                <column name="GENDER" />
            </property>
            <property name="birthday" type="java.util.Date">
                <column name="BIRTHDAY" />
            </property>
            <property name="address" type="java.lang.String">
                <column name="ADDRESS" />
            </property>
        </class>
    </hibernate-mapping>
      

  4.   

    hibernate.cfg.xml<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect </property> 
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="connection.autocommit">true</property>
    <mapping resource = "student.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>