org.hibernate.MappingException:AnnotationConfiguration实例需要使用<mapping class =“

时间:2016-09-03 18:09:46

标签: java hibernate hibernate3

=“”

我正在尝试编写我的第一个hibernate注释应用程序是我做过的事情

package org.hibernate.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class Employee {

@Id
@GeneratedValue
Integer id;
@Column(name="Employee_Name")
String userName;
@Column(name="Address")
String age;



public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
/**
 * Hi this is to test java commenting
 * @return
 */
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

}

Hibernate.config.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.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">$Ailaja12</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sessions</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <!-- <property name="show_sql">true</property> -->


       <!--  <mapping resource="org/hibernate/pojo/Employee.xml"></mapping> -->
        <mapping class="org.hibernate.pojo.Employee"/>
       <mapping resource="org/hibernate/pojo/Item.xml"></mapping>
    </session-factory>
</hibernate-configuration>

客户端

package org.hibernate.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.pojo.Employee;



      public class Client {

            /**
             * @param args
             */
            public static void main(String[] args) {

                //Starting hibernate environment in your application 
                Configuration conf = new Configuration();

                //2 Loading hibernate configuration file
                conf.configure("hibernate.cfg.xml");

                SessionFactory factory=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();


                //SessionFactory factory = conf.buildSessionFactory();

                Session session = factory.getCurrentSession();

                Employee ee = new Employee();
                ee.setUserName("Kranthi");
                //ee.setId(123);
                ee.setAge("Dallas");

                Transaction tx = session.beginTransaction();
                session.save(ee);
                tx.commit();

                session.close();
                factory.close();
            }

        }

我正处于异常

之下

线程中的异常&#34; main&#34; org.hibernate.MappingException:使用

需要AnnotationConfiguration实例

我通过谷歌搜索尝试但没有一个有效 以下是我用过的罐子清单

antlr_2.7.6.jar
asm-3.3.1.jar
cglib-2.2.2.jar
domj-1.3.jar
ehcache.jar
hibernate-3.2.jar
javax.persistence.jar
jta.jar
mysql-connector.jar
commonlogging.jar

1 个答案:

答案 0 :(得分:0)

将以下jar添加到您的类路径: hibernate-annotations.3.3.0.GA.jar 或将以下依赖项添加到您的pom文件中,如果您使用的是maven项目

<dependency>
    <groupId>hibernate-annotations</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.3.0.GA</version>
</dependency>