I started learning Hibernate, so I decided to start with simple program. I have a class:
Video:
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...
}
My hibernate.cfg.xml file looks like:
<?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>
I have a method that adds simple record to DB:
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();
}
And when I launch my program, it crashes on this method with exception:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.media.Video
There’s a corresponding tables in my DB with a name and columns like in annotations. What is wrong with it?
Thank you in advance.
Try using
javax.persistence.Entityandjavax.persistence.Tableinstead of Hibernate’s in your imports.From http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping:
“JPA annotations are in the javax.persistence.* package. You favorite IDE can auto-complete annotations and their attributes for you (even without a specific “JPA” module, since JPA annotations are plain JDK 5 annotations).”