如何从另一个JAR的外部类中使用JPA实体创建Micronaut项目

发布于 2025-02-11 01:33:00 字数 1802 浏览 2 评论 0原文

  1. 我有一个Micronaut项目,并且我正在尝试使用我的外部实体类构建该项目,该类别是Spring JPA中添加了pom.xml中的依赖性,但我总是有一个错误:由:io引起。 micronaut.context.exceptions.configurationException:jpa配置找不到的实体:packages [packageName]

注意:在我的Micronaut项目中,我没有@Entity类,因为我想使用我完成的Spring JPA实体类的外部罐子,以便可以将其重复使用其他应用程序。

编辑:MVN清洁安装或以日志运行:

io.micronaut.context.exceptions.beaninStantiationException: bean定义[org.hibernate.sessionfactory]无法加载:错误实例化bean类型[org.hibernate.boot.sessionfactorybuilder]

消息:jpa配置找不到的实体:packages [packagename]中找不到'default'。检查您是否已正确指定了一个包含JPA实体的软件包。 “ foo.bar”,纳入= entity.class)在您的申请类中声明 所需的路径:sessionFactory.hibernateSessionFactory(SessionFactoryBuilder sessionFactoryBuilder) - > sessionFactory.hibernateSessionFactory([SessionFactoryBuilder session factorybuilder]) - > sessionFactoryBuilder.HibernateSessionFactoryBuilder([元数据元数据],valioratorFactory validatorFactory,Interceptor HibernateInterceptor)

  1. I have a Micronaut project, and I'm trying to build the project with my external Entity classes which are in Spring JPA added on dependency in pom.xml, but I'm always having an error: Caused by: io.micronaut.context.exceptions.ConfigurationException: Entities not found for JPA configuration: 'default' within packages [packageName]
  • [Failed] I have already added my Entity jar a dependency with "scope compile" in pom.xml
  • [Failed] Already created a class file with "@Introspected(classes = User.class)", the User.class resides in my external Entity jar, as per micronaut documentation we can use the already compile classes by adding the annonation @Introspected, but this doesn't work
  • [Failed] I also combine the @Entity and @Introspected at the same class if this can determine the Entity class, but it doesn't work
  • [Failed] I also tried to create an AnnotationMapper, this will automate the mapping of package with "javax.persistence.Entity", see a reference https://github.com/micronaut-projects/micronaut-core/blob/3.5.x/inject/src/main/java/io/micronaut/inject/beans/visitor/EntityReflectiveAccessAnnotationMapper.java
  • removing dependency micronaut-data-spring-jpa doesn't requires me an Entity class, but the auto migration of Micronaut to Spring JPA repository will be having an error.

Note: In my Micronaut project, I don't have @Entity classes, since I want to use the external jar of Spring JPA Entity classes that I made so I can reuse it to other applications.

Edit: mvn clean install or run as log:

io.micronaut.context.exceptions.BeanInstantiationException:
Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type [org.hibernate.boot.SessionFactoryBuilder]

Message: Entities not found for JPA configuration: 'default' within packages [packageName]. Check that you have correctly specified a package containing JPA entities within the "jpa.default.entity-scan.packages" property in your application configuration and that those entities are either compiled with Micronaut or a build time index produced with @Introspected(packages="foo.bar", includedAnnotations=Entity.class) declared on your Application class
Path Taken: SessionFactory.hibernateSessionFactory(SessionFactoryBuilder sessionFactoryBuilder) --> SessionFactory.hibernateSessionFactory([SessionFactoryBuilder sessionFactoryBuilder]) --> SessionFactoryBuilder.hibernateSessionFactoryBuilder([MetadataSources metadataSources],ValidatorFactory validatorFactory,Interceptor hibernateInterceptor)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没企图 2025-02-18 01:33:00

这就是我为解决自己的问题所做的:

  1. 我再次从头开始,然后使用 https:// micronaut。 IO/启动/
    要生成工作空间(就像 https://start.spring.io/ for Spring)
  2. 代替 )直接使用我的用户。从外部JAR中使用,我首先在Micronaut Project(包括存储库)上创建它,并证明应用程序启动和工作(我还公开了用于触发测试的服务),
  3. 然后更改用户的参考。班级,从本地到外部罐子。当我最初尝试它时,没有“ @intropstect”,“没有找到持久的类”,但是使用“ @intropsected”,它可以起作用。
  4. 我能够从外部类中使用该实体,但不能使用存储库类(目前,将不会继续研究如何将外部JPA存储库类用于Micronaut)

This is what I did to resolve my own issue:

  1. I started from scratch again, and I use the https://micronaut.io/launch/
    to generate the workspace (its like https://start.spring.io/, for spring)
  2. Instead of directly using my User.class from external jar, I create it first on micronaut project, (including repository), and prove that application starts and works (I expose also a service to trigger for testing)
  3. Then, I change the reference of User.class, from local to external jar. When I initially try it, without "@Introspected", "No persistent classes found" was encountered, but with "@Introspected", it works.
  4. I able to use the entity from my external class, but not the repository class (for now, will not pursue further research how to use external jpa repository class to micronaut)
与君绝 2025-02-18 01:33:00

我遇到了同样的问题,然后我就解决了:

  • 添加@introppected(class = {entity} .class)在您的 application.java class micronaut的类别proj
  @introppected(class = {entity} .class)
公共类应用程序{
    公共静态void main(string [] args){
        micronaut.run(application.class,args);
    }
}
 

这将导入您的实体类,并使 jpa 将其识别为 jpa entity

只是一个提醒:您必须将{entity}类带有@entity

导入javax.persistence.entity;

I had the same problem and I solved it like this:

  • Adding @Introspected(classes={Entity}.class) on your Application.java class of your Micronaut Proj
@Introspected(classes = {Entity}.class)
public class Application {
    public static void main(String[] args) {
        Micronaut.run(Application.class, args);
    }
}

This will import your entity class and make JPA to recognise it as a JPA Entity.

Just a reminder: You must have your {Entity} class annotated with @Entity from

import javax.persistence.Entity;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文