1

私は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>

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

そして、プログラムを起動すると、例外を除いてこのメソッドでクラッシュします。

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.media.Video

私のDBには、注釈のような名前と列を持つ対応するテーブルがあります。何が問題なのですか?

前もって感謝します。

4

3 に答える 3

1

インポートでHibernateの代わりにjavax.persistence.Entityandを使用してみてください。javax.persistence.Table

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mappingから:

「JPAアノテーションはjavax.persistence。*パッケージに含まれています。お気に入りのIDEは、アノテーションとその属性を自動補完できます(JPAアノテーションはプレーンなJDK 5アノテーションであるため、特定の「JPA」モジュールがなくても)。」

于 2012-06-08T17:20:54.257 に答える
0

そのテーブル名属性。

@Tableはクラスレベルで設定されます。エンティティマッピングのテーブル、カタログ、およびスキーマ名を定義できます。@Tableが定義されていない場合は、デフォルト値(エンティティの非修飾クラス名)が使用されます。

@Entity
@Table(name="tbl_sky")
public class Sky implements Serializable {
   ...
}           

 applyTo tryname=の代わりに

私もこれを見つけたので、最初にname =が気に入らない場合は、importステートメントを変更する必要があるかもしれません。

@ org.hibernate.annotations.Tableは補完であり、@javax.persistence.Tableの代わりではありません。特に、テーブルのデフォルト名を変更する場合は、@org.hibernate.annotations.Tableではなく@javax.persistence.Tableを使用する必要があります。

于 2012-06-08T17:11:24.510 に答える
0

休止状態のものを使用する代わりに、javax.persistenceインポートを使用する必要があります。

インポートjavax.persistence.Table; インポートjavax.persistence.Entity;

また、エンティティ名(クラス名)がテーブルと同じ場合は、テーブルアノテーションを使用する必要はありません。エンティティ名がテーブル名と異なる場合は、@ Table(name = "")を使用してください

于 2012-06-09T10:33:20.110 に答える