Do we need to display Hibernate? In this scenario?

I am new to sleep mode. Please help me.

I have 2 tables named Employee and Country. I need to save Employee with the selected country. In my case, I will not return data about employees anywhere and show them in the user interface. Do I need to support onetoone mapping between Employee and Country objects? Can I directly save an employee in the selected country?

can i do as below in my Employee domain object? If not, please tell me with potential problems?

@column (name = "CountryId")

private int countryId;

+7
source share
2 answers

I believe you can have a couple of options.

  • You can simply leave the country primitive from your domain class completely. If you do not need it, there is no reason to tell Hibernate to get it.
  • If you are not going to change it at any comparison in your domain class and apply the @Transitive annotation, you can get what you need. This tells Hibernate that nothing about the country identifier is required at the database level.

Any of these methods helps ensure referential integrity between the employee object and the country identifier. However, I would recommend using the first one. If you have no reason to ever need a country identifier, do not make it part of the object in the first place.

If you need to use the country identifier for relations on the back, I would recommend including this

 @Column(name = "country", insertable = "false", updatable = "false"). 

This allows you to get the country identifier without fear that you overwrite it by accident.

Hope this helps!

+2
source

Yes, you can put countryId in your employee object. But if you do not, in the future you need an object of the country. It makes no sense to save the object of the country.

If you are developing things for future prospects and may be required in the future, but not now, then it is better to preserve the country's object.

+1
source

All Articles