Hibernate Tip : How to map multiple entities to the same table

preview_player
Показать описание

One of the readers of my blog asked me this question: Some of our entities have a lot of attributes and we don’t need all of them in all use cases. Can I create a second entity that only maps a subset of the most commonly used attributes? Is there anything I should consider?

Yes, you can map two or more entities to the same database table. If you want to map the same database table to two entities, you should create a simple inheritance hierarchy. The superclass, in this example a class called AbstractAuthor, should be abstract and contain all attributes that are shared by both entities. You should map it as a mapped superclass so that it is not an entity itself.

If you like this video, please give me your thumbs up and share it with your friends and co-workers.

Like my channel? Subscribe!

Join the free Member Library:

Want to connect with me?

#Hibernate #JPA #HibernateTips
Рекомендации по теме
Комментарии
Автор

Is there an increased risk of deadlocks due to using the 2 entities if they are only for queries?

lavinahum
Автор

And how to map multiple linked entities to the different tables

igordryagunov
Автор

Great video thanks !. Would this be the same to JPA? I have a problem where I need to map compatible computer parts that are compatible with each other. What is the best? To create two different entities ComputerPart and ComputerPartCompatible with @OneToMany? Or I keep ComputerPart Entity and map it to itself with @ManyToMany?

markkuuss