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;


I beleive you may have a couple of options here.

  • 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.
  • 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!


    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.

    链接地址: http://www.djcxy.com/p/10924.html

    上一篇: 如果软键盘可见,ScrollView不会滚动

    下一篇: 我们是否需要Hibernate映射在这种情况下?