论坛首页 入门技术论坛

Hibernate Annotation 遇到问题

浏览 4180 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-11-16  
数据库结构

 CREATE TABLE `person` (                 
          `id` int(4) default NULL,             
          `name` varchar(20) default '',        
          `password` varchar(20) default ''     
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1 


映射表的java类


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() {
    }



}



HibernateUtils

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



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

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


测试类test



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


	}


}


运行出现一下异常

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)


这是什么错误啊?有谁遇到过吗?苦恼了好几个小时了。。。
   发表时间:2007-11-16  
呵呵,问题解决了,我把所有的JAR包都换成了最新版本,却忘了换hibernate3的包。。。。。。
0 请登录后投票
   发表时间:2007-11-27  
HibernateUtils   这个类必须得写吗
0 请登录后投票
   发表时间:2007-11-27  
嘿嘿 按照你的例子终于事搞定了 谢谢哈
0 请登录后投票
   发表时间:2008-05-08  
我感觉你的HibernateUtil应该仿照myeclipse自动生成的那个例子来写,应为你的线程是不安全的,根本就没有ThreadLocal这样来重构session
0 请登录后投票
   发表时间:2008-05-15  
引用
webpiggy 2007-11-16
呵呵,问题解决了,我把所有的JAR包都换成了最新版本,却忘了换hibernate3的包。。。。。。


web pig
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics