Hibernate Mapping

Hello Experts:
I would like to know that whatever I know is known as perfect or not! :-))

Well jokes apart,
I am new to Hibernate, and I have got to work on the same.
It is actually a employee management system.

I'm using MyEclipse 7.0 IDE , JSP as frontend, MS SQL Server as DB and Hibernate for mapping.

I have created Java bean let's call DepartmentBean.java , I've interface called EmpSysInterface.java, Department.hbm.xml file,

Servlet will have access of DepartmentBean.java (which exposes the functinality) via Interface EmpSysInterface.java, isn't it?
Business logic provided by this bean.java will pull JPA into picture and Department.hbm.xml file will map the DB tables, isn't it?
What does it mean when said"hbm.xml file is for Parent and child object"?

thnx a lot,
Hope to get explained answers .
objectkkAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ChristoferDutzConnect With a Mentor Commented:
It seems as if the Hibernate magic is going on in one of the services that are used.
Have a look at the DomainService. I would guess that this is responsible for insertig/updating beans. The code above does a lot of checking, but I think the actual work is done by the DomainService.
0
 
ChristoferDutzCommented:
Hi objectkk,

I have to admit, that I didn't completely understand your question, but I'll try to comment as good as I can.

You are talking about JPA and "*.hbm.xml" files. As far as I understood the thing, JPA can use Hibernate for the task of the object-relational mapping, but you have to configure it using annotations in your bean and configure the class in the META-INF/persistence.xml ... I doubt that you usually configure a JPA application using "*.hbm.xml" files.

I think in your case, you are simply using plain Hibernate. Well here you are descripbing a standard SessionFacade/Dao pattern (Depending on the amount of logic done in your EmpSysInterface ... just loading, or real processing). It's pretty much the ordinary way to go.

In order to achieve the Object-To-Db mapping, the mapper needs additional Data, this is where hibernates "*.hbm.xml" files come into action. Other approaches configure this by adding Java5 Annotations (Which I certainly prefer). Above that you have to configure the Database connection somehow, this is usually done separate from your dao code (hibernateConfiguration, Spring Configuration, ...)

If you have a direct question, please feel free to ask.
0
 
objectkkAuthor Commented:
Hi ChristoferDutz,

Thats great to have such a prompt reply which has enlighened me much.Thnx.

Actually after writting you I've been reading about Hibernate as an implementation tool for JPA(SPEC).
Because you've asked I would like to be much clear to question you.

I've been asked to use SaveOrUpdate command , but I've no idea about what I shall write in there ?
will you please help me understand how one should do it?

thnx again
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ChristoferDutzCommented:
Well a hibernate Session Object posesses a method called saveOrUpdate. It's just something like a helper method. You can think of the algorithm as the following:

1. Check if the object was persisted (pk-fields are not null)
2a. If it was not saved yet -> Do a DB insert equivalent to a hibernate session.save(bean);
2b. If it was saved -> Do a DB update equivalent to hibernate session.update(bean);

You don't have do do anything "in" this method, all you do, is to provide the bean you want to save-or-update and Hibernate does it for you:
session.saveOrUpdate(bean);
0
 
ChristoferDutzCommented:
Here a small code example:
Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Department dep = new Department("Cool Department");
session.saveOrUpdate(dep );
tx.commit();
session.close();

Open in new window

0
 
objectkkAuthor Commented:
Okey...thnx a lot..
Is it like my client should have Save or Update button for that perticular web page I've created?
If not then does SaveorUpdate() method works automatically , how?

0
 
ChristoferDutzCommented:
Save or Update should be all you need, becaue it makes sure the right method is called automatically. When using update on a bean that has not yet been saved, you would surely get exceptions. Just stick to saveOrUpdate and you're good to go.
0
 
objectkkAuthor Commented:
Could you please check if below code is written correctly?
thnx a lot

