我认为我的应用程序不是在阅读hibernate配置文件

时间:2013-07-16 14:45:19

标签: hibernate

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

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();

}
}

0 个答案:

没有答案