aspose file tools*
The moose likes Object Relational Mapping and the fly likes Breaking one table into two Entity Beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Breaking one table into two Entity Beans" Watch "Breaking one table into two Entity Beans" New topic
Author

Breaking one table into two Entity Beans

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I am working on an EJB 3 project where I need something where there is information related to a person. There are a lot of information like DOB, homepage, like, dislike etc. Now I want to retrieve a list of all users. But due to a lot of information, the page renders very slowly. So I want that when someone views a list of users, then only the first name and last name should be retrieved from the database. When someone sees the profile of the user, then only the full information of the user must be retrieved.

For this I thought that @Embedded should be great. But I didn't find much detail about embedded fields in any book. I thought that I would lazily load the embedded field. I don't know if that is possible or not. Also I am not sure how would the embedded field will be updated. So I am stuck now.

If embedded is not the solution, then I can go for one to one mapping. I can split the table into two also. So please suggest me the best way of solving this problem.

Also can I have 10 columns in the database and have two separate java classes which map to different columns of the same table using EJB 3. This may sound stupid as I don't know if this is possible or not.

Thanks in advance...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Have you tried mapping two classes, with different levels of detail, to that one table?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Thanks Edvins. Well I have not tried this. This seems a good solution. But I don't know how to do this. Is it done by creating two entities and having less fields in one of the entities and all the fields in the other. If yes then how will I use queries with entity manager to manipulate the entity?? Can anyone give me a sample of how to do this or some internet tutorial...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Well I made it work. Thanks Edvins. The idea was enough for me to do it . I never thought about that. I was thinking about embedded objects and one to one mapping :XD: . Now the performance of my code has increased many folds.

Thanks again...
Debbie Waltz
Greenhorn

Joined: May 12, 2008
Posts: 13
Edvins Reisons wrote:Have you tried mapping two classes, with different levels of detail, to that one table?


That is a dirty trick but works. Cool idea.
Another option could be simply to have the data spread over two tables in 1 to 1 realation to each other, that share the primary key.
Hitesh Rathod
Greenhorn

Joined: Sep 03, 2007
Posts: 5
Hello Ankit,

It seems that you have found a way to map two classes to one table. Could you please share how you made it possible? I have a similar situation in my project. I also am using EJB3 and similar situation to yours.

Thank you.
Hitesh Rathod
Greenhorn

Joined: Sep 03, 2007
Posts: 5
Got my answer .... thanks :-)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Sorry Hitesh I saw this very late. But still if you need any help you can ask me. Debbie it seems you are right. One of the senior person scolded me for using this design saying that it is a bad design for future changes. I don't know what he meant by that but at least it works for now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Breaking one table into two Entity Beans