怎么调用hibernate.cfg.xml文件?

时间:2012-06-01 12:52:38

标签: hibernate

你好朋友,当我调用SessionFactory sessionFactory = new Configuration()。configure()。buildSessionFactory();时,我用hibernate应用程序创建了struts。             session = sessionFactory.openSession(); 这段代码生成错误请帮帮我示例代码在这里 这是我的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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/logintest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">xxxx</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">
        org.hibernate.cache.NoCacheProvider
    </property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="hibernate.hbm.xml"/>

我的映射文件在这里

   <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <hibernate-mapping>
    <class name="net.model.Contact" table="contacts">
   <id name="id" type="int" column="id" >
    <generator class="increment"></generator>
</id>   
<property name="firstName" column="firstname"></property>
<property name="lastName" column="lastname"></property>
 <property name="cellNo" column="cell_no"></property>
<property name="emailId" column="email_id"></property>
 <property name="birthDate" column="birthdate"/>
<property name="website" column="website"/>
  </class>
   </hibernate-mapping>

mainclass就在这里

        public class Testmain {
       public static void main(String args[])
{
    Session session=null;
    try
    {
   SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
        session =sessionFactory.openSession();

    Contact contact=new Contact();
   contact.setFirstName("mohan");
  contact.setLastName("raddy");
  contact.setEmailId("mm@gmail.com");
    contact.setCellNo("4664564564");
   contact.setWebsite("www.stackoverflow.com");
        session.save(contact);
       session.beginTransaction().commit();
      System.out.println("done");


    }catch(Exception e)
    {
    System.out.println(e);
    }




    session.beginTransaction();
    List<Contact> contacts = null;
    try {

        contacts = (List<Contact>)session.createQuery("from Contact").list();

    } catch (HibernateException e) {
        e.printStackTrace();
        session.getTransaction().rollback();
    }
    session.getTransaction().commit();


    for (Contact contact : contacts) {
        System.out.println(contact.getFirstName());
      System.out.println(contact.getLastName());
        System.out.println(contact.getEmailId());
          System.out.println(contact.getCellNo());
            System.out.println(contact.getWebsite());
    }   
   }}

以下错误显示我何时运行主类

线程“main”中的异常java.lang.NoSuchMethodError:org.objectweb.asm.ClassWriter。(Z)V     在net.sf.cglib.core.DebuggingClassWriter。(DebuggingClassWriter.java:47)     at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)     at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)     at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)     at net.sf.cglib.core.KeyFactory $ Generator.create(KeyFactory.java:145)     在net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)     在net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)     在net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)     在net.sf.cglib.proxy.Enhancer。(Enhancer.java:69)     在org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:117)     在org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43)     在org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)     在org.hibernate.tuple.entity.AbstractEntityTuplizer。(AbstractEntityTuplizer.java:135)     在org.hibernate.tuple.entity.PojoEntityTuplizer。(PojoEntityTuplizer.java:55)     在org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping。(EntityEntityModeToTuplizerMapping.java:56)     在org.hibernate.tuple.entity.EntityMetamodel。(EntityMetamodel.java:295)     在org.hibernate.persister.entity.AbstractEntityPersister。(AbstractEntityPersister.java:434)     在org.hibernate.persister.entity.SingleTableEntityPersister。(SingleTableEntityPersister.java:109)     在org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)     在org.hibernate.impl.SessionFactoryImpl。(SessionFactoryImpl.java:226)     在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)     在net.model.Testmain.main(Testmain.java:25) Java结果:1

1 个答案:

答案 0 :(得分:0)

查看this answer,表明您可能选择了错误的asm.jar版本。您应该检查类路径并确保没有安装多个版本。