簡體   English   中英

org.hibernate.MappingException:AnnotationConfiguration實例需要使用

[英]org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class=

我正在嘗試編寫我的第一個休眠注釋應用程序,以下是我所做的事情

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

        }

我在例外之下

線程“主” 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

如果您正在使用maven項目,則在jar下方添加到您的類路徑: hibernate-annotations.3.3.0.GA.jar或在pom文件中添加以下依賴項

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM