Inheritance persistence, one-to-one relationship and manual primary key

Asked

Viewed 154 times

0

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

I’m having a data modeling problem that I’m having trouble solving:

Exemplo de modelagem

An Equipment class must have as attribute a number of assets. Cpu and Modem inherit attributes from the Equipment class. The equipment attribute must be represented as the primary key in the database, however, its value must be entered by the user.

Note: I am using Hibernate notes

I tried using @Onetoone, @Id, @Naturalid annotation. All attempts I tried to use or do not compile (do not run) or allow a cpu and modem to be registered with the same asset number.

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

Thank you for your attention!

2 answers

0

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

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

Thanks to those who helped me! =)

PS.: Forgive me anything, I’m new around here.

0

@Id 

works for your case, as long as you use a Single table for the entire class hierarchy, i.e.,

 @Inheritance(strategy=InheritanceType.SINGLE_TABLE) 

and a heading for distinguishing between classes.

@OneToOne 

is for mapping compositions, not inheritance.

  • Specifically for my graduation work I will not be able to use Single table so I am using 3 tables, one for equipment, 1 for cpu and 1 for modem. Regarding the classes, @Inheritance(Strategy=Inheritancetype.JOINED) being used in the equipment table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.