0

当我对语句进行硬编码时,它没有读取带注释的类,也没有读取删除和重新创建数据库模式,而启动属性也不是这些,

config.addAnnotatedClass(UserDetail.class); this is for making annotation class visible to my application and

new SchemaExport(config).create(true, true); this is for recreate database schema while startup property.

以上两个语句工作正常,但如果没有这两个语句,我们可以通过配置文件读取它们,即使我做了它,它也没有读取。为什么?

这是我的配置文件

<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/testDb</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!--  <property name="hibernate.default_schema">TESTSCHEMA</property> -->

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup --> 
        <property name="hbm2ddl.auto">create</property>

        <!-- name of the annotated entity class -->
        <mapping class="com.hibernate.CollectionDemo.UserDetail"/>

        <!--<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
        <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/>

    -->
    </session-factory>

</hibernate-configuration>

并且这个配置文件中有一个小警告,它说“资源是 src/hibernate.cfg.xml 的副本,并且没有复制到输出文件夹”这意味着什么?如果其他请指定,这里是主要课程,

package com.hibernate.CollectionDemo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;


public class TestUserDetail 
{
public static void main(String[] args)
{
    UserDetail usrd=new UserDetail();

    Configuration config=new Configuration();
    config.addAnnotatedClass(UserDetail.class);
            config.configure("hibernate.cfg.xml");
            new SchemaExport(config).create(true, true);

            SessionFactory sf=config.buildSessionFactory();

            Session s=sf.openSession();
            s.beginTransaction();

            usrd.setUserName("FirstUser");

            Address addr=new Address();
            addr.setCity("CN");
            addr.setPincode("16345");
            addr.setState("TN");
            addr.setStreetName("t-nagar");

            Address addr2=new Address();
            addr2.setCity("hyd");
            addr2.setPincode("5845");
            addr2.setState("AP");
            addr2.setStreetName("pet");

            usrd.getListOfAddress().add(addr);
            usrd.getListOfAddress().add(addr2);

            s.save(usrd);
            s.getTransaction().commit();

}
}
4

0 回答 0