2 Replies Latest reply on Mar 28, 2010 11:57 AM by sogukk

    Hibernate Annotations doesnt work if i dont declare the mapping class

    sogukk
      Hello,

      My problem is ,my hibernate annotations doesnt work(says unknown entity) if
      i dont define the entity classes in the hibernate.cfg.xml file with putting
      a line like                    
      "<mapping class="domain.view.MenuCategory"></mapping>".
      What can i do to fully get use of annotations?



      My components.xml file has the following lines.

          <persistence:hibernate-session-factory name="hibernateSessionFactory" />
              <persistence:managed-hibernate-session name="sessionHibernate" auto-create="true" session-factory-jndi-name="myfactory"/>



      And this is my hibernatecfg.xml file.


      <?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 name="myfactory">

      <property name="current_session_context_class">thread</property>
      <propertyname="hibernate.bytecode.use_reflection_optimizer">false</property><property name="show_sql">true</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.connection.datasource">java:comp/env/jdbc/testdatasource</property>
      <property name="hibernate.format_sql">true</property>
      <property name="hbm2dll.auto">create-drop</property>
      <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
      <property name="transaction.flush.before_completion">true</property>
      <property name="connection.release_mode">after_statement</property>

          </session-factory>
      </hibernate-configuration>


      And my context.xml files is as follows.


      <Context path="/DBTest" docBase="DBTest"
      debug="5" reloadable="true" crossContext="true">


      <Resource name="jdbc/testdatasource" auth="Container" type="javax.sql.DataSource"
      maxActive="100" maxIdle="30" maxWait="10000"
      username="root" password="" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost/hacivad"/>
      </Context>



      and the class is,


      package domain.view;

      import java.io.Serializable;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;

      @SuppressWarnings("serial")
      @Entity
      @Table(name = "VIEW_MENU_CATEGORY")
      public class MenuCategory implements Serializable {

              @Id
              @Column(name = "ID")
              private Long id;
             
              @Column(name = "CATEGORY_LABEL")
              private String label;
             
              @Column(name = "KEY")
              private String key;
             

              public Long getId() {
                      return id;
              }

              public String getLabel() {
                      return label;
              }

              public String getKey() {
                      return key;
              }

      }



      thanks for the help.





        • 1. Re: Hibernate Annotations doesnt work if i dont declare the mapping class
          swd847

          Are the entities in the same jar as the persistence.xml?

          • 2. Re: Hibernate Annotations doesnt work if i dont declare the mapping class
            sogukk
            yes,but i dont reference it from components.xml file.Because i dont want to use an EntityManager(surely there is something wrong with my logic).I will share my components.xml file and persistence.xml file so we can figure out what the problem is more easily.


            persistence.xml(not so diff. from the example project,just the datasource)


            <?xml version="1.0" encoding="UTF-8"?>
            <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                         version="1.0">
               <persistence-unit name="bookingDatabase" transaction-type="RESOURCE_LOCAL">
                  <provider>org.hibernate.ejb.HibernatePersistence</provider>
                  <jta-data-source>java:comp/env/jdbc/testdatasource</jta-data-source>
                  <properties>
                     <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                     <property name="hibernate.show_sql" value="true"/>
                     <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>

                  </properties>
               </persistence-unit>
            </persistence>









            components.xml



            <?xml version="1.0" encoding="UTF-8"?>
            <components xmlns="http://jboss.com/products/seam/components"
                        xmlns:core="http://jboss.com/products/seam/core"
                        xmlns:persistence="http://jboss.com/products/seam/persistence"
                        xmlns:drools="http://jboss.com/products/seam/drools"
                        xmlns:bpm="http://jboss.com/products/seam/bpm"
                        xmlns:security="http://jboss.com/products/seam/security"
                        xmlns:mail="http://jboss.com/products/seam/mail"
                        xmlns:web="http://jboss.com/products/seam/web"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:transaction="http://jboss.com/products/seam/transaction"
                        xsi:schemaLocation=
                            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
                             http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                             http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                             http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                             http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                             http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd
                             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">

                    <core:init transaction-management-enabled="false" debug="true"/>
                            <transaction:no-transaction/>
                           
                <core:manager concurrent-request-timeout="500"
                             conversation-timeout="120000"
                             conversation-id-parameter="cid"
                             parent-conversation-id-parameter="pid"/>

               <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
               <web:hot-deploy-filter url-pattern="*.seam"/>
              
                <persistence:hibernate-session-factory name="hibernateSessionFactory" />
                    <persistence:managed-hibernate-session name="sessionHibernate" auto-create="true" session-factory-jndi-name="myfactory"/>

               <drools:rule-base name="securityRules">
                  <drools:rule-files><value>/security.drl</value></drools:rule-files>
               </drools:rule-base>

               <security:rule-based-permission-resolver security-rules="#{securityRules}"/>

               <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>

               <event type="org.jboss.seam.security.notLoggedIn">
                  <action execute="#{redirect.captureCurrentView}"/>
               </event>
               <event type="org.jboss.seam.security.loginSuccessful">
                  <action execute="#{redirect.returnToCapturedView}"/>
               </event>

               <mail:mail-session host="localhost" port="25"/>

               <!-- For use with jBPM pageflow or process management -->
               <!--
               <bpm:jbpm>
                  <bpm:process-definitions></bpm:process-definitions>
                  <bpm:pageflow-definitions></bpm:pageflow-definitions>
               </bpm:jbpm>
               -->

            </components>


            And this is how i get the hibernateSession from the context in the DAO class.


                    @In("sessionHibernate")
                    private Session session;




            By the way, isnt persistence.xml is some kind of needless here,because i have all this information in hibernate.cfg.xml file.