2

在我的休眠程序中,我收到以下异常:

log4j:WARN 找不到记录器(org.hibernate.cfg.Environment)的附加程序。log4j:WARN 请正确初始化 log4j 系统。线程“主”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
}
     }

异常是什么意思,我做错了什么?

4

3 回答 3

0

cfg = new Configuration(); 我们需要使用 AnnotationConfiguration 代替 Configurationcfg = new AnnotationConfiguration();

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

于 2015-06-18T09:25:47.273 回答
0

(1)在Maven的添加依赖pom.xml

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

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

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

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

return new AnnotationConfiguration().configure().buildSessionFactory();
于 2015-06-18T13:22:22.343 回答
0

就我而言,我在本地机器上的 path( 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">
于 2015-06-23T10:26:40.937 回答