2017-02-07 11 views
0

私はWebアプリケーションを開発してTomcat 7.0にデプロイしています。私のアプリはMySQLデータベースを使用しています。私はすでにアプリケーションとデータベースの間の接続を設定しており、Hibernate 5.2.5のサポートを追加したいと考えていました。 は、以下の設定でHibernateコンソール経由でデータベースと通信できます。私は非Webアプリケーションで使用すると動作します。問題はサーバーに展開するときだけです。私は警告を受けるTomcat + hibernate =クエリクラスの永続クラスが見つかりません

no persistent classes found for query class: from entities.UserH 

それに起因するサーバーエラー。

私のエンティティクラス:

@Entity 
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames =  {"id"})}) 
public class UserH { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id", nullable = false, unique = true, length = 11) 
    private int id; 
    @Column(name = "login", nullable = false, length = 45) 
    private String login; 
    @Column(name = "name", nullable = false, length = 45) 
    private String name; 
    @Column(name = "surname", nullable = false, length = 45) 
    private String surname; 

    /* getters and setters*/ 
} 

マイ休止-annotation.cfg.xml:私が述べたように

public List<UserH> getAllUsers() { 

    try (Session session = HibernateUtils.getSessionAnnotationFactory().getCurrentSession()) { 
     session.beginTransaction(); 
     Query<UserH> usersQuery = session.createQuery("from entities.UserH", UserH.class); 
     List<UserH> usersList = usersQuery.getResultList(); 

     session.getTransaction().commit(); 

     return usersList; 
    } 
} 

- それは動作します:ユーザーを取得する必要があります

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection properties - Driver, URL, user, password --> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/web-database</property> 
     <property name="hibernate.connection.username">username</property> 
     <property name="hibernate.connection.password">password</property> 

     <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! --> 
     <property name="hibernate.current_session_context_class">thread</property> 

     <!-- Mapping with model class containing annotations --> 
     <mapping class="entities.UserH"/> 
    </session-factory> 
</hibernate-configuration> 

方法通常のアプリではOKですが、Tomcatではできません。利用可能なすべての要素をWebアーティファクトに追加しましたが、それは役に立たなかった。私もpersistance.xmlでJPAサポートを追加しようとしましたが、まだ運がありません。

その他に何か問題がありますか?

編集:私のHibernateUtilsクラス:

public class HibernateUtils { 
    private static SessionFactory sessionAnnotationFactory; 

    private static SessionFactory buildSessionAnnotationFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      Configuration configuration = new Configuration(); 
      configuration.configure("hibernate-annotation.cfg.xml"); 
      System.out.println("Hibernate Annotation Configuration loaded"); 

      ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
      System.out.println("Hibernate Annotation serviceRegistry created"); 

      SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

      return sessionFactory; 
     } catch (Throwable ex) { 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionAnnotationFactory() { 
     if (sessionAnnotationFactory == null) 
      sessionAnnotationFactory = buildSessionAnnotationFactory(); 
     return sessionAnnotationFactory; 
    } 
} 
+0

あなたの 'HibernateUtils'クラスコードを含めてください。 – Naros

+0

@Naros、私は自分の投稿を編集しました。 – Mohru

答えて

0

は多分これは私の質問FOT正しい答えではないが、これは実際に私のために働いていたものです。私はJPA設定+休止状態で休止状態を置き換えました。

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> 

    <persistence-unit name="NewPersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <class>entities.UserH</class> 

     <properties> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/web-database"/> 
      <property name="javax.persistence.jdbc.user" value="Admin"/> 
      <property name="javax.persistence.jdbc.password" value="[email protected]"/> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
     </properties> 
    </persistence-unit> 

</persistence> 

getEntityManager:

public static EntityManager getEntityManager() { 
    EntityManager entityManager = Persistence 
      .createEntityManagerFactory("NewPersistenceUnit") 
      .createEntityManager(); 

    return entityManager; 
} 

getAllUsers:

public List<UserH> getAllUsers() { 

    EntityManager entityManager = HibernateUtils.getEntityManager(); 
    EntityTransaction transaction = entityManager.getTransaction(); 
    transaction.begin(); 
    TypedQuery<UserH> usersQuery = entityManager.createQuery("from UserH ", UserH.class); 
    List<UserH> usersList = usersQuery.getResultList(); 
    transaction.commit(); 
    entityManager.close(); 

    return usersList; 
} 

だからではなく、休止状態-annotation.cfg.xml +セッションを使用して、私はpersistance.xml + EntityManagerを使用しましたしかし、私はまだこの構成が動作する理由を理解できませんが、休止状態だけでは動作しません。どんなコメント/提案も感謝します。

0

あなただけのクラス名のみを参照するようにクエリを変更する必要があります。

session.createQuery("FROM UserH", UserH.class) 

あなたがいるときにパッケージが重要である唯一の時間は次のようになりエンティティを内部クラスとして定義します。

これらの場合、完全なクラス名com.c.SomeOutterClass$MyEntityを使用する必要があります。別の方法としては、明示的にあなたのエンティティクラスに名前を付けるように、それはname属性が含まれるように@Entity注釈を変更することです:

public class SomeOutterClass { 
    @Entity(name = "MyEntity") 
    public static class MyEntity { 

    } 
} 
+0

私のクラスは内部クラスではありません。クエリからパッケージの部分を削除するとエラーが表示されます: 'QuerySyntaxException:UserHは[UserH]からマップされていません '。 '@ Entity'アノテーションで名前を指定しても起こります。 – Mohru

関連する問題