DomainBean domainBean = (DomainBean) req.getSession().getAttribute(
				"domainBean");
		if (null == domainBean) {
			domainBean = new DomainBean();
			req.getSession().setAttribute("domainBean", domainBean);
		}
		domainBean.clearErrorContainer();
 
		UserBean userBean = (UserBean) req.getSession()
				.getAttribute("user");
		DomainService ds = new DomainService();
		Hashtable<String, String> parameter = ds.parameterListEvaluation(req);
		try {
			if (userBean
					.getPermissions()
					.hasPermission(
							EMSIntActPermInterface.DOMAIN_ALL_CHANGES)
					|| userBean
							.getPermissions()
							.hasPermission(
									EMSIntActPermInterface.DOMAIN_ASSIGNED_CHANGES)
					|| userBean
							.getPermissions()
							.hasPermission(
									EMSIntActPermInterface.DOMAIN_CREATE)) {
				ds.updateDomain(parameter, domainBean,
						userBean);
			}
			if (domainBean.getErrorMessages().isEmpty()) {
				if (null == domainBean.getUpdateDomainBO()
						.getBereichId()
						|| 0 > domainBean.getUpdateDomainBO()
								.getDomainId().intValue()) {
					if (userBean
							.getPermissions()
							.hasPermission(
									EMSIntActPermInterface.DOMAIN_CREATE)) {
						ds.insertDomain(domainBean
								.getUpdateDomainBO(), userBean
								.getBearbeiterBO());
						domainBean
								.setResultMessage(EmsInfoMessages.EMS_INFO_DATA_SAVE);
					} else {
						domainBean
								.putErrorMessage(EmsErrorMessages.EMS_ERROR_CREATE_GB);
					}
				} else {
					if (userBean
							.getPermissions()
							.hasPermission(
									EMSIntActPermInterface.DOMAIN_ALL_CHANGES)
							|| userBean
									.getPermissions()
									.hasPermission(
											EMSIntActPermInterface.DOMAIN_ASSIGNED_CHANGES)) {
						ds.updateDomain(domainBean
								.getUpdateDomainBO(), userBean
								.getUserBO());
						domainBean
								.setResultMessage(EmsInfoMessages.EMS_INFO_DATA_SAVE);
					} else {
						domainBean
								.putErrorMessage(EmsErrorMessages.EMS_ERROR_CREATE_GB);
					}
				}
			}
		} catch (InvalidEMSPermParameterException e) {
			logger.error("SaveOrUpdateDomainCommand", e);
		} catch (EmsDaoException e) {
			logger.error("SaveOrUpdateDomainCommand", e);
			domainBean
					.putErrorMessage(EmsErrorMessages.EMS_ERROR_DATA_SAVE);
		}
		if (domainBean.getErrorMessages().isEmpty()) {
			domainBean.resetDomainBean();
			try {
				ds.initializeDomainBean(domainBean);
								
				boolean loadGBHistory = userBean.getPermissions()
						.hasPermission(
								EmsPermission.DOMAIN_HISTORY_READ);
						if (userBean.getPermissions().hasPermission(
						EmsPermission.GESCHAEFTSBEREICH_ALLE_LESEN)) {
					domainBean.setDomainBOList(ds
							.loadDomainBOList(loadGBHistory, false,
									false));
				} else if (userBean.getPermissions().hasPermission(
						EmsPermission.DOMAIN_ASSIGNED_READ)) {
					DomainBO dbo = ds
							.loadDomainBOByDomainleader(userBean
									.getUserBO(), loadGBHistory, false,
									false);
					if (null != dbo) {
						domainBean.getDomainBOList().add(dbo);
						domainBean.setUpdateDomainBO(dbo);
					} else {
						domainBean
								.putErrorMessage(EmsErrorMessages.EMS_ERROR_NO_GB_FOR_GBL);
					}
				}
			} catch (Exception e) {
				throw new CommandException(e);
			}

Open in new window

0
 
ChristoferDutzCommented:
Well it looks good, but you don't have any Hibernate related code in this.
0
 
objectkkAuthor Commented:
Where I should have them?
Actaully I did not understand the whole code which is written above...will youplease explain me what exactly have been done above?

How one should map this with hibernate ,I do habe hbm.xml file too...
Would be reallly helpful ...
thnx
0
 
objectkkAuthor Commented:
How did u come to knowthat Domain Service is having SaveOrUpdate code?
I would like to close this Question after I get reply from you.
Will you please help me understand other things I will ask thru new question.
Thank you!

0
 
ChristoferDutzCommented:
Well I don't know that, but it's a common design priciple to wrap operations to beans inside services and as there was no Hibernate code and that was the only service, it was just a guess that this is responsible for doing the persisting ... especially if it has methods with names like: insertDomain and updateDomain
Don't know if it's correct, but it was all I could see.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.