java - 以编程方式添加实体时出现 MappingNotFound 异常

标签 java hibernate

我需要动态地将实体类(注释驱动,没有 hbm 文件)添加到持久性中。

我尝试了以下方法来动态添加实体。

Configuration cfg = new Configuration()
                .addClass(org.entity.Person.class);

我收到 MappingNotFoundException:找不到映射(RESOURCE):org.entity.Person.hbm.xml

还有其他方法可以做到这一点吗?

我浏览了很多帖子,但没有找到解决问题的确切方法。非常感谢任何帮助。

最佳答案

addClass 方法查找映射文件

Read a mapping as an application resource using the convention that a class named foo.bar.Foo is mapped by a file foo/bar/Foo.hbm.xml which can be resolved as a classpath resource.

http://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/cfg/Configuration.html#addClass-java.lang.Class-

如果你想添加带注释的类,你应该使用 addAnnotatedClass

 public Configuration addAnnotatedClass(Class annotatedClass)
 // Read metadata from the annotations associated with this class.
 Parameters:
    annotatedClass - The class containing annotations
 Returns:
    this (for method chaining)

例如:

 Configuration cfg = new Configuration()
            .addAnnotatedClass(org.entity.Person.class);

关于java - 以编程方式添加实体时出现 MappingNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43979401/

相关文章:

java - 缩放和旋转点数组

java - 如何修复 "org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.MySQL8Dialect]"?

mysql - Hibernate/JPA 允许多少个并发 MySQL 连接?

spring - JTATransactionManager 未提交到数据库

sql - Spring JPA 查询总是使用序列扫描而不是索引扫描

java - 从 Groovy 类创建 Spring bean 导致 "cannot find symbol error"

java - 如何使用流在 Java 8 中按值范围分组

java - Spring 上下文层次结构中的 bean 销毁顺序

c# - java中通过JNA访问.net dll

java - 异常设计与应用需求不匹配