Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need Hibernate mapping In this scenario?

I am new to hibernate. Please help me.

I have 2 tables named Employee and Country.I need to save the Employee with selected country.In my case , I will nowhere get employee details back and show it on UI. Do i need to maintain mapping(onetoone mapping) between Employee and Country objects? Can't i directly save employee with selected country?

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

@column(name="countryId")

private int countryId;

like image 647
Java P Avatar asked Jun 30 '12 19:06

Java P


2 Answers

I beleive you may have a couple of options here.

  1. You can just leave the country primitive out of your domain class entirely. If you do not need it, there is no reason to tell Hibernate to fetch it.
  2. If you're not intending on modifying it at all mapping it in your domain class and applying the @Transitive annotation might get you what you're after. This tells Hibernate that nothing about the Country ID needs persisting at the database level.

Either of these methods help to ensure referential integrity between the employee object and the country ID. I'd recommend using the first, though. If you have no reason at all to ever need the country ID, do not make it part of the object in the first place.

If you do need to use the Country ID for a relationship on the back side, I'd suggest making sure that you include this

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

This allows you to get country ID without fearing you overwrite it accidentally.

Hope that helps!

like image 114
Jason Lowenthal Avatar answered Sep 30 '22 18:09

Jason Lowenthal


Yes you can put countryId in your employee object. But if you don't need the country object in future then, It does not make any sense to save the country object.

If you are developing the things for future prospective and may be required in future but not now then better to save the country object.

like image 34
subodh Avatar answered Sep 30 '22 17:09

subodh