1

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.

4
  • When both the microservice and the SDK define JPA entities that map to the same table, Hibernate (especially 6.x) will refuse it by design. Even if the entities have different Java class names, mapping the same table twice in the same EntityManagerFactory is not supported (outside of specific inheritance/subselect patterns). Commented Aug 30 at 1:30
  • Make a common layer like Service->Entity and point to it from different microservices. Commented Aug 30 at 4:37
  • 1
    Turn off DDL generation, and instead validate. I think you're getting an error because Hibernate would need to update the same table for two different entities. Commented Aug 30 at 12:28
  • It depends on what you want to achieve. You may want to look into @MappedSuperclass and @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) Commented Aug 30 at 14:16

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If I could decide by myself I would in fact remove all of the old entities from the micro-services, but the rest of my team want to do this progressively. But maybe some additional context that is needed is that the main error comes from the logic the team is using to create the SDK, we are migrating some entities to the SDK that I would not consider integral to the principal functionality. This is cause we use jpa model gen for Criteria API queries. I already recommended using DTOs for this but they refused.
In this case, it seems that the problem is not technical, but business in nature – since the proposed solutions are rejected by the other team without argument, no one here is likely to be able to solve this type of problem.

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.