Java 从Hibernate批注切换到hbm.xml文件时出错

Java 从Hibernate批注切换到hbm.xml文件时出错,java,sql,xml,oracle,hibernate,Java,Sql,Xml,Oracle,Hibernate,因此,在处理我的项目时,我最初在java类中使用了Hibernate注释@Entity、@Table、@Column、@SequenceGenerator和@GeneratedValue,并且能够成功地将项目添加到Oracle数据库中 现在我尝试复制相同的东西,但是使用*.hbm.xml文件时遇到了问题 以下是注释被注释掉的原始Java类代码: //@Entity //@Table (name="client") @SequenceGenerator(name="seq_client",sequ

因此,在处理我的项目时,我最初在java类中使用了Hibernate注释@Entity、@Table、@Column、@SequenceGenerator和@GeneratedValue,并且能够成功地将项目添加到Oracle数据库中

现在我尝试复制相同的东西,但是使用*.hbm.xml文件时遇到了问题

以下是注释被注释掉的原始Java类代码:

//@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;
下面是相应的hbm.xml文件,它位于我的项目的src目录中

<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> 
我没有对实际创建对象并通过会话将其添加到数据库的类进行任何更改。。。我需要吗


谢谢你的帮助

我认为您可能忘记了映射标记,它列出了项目中使用hibernate持久性的所有资源

下面是一个例子:

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>


org.h2.Driver
jdbc:h2:file:db/personh2db;DB_CLOSE_DELAY=-1;MVCC=真
sa
1.
org.hibernate.dial.h2方言
org.hibernate.cache.internal.NoCacheProvider
真的
创造

我想您可能忘记了在项目中列出所有使用hibernate持久性的资源的映射标记

下面是一个例子:

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>


org.h2.Driver
jdbc:h2:file:db/personh2db;DB_CLOSE_DELAY=-1;MVCC=真
sa
1.
org.hibernate.dial.h2方言
org.hibernate.cache.internal.NoCacheProvider
真的
创造

您显示的xml文件是hibernate配置文件,而不是hbm.xml文件。 您必须为您创建的每个持久实体创建“classname.hbm.xml”文件——在您的示例中,它是您的客户机类。因此,您必须创建一个Client.hbm.xml文件。之后,您必须将该资源添加到配置文件和Hibernate实用程序文件中。你可能会发现这很有帮助。

您显示的xml文件是hibernate配置文件,而不是hbm.xml文件。 您必须为您创建的每个持久实体创建“classname.hbm.xml”文件——在您的示例中,它是您的客户机类。因此,您必须创建一个Client.hbm.xml文件。之后,您必须将该资源添加到配置文件和Hibernate实用程序文件中。你可能会发现这很有帮助。

我肯定忘了添加映射标记,所以这很有帮助!但是我仍然有一些问题。。首先,我得到了一个关于缺少名为Client的实体类的错误(我认为这是因为我注释掉了java文件中的@entity标记)。因此,我在Client.java文件中重新添加了Entity标记,并得到一个新错误“Exception in thread”main“org.hibernate.AnnotationException:没有为Entity指定标识符:com.luv2code.hibernate.demo.Entity.Client”。。此时,我觉得我要回到hibernate的注释样式,而不是.hbm.xml样式。有什么想法?我肯定忘了添加映射标签,所以这很有帮助!但是我仍然有一些问题。。首先,我得到了一个关于缺少名为Client的实体类的错误(我认为这是因为我注释掉了java文件中的@entity标记)。因此,我在Client.java文件中重新添加了Entity标记,并得到一个新错误“Exception in thread”main“org.hibernate.AnnotationException:没有为Entity指定标识符:com.luv2code.hibernate.demo.Entity.Client”。。此时,我觉得我要回到hibernate的注释样式,而不是.hbm.xml样式。想法?所以我想的是我应该更改我的工厂=新配置().configure(“hibernate.cfg.xml”).addAnnotatedClass(“Client.class”)。。。。。。to.configure(“…”).addResource(“Client.hbm.xml”)…当我这样做时,我得到一个新错误:线程“main”中的异常“org.hibernate.MappingException:找不到实体类:客户端是否将该资源添加到hibernate配置文件中?教程中显示了它。使用tag.So我想的是我应该更改我的factory=new Configuration().configure(“hibernate.cfg.xml”).addAnnotatedClass(“Client.class”)。。。。。。to.configure(“…”).addResource(“Client.hbm.xml”)…当我这样做时,我得到一个新的错误:线程“main”org.hibernate.MappingException中的异常:找不到实体类:Client是否将该资源添加到hibernate配置文件中?如教程所示。使用标记。