我开始学习 Hibernate,所以我决定从简单的程序开始。我有一堂课:
视频:
package org.media;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;
@Entity
@Table(appliesTo="video")
public class Video {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="description", length=64)
private String description;
//getters and setters...
}
我的 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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">1</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/media</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hibernate.hb2ddl.auto">update</property>
<mapping class="org.media.Video" />
</session-factory>
</hibernate-configuration>
我有一个向数据库添加简单记录的方法:
public void addVideo(String description) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Video video = new Video();
video.setDescription(description);
session.save(video);
session.getTransaction().commit();
}
当我启动我的程序时,它会在此方法上崩溃,但有异常:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.media.Video
我的数据库中有一个相应的表,其名称和列类似于注释。它有什么问题?
先感谢您。