简体   繁体   中英

Error switching from Hibernate Annotations to hbm.xml file

So while working on my project I originally used Hibernate Annotations @Entity, @Table, @Column, @SequenceGenerator, and @GeneratedValue in my java class and was able to successfully add items to my Oracle database.

Now I'm trying to replicate the same thing, but using a *.hbm.xml file and encountering problems.

Here is the original Java Class code with the annotations commented out:

//@Entity
//@Table (name="client")
@SequenceGenerator(name="seq_client",sequenceName="BIMB2013WMMEE.seq_client",
allocationSize=1, initialValue=1)
public class Client {

    //Fields
    //@Id
    //@GeneratedValue(strategy=GenerationType.SEQUENCE)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_client")
    //@Column(name="CLIENT_ID")
    private int id;
    //@Column(name="CLIENT_NAME")
    private String clientName;
    //@Column(name="CLIENT_CODE")
    private String clientCode;

Here is the corresponding hbm.xml file which is located in the src directory of my project.

<hibernate-configuration>

    <session-factory>

        <!-- JDBC Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@endeavour.us.manh.com:1523/pso11r2f</property>
        <property name="connection.username">BIMB2013WMMEE</property>
        <property name="connection.password">BIMB2013WMMEE</property>

        <!-- JDBC connection pool settings ... using built-in test pool -->
        <property name="connection.pool_size">1</property>

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Echo the SQL to stdout -->
        <property name="show_sql">true</property>


        <!-- Set the current session context -->
        <property name="current_session_context_class">thread</property>

    </session-factory>

</hibernate-configuration> 

Finally here is the Eclipse Error code:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.luv2code.hibernate.demo.entity.Client

I didn't make any changes to the class that is actually creating the object and adding it to the database via a session... do I need to?

Thanks for the help!!

I think you may have forgotten mapping tags to list all the resources that are using hibernate persistence in your project.

Here's an example :

hibernate.cfg.xml

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.h2.Driver</property>
    <property name="connection.url">jdbc:h2:file:db/personh2db;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"/>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <mapping resource="com/example/model/Person.hbm.xml"/>
    <mapping resource="com/example/model/Properties.hbm.xml"/>

</session-factory>

The xml file which you have shown is hibernate configuration file it is not hbm.xml file. You have to make "classname.hbm.xml" file for each persistent entity you make - in your case it is your Client class. so you have to make a Client.hbm.xml file. After that you have to add that resource to your configuration file and Hibernate Utility file. You might find this helpful. http://www.mkyong.com/hibernate/how-to-add-hibernate-xml-mapping-file-hbm-xml-programmatically/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM