javaspring-boothibernatejpaspring-data-jpa

Is there a way to have two spring boot jpa entities point to the same table?


I have some micro-services and some functionalities are shared across these micro-services, so with my team we agreed that we needed this functionality moved to a SDK.

I've started the migration of a couple functionalities to the SDK, like for example creating a new payment and all that is related to this. When I try integrating this into a micro-service, I'm encountering the next error: error log

Basically, Hibernate tells me that is not possible to have two entities pointing to the same table.

What I've done to solve this is simple, I made some configurations on my DatasourceReadConfiguration file and I have also made some changes to the entities, for example this is how the entity is in the micro-service:

@Entity
@Table( schema = SCHEMAS.admission,
    uniqueConstraints = {
    @UniqueConstraint(name = "UKSL014", columnNames = {"curp", "idc"})
})
public class PersonProspect extends AbstractGeduxCampusModelEntity {}

And this is how I modified it in the SDK:

@Entity
@Table( schema = SCHEMAS.admission,
    name = "person_prospect",
    uniqueConstraints = {
    @UniqueConstraint(name = "UKSL014", columnNames = {"curp", "idc"})
})
public class PersonProspectPSDK extends AbstractGeduxCampusModelEntity {}

I create the SDK using the command mvn install, and then I import it in the micro-service in my pom.xml like this:

<dependency>
    <groupId>com.quiox.gedux</groupId>
    <artifactId>gedux-payment-sdk</artifactId>
    <version>0.0.2</version>
</dependency>

I just want to know if there is a simple solution to the problem I'm having or if I have to restructure the whole SDK. I think the issue comes from the way we use Criteria API and metamodels by jpamodelgen instead of using native SQL in this particular SDK. This causes to have secondary classes like the one mentioned in the error both in the SDK and in the micro-services.


Solution

  • But first -> why just don't remove old Entity in microservice when You have it in SDK?

    If You want disable/enable sdk for concrete microservices, configure it per microservice with @EnableJpaRepositories, @ComponentScan, @EntityScan (just dont specify SDK package there).

    If You don't want remove entity from microservice, maybe pack this "shared logic" with "shared fields" into @Embeddable class and add it in microservice entity as @Embedded object => but if You can do this, i would recommend to reengineer this structure into f.e inheritence ORM (@MappedSuperclass, @Inheritance) or interface inheritance in plain Java.