gpt4 book ai didi

java - 从 Hibernate Annotations 切换到 hbm.xml 文件时出错

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:51 24 4
gpt4 key购买 nike

因此,在处理我的项目时,我最初在 java 类中使用了 Hibernate Annotations @Entity、@Table、@Column、@SequenceGenerator 和 @GenerateValue,并且能够成功将项目添加到我的 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 错误代码:

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

我没有对实际创建对象并通过 session 将其添加到数据库的类进行任何更改...我需要这样做吗?

感谢您的帮助!!

最佳答案

您显示的 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/

关于java - 从 Hibernate Annotations 切换到 hbm.xml 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37661588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com