-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: one to many concept
PostPosted: Thu Dec 03, 2009 8:42 am 
Newbie

Joined: Thu Dec 03, 2009 8:40 am
Posts: 3
Hi,

I am a newbie to hibernate and am struck with the on-to-many usage. Consider this scenario: I have two tables department and employee and the relation between them is one-to-many.

Department table:
department_id int primary key
department_name not null varchar(100)

Employee table:
employee_id int primary key
employee_name not nul varchar(100)
department_id int foreign key references department(department_id)

The data in the Department table is pre-defined as there will be fixed number of departments in any organization.
Now, I need to create new employee and assign them to a department. For this purpose, I created a jsp page with a text box for name and a drop down list pre-populated with departments. To insert the record, it is pretty straightforward. Using request.getParameter(), I retrieved the values of name and department and assigned them to the Employee object. Using the session.save(), I am able to save the object without using the one-to-many relationship and I suppose it has to be done that way only.

From the examples I found on the web for one-to-many relationship, the parent object is saved first and then the child object.

Now my question is : Should we use one-to-many relationship only when we need to store objects in both the tables?

Regards,
Viswanath


Top
 Profile  
 
 Post subject: Re: one to many concept
PostPosted: Thu Dec 03, 2009 9:10 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Why don't you do it kind of like this way?:

Your select list of departments is a list of ID's and Labels / Value pairs, like this in HTML
Code:
<option value="1">Department 1</option>


In your code you then say something like:

Code:
Session session = HibernateUtils.getSessionFactory().getCurrentSession();
session.getTransaction().begin();
Department loadedDepartment =  session.get(Department.class, jsfBeanOrActionForm.getDepartmentId());
Employee employee = jsfBeanOrActionForm.getEmployee();
employee.setDepartment(loadedDepartment);
session.save(employee);
session.getTransaction().commit();


That way, you cannot save a new department, and your many to one link would be saved together with the employee.


Top
 Profile  
 
 Post subject: Re: one to many concept
PostPosted: Thu Dec 03, 2009 9:11 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
To answer your question:

No, you can use many to one also to a table where you don't store new data but only retrieve it.


Top
 Profile  
 
 Post subject: Re: one to many concept
PostPosted: Fri Dec 04, 2009 2:22 am 
Newbie

Joined: Thu Dec 03, 2009 8:40 am
Posts: 3
Thanks, it worked and regarding this many-to-one mapping, its for retrieving the objects, right?
When I use Query q = session.createQuery("from Employee"); I get an Employee object.
If I need specific fields, should the query be of this form;
Query q = session.createQuery("select firstName, lastName from Employee");

And how to retrieve specific fields from both the objects? Is it this way:

Query q = session.createQuery("select e.firstName, e.lastName, d.departmentName from Employee e, Department d where e.departmentId=d.departmentId");

Or is there any sophisticated way?

Regards,
Viswanath


Top
 Profile  
 
 Post subject: Re: one to many concept
PostPosted: Fri Dec 04, 2009 3:58 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
You can choose how you select your properties
http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html#queryhql-select

I would do something like
Code:
Query q = session.createQuery("select e.firstName, e.lastName, d.departmentName from Employee e inner join employee.department as d");

Or, more readable:
select e.firstName, e.lastName, d.departmentName
from Employee e
inner join employee.department as d

I don't know whether the join in the where clause works in HQL. And especially, you should then be doing where e.department.id as department has a getId() method. Your employee shouldn't have a getDepartmentId() method but a getDepartment() method which returns a Department instance.

The more sophisticated way is returning a map of aliasses and select values, like this:
Code:
Query q = session.createQuery("select new map(e.firstName as firstName, e.lastName as lastName, d.departmentName as department) from Employee e inner join employee.department as d");

Or, more readable:
select new map(e.firstName as firstName, e.lastName as lastName, d.departmentName as department)
from Employee e
inner join employee.department as d

Or just do "from Employee" and do the necessary getters (if you do getDepartment() he will join automatically or execute a "select ... from department where department_id = ...." query with the department_id found in the employee.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.