[英]Error switching from Hibernate Annotations to hbm.xml file
因此,在處理我的項目時,我最初在我的java類中使用了Hibernate Annotations @ 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>
最后這里是Eclipse Error代碼:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.luv2code.hibernate.demo.entity.Client
我沒有對實際創建對象的類進行任何更改,並通過會話將其添加到數據庫中...我需要嗎?
謝謝您的幫助!!
我想您可能已經忘記了映射標記,以列出在項目中使用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>
您顯示的xml文件是hibernate配置文件,它不是hbm.xml文件。 您必須為您創建的每個持久實體創建“classname.hbm.xml”文件 - 在您的情況下,它是您的Client類。 所以你必須制作Client.hbm.xml文件。 之后,您必須將該資源添加到配置文件和Hibernate Utility文件中。 您可能會覺得這很有幫助。 http://www.mkyong.com/hibernate/how-to-add-hibernate-xml-mapping-file-hbm-xml-programmatically/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.