在Eclipse中Hibernate程序中的异常

时间:2013-09-02 15:57:33

标签: java hibernate

在我的hibernate程序中,我遇到以下异常:

  

log4j:WARN找不到logger(org.hibernate.cfg.Environment)的appender。   log4j:WARN请正确初始化log4j系统。   线程“main”中的异常org.hibernate.MappingException:需要使用AnnotationConfiguration实例

我的Hibernate.cfg.config文件是

<?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">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mayank</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show-sql">true></property>
<property name="hbm2ddl.auto">update</property>
<mapping class="one.Student"/>
</session-factory>
</hibernate-configuration>

The Student file is 

package one;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student
{
@Id
private Long id;

private int roll;
private String name;

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public int getRoll() {
    return roll;
}
public void setRoll(int roll) {
    this.roll = roll;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Student(Long id, int roll, String name) {
    super();
    this.id = id;
    this.roll = roll;
    this.name = name;
}
public Student() {
    // TODO Auto-generated constructor stub
}
     }

Exception意味着什么,我做错了什么?

3 个答案:

答案 0 :(得分:0)

取代配置cfg = new Configuration(); 我们需要使用AnnotationConfiguration cfg = new AnnotationConfiguration();

对于XML文件:配置 对于注释:AnnotationConfiguration

答案 1 :(得分:0)

(1)在Maven的pom.xml

中添加依赖项
<dependency>
    <groupId>hibernate-annotations</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.3.0.GA</version>
</dependency>

(2)使用AnnotationConfiguration构建会话工厂    正常的Hibernate XML文件映射使用Configuration()

return new Configuration().configure().buildSessionFactory();

对于Hibernate注释,您必须将其更改为AnnotationConfiguration

return new AnnotationConfiguration().configure().buildSessionFactory();

答案 2 :(得分:0)

在我的情况下,我在我的机器上在路径(D:/Praful_WorkSpace/firstHibernate/src/hibernate-configuration-3.0.dtd)下本地下载了dtd文件,然后我在xml文件声明中提到了该路径。我对两个xml文件都做了同样的事情。

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"file:///D:/Praful_WorkSpace/firstHibernate/src/hibernate-configuration-3.0.dtd">