数据库结构
[code] CREATE TABLE `person` (
`id` int(4) default NULL,
`name` varchar(20) default '',
`password` varchar(20) default ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1 [/code]
映射表的java类
[code]
import javax.persistence.*;
@Entity
@Table(name="person")
public class User implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
}
[/code]
HibernateUtils
[code]
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
public class HibernateUtils {
private static final Logger logger = Logger.getLogger(HibernateUtils.class);
private static final SessionFactory sessionFactory;
private static Configuration configuration;
private static final String resource;
static {
try {
resource = "hibernate.cfg.xml";
sessionFactory = new AnnotationConfiguration().configure(resource).buildSessionFactory();
//sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
//configuration = new Configuration();
// sessionFactory = configuration.configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Exception e) {
logger.error(e.getMessage());
throw new ExceptionInInitializerError(e);
}
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
[/code]
hibernate.cfg.xml配置内容:
[code]
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<!-- 设置entity bean -->
<!--<mapping resource="t_user.hbm.xml"/>-->
<mapping class="User"/>
</session-factory>
</hibernate-configuration>
[/code]
测试类test
[code]
import org.hibernate.Session;
import org.hibernate.Transaction;
public class test {
public static void main(String[] args) {
// 将持久化的物件
User user = new User();
user.setId(3);
user.setName("ady");
user.setPassword("123456");
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(user);
tx.commit();
}
}
[/code]
运行出现一下异常
[code]
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.util.ReflectHelper.classForName(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:752)
at HibernateUtils.<clinit>(HibernateUtils.java:22)
at test.main(test.java:17)
[/code]
这是什么错误啊?有谁遇到过吗?苦恼了好几个小时了。。。
[code] CREATE TABLE `person` (
`id` int(4) default NULL,
`name` varchar(20) default '',
`password` varchar(20) default ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1 [/code]
映射表的java类
[code]
import javax.persistence.*;
@Entity
@Table(name="person")
public class User implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
}
}
[/code]
HibernateUtils
[code]
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
public class HibernateUtils {
private static final Logger logger = Logger.getLogger(HibernateUtils.class);
private static final SessionFactory sessionFactory;
private static Configuration configuration;
private static final String resource;
static {
try {
resource = "hibernate.cfg.xml";
sessionFactory = new AnnotationConfiguration().configure(resource).buildSessionFactory();
//sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
//configuration = new Configuration();
// sessionFactory = configuration.configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Exception e) {
logger.error(e.getMessage());
throw new ExceptionInInitializerError(e);
}
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
[/code]
hibernate.cfg.xml配置内容:
[code]
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<!-- 设置entity bean -->
<!--<mapping resource="t_user.hbm.xml"/>-->
<mapping class="User"/>
</session-factory>
</hibernate-configuration>
[/code]
测试类test
[code]
import org.hibernate.Session;
import org.hibernate.Transaction;
public class test {
public static void main(String[] args) {
// 将持久化的物件
User user = new User();
user.setId(3);
user.setName("ady");
user.setPassword("123456");
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(user);
tx.commit();
}
}
[/code]
运行出现一下异常
[code]
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.util.ReflectHelper.classForName(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:752)
at HibernateUtils.<clinit>(HibernateUtils.java:22)
at test.main(test.java:17)
[/code]
这是什么错误啊?有谁遇到过吗?苦恼了好几个小时了。。。