0

I am trying to create a structure to allow customization in my spring boot project. When there are two Entities with the same name, I want the Entities in the specified package to crush the entity in the other package. How can I do this? I couldn't do it with ClassLoader because spring boot scans Entities with @EntityScan.

Main class

@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.project")
@EntityScan(basePackages = {"com.project"})
public class ProjectMain {
    SpringApplication.run(ProjectMain.class, args);
}

Entity in com.project.data.entity package

package com.project.data.entity;

import jakarta.persistence.Entity;

@Entity
public class User {
   ...
}

Entity in com.project.data.domain package

package com.project.data.domain;

import jakarta.persistence.Entity;

@Entity
public class User {
   ...
}

I cannot solve the problem with ClassLoader because Spring Boot manages Entities differently. I get the following error when there is an Entity with the same name. As I said, my goal is to ignore more than one entity with the same name.

Error creating bean with name 'openEntityManagerInViewInterceptor' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration$JpaWebConfiguration.class]: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Entity classes [com.project.data.entity.User] and [com.project.data.domain.User] share the entity name 'User' (entity names must be distinct)
5
  • did you explored @Conditional annotation in springboot? See if that helps you in some way. Commented Feb 5 at 9:31
  • Why ignore it? They are there for a reason, if you ignore one of them parts of your application aren't going to work. Commented Feb 5 at 9:32
  • @PrateekJain not working Conditional annotation for entity Commented Feb 5 at 9:37
  • @M.Deinum I want the customer to be able to customize an existing entity in my Core project without touching the base entity. For this, write Entity with the same name and ignore my entity. In this way, I do not need to change the entities used by existing repositories. Commented Feb 5 at 9:42
  • Yes you will as those imports don't magically change, those repositories will still use the same entities and not the new ones. Leading to errors of not being able to find entities. Commented Feb 5 at 9:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.