你好朋友,当我调用 SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory(); 时,我用 hibernate 应用程序创建了 struts;会话 =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>
主课在这里
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 at net.sf.cglib.core.DebuggingClassWriter.(DebuggingClassWriter.java:47) at net.sf.cglib.core .DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30) 在 net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24) 在 net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) 在net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) 在 net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117) 在 net.sf.cglib.core.KeyFactory。在 org.hibernate 的 net.sf.cglib.proxy.Enhancer.(Enhancer.java:69) 的 net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104) 处创建(KeyFactory.java:108)。 proxy.pojo.cglib.CGLIBLazyInitializer。getProxyFactory(CGLIBLazyInitializer.java:117) at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43) at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162) at 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) at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:226) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at net.model.Testmain.main( Testmain.java:25) Java 结果:1