Persistence with inheritance, one-to-one relationship and manual primary key

0

I am developing a web heritage project to finish my degree. I'm using Java (IDE: Spring Tool Suite), Spring, Hibernate and Mysql.

I'm having a data modeling problem that I can not solve:

An Equipment class must have an asset number as its attribute. Cpu and Modem inherit attributes of the Equipment class. The device attribute should be represented as the primary key in the database, however, the value should be typed by the user.

Note: I am using Hibernate annotations

I tried to use @OneToOne, @Id, @NaturalId annotation. All attempts that I tried to use do not compile (do not run) or allow a cpu and a modem to be registered with the same number of patrimony.

So what is the path I can use to solve this problem?

Thank you for your attention!

    
asked by anonymous 07.10.2017 / 22:47

2 answers

0
@Id 

works for you as long as you use a single table for the entire class hierarchy, ie

 @Inheritance(strategy=InheritanceType.SINGLE_TABLE) 

and a descriptor to distinguish between classes.

@OneToOne 

is for mapping compositions, not inheritance.

    
08.10.2017 / 03:34
0

In the save method of each class I added a search of the number of assets within the equipment database.

If the asset was not inside the database the object would be persisted if not otherwise.

Thanks to whoever helped me! =)

PS: Forgive me anything, I'm new here.

    
11.10.2017 / 15:49