我需要动态地将实体类(注释驱动,没有 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.
如果你想添加带注释的类,你应该使用 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/