Solved

Hibernate Mapping

Posted on 2009-05-07
12
372 Views
Last Modified: 2013-11-24
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 .
0
Comment
Question by:objectkk
  • 7
  • 5
12 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
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
 

Author Comment

by:objectkk
Comment Utility
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
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
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
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
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
 

Author Comment

by:objectkk
Comment Utility
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
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:objectkk
Comment Utility
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
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
Well it looks good, but you don't have any Hibernate related code in this.
0
 

Author Comment

by:objectkk
Comment Utility
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
 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 500 total points
Comment Utility
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
 

Author Comment

by:objectkk
Comment Utility
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
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now