hibernate one to one mapping for employee and country

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.we are using .net as front end and java as back end. do we need to load country object for selected country and set it into employee object as we have one-one mapping between employee and country? is there any other way to do this?


Yes, that's what you should do. Note though that:

  • unless yo have only one employee for a given country, you don't have a one-to-one association, but a many-to-one association
  • using session.load(countryId) gets you a reference to a given country (if you know its ID, of course) without even executing a select query. If the country hasn't been loaded yet into the session, it will simply return an unitialized proxy to the country, which is sufficient to initialize the association. Of course, if you want to make sure the country actually exists, you should use session.get(countryId) , which executes a select query and loads the data of the country in the session (unless it has already been loaded, of course).
  • 链接地址: http://www.djcxy.com/p/63684.html

    上一篇: 不想将数据保存到第二个表中

    下一篇: 休眠员工和国家的一对一映射