This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes can i use one entity bean to access more than one tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "can i use one entity bean to access more than one tables " Watch "can i use one entity bean to access more than one tables " New topic

can i use one entity bean to access more than one tables

Chandra Pay

Joined: Aug 09, 2001
Posts: 2
Generally a entity bean is an instance of a particular record in the database. so it will normally refer to one particular row in a table.
suppose i want to do it for more than one tables? that is with one enity bean can i create,load,store,find rows in more than one tables? if so what are the ways to do it?
I am new here and this is my first question..which is the same as Senthil Posted above. I would like to know if the above is possible and if yes, how. I am using J2EE 1.3. I did search the whole list and couldn't find the answer to this, as suggested by someone.
The main problem is that when I want to access the second table using the business methods of the bean, the load and store methods are trying to access the first table.
Anyways what I had planned was that I would use an entity bean for each table. But somehow that doesn't sound as a good idea to me. I am sure there should be some other simpler way.Please help me..thanks in advance .

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960

I guess what you're asking is "can I use the same logic, just aim it at the table of my choice?" - as opposed can "can I use this logic on multiple tables concurrently?"
Technically, yes. For a BMP bean, you'd just make the table name one of the parameters for the various functions that construct the bean. For a CMP, you'd use the same prototypical bean and deploy it multiple times, changing the deployment descriptor for each one.
In the BMP bean's case, what you have effectively done is added an additional column to make a virtual database table comprised of the union of the individual tables plus their names (that is, the additional column holds the table name).
Which leads to the question of why, instead of making a complex one-time solution in logic that only works inside the EJB system, you don't simply consolidate the tables into one table.
There's an alternative way I could read this - if your various tables are not all identically structured. In order to use the same bean for heterogeneous tables, you'd end up constructing a complex and baroque component that would be a nightmare to maintain. If you're worried about performance, bypass EJBs. EJBs are to make life simpler at the expense of some efficiency.

Customer surveys are for companies who didn't pay proper attention to begin with.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I had the same question as Senthil and Chandra's. There are multiple tables in my database (The database is already structured and given to me). Each table has a primary key, and the table that has relation to other tables has a foreign key.
I have an entity bean for each table. And I use the primary key and foreign key to go from one table to another.
I also wonder if I have a good design, or I should ask the data person to restructure the database.
Chandra Pay

Joined: Aug 09, 2001
Posts: 2
Thanks a lot Tim and Jen. My problem is like this...The two tables are totally different. I have a table with login and pwd as fields and the other table contains 6 completely different fields. Queries to the second table can be made only if the user login and pwd are found in the first table.I am using BMP .
Now I have a servlet that accesses both the beans. The Servlet connects to the first entity bean(which checks whether the login and pwd are right) and if the user is valid, then it connects to the second entity bean. I was just wondering whether this is a correct approach i.e I am creating an entity bean for each that ok? Here's part of the Servlet code
public void init() throws ServletException {
//System.out.println("in init of HelloServlet");
try {
InitialContext ic = new InitialContext();
InitialContext ic2 = new InitialContext();
Object objref = ic.lookup("java:comp/env/ejb/Hello");
Object objref2 = ic2.lookup("java:comp/env/ejb/Hello2");
//System.out.println("lookup ok");
home = (HelloHome)PortableRemoteObject.narrow(objref,HelloHome.class);
home2= (HelloHome2)PortableRemoteObject.narrow(objref2,HelloHome2.class);

Is the above correct?? Will I be able to access both the beans now..Somehow I keep getting errors and dunno whether I am right here.. I think, my servlet is not accessing the second bean. If possible, do tell me whatever suggestions u have...
thanks a lot in advance..
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I am so glad that Chandra spells out her question. My database is structured similar to Chandra's. The difference is that I am using CMP. I am waiting if expertees in the area could shed some light.
I agree. Here's the link:
subject: can i use one entity bean to access more than one tables
Similar Threads
J2EE Design Question -- Am I On The Right Track?
My SCEA Part 1Study Notes
can i use one entity to create,update more than one tables
About Multiple Tables involved in a single CMP Bean
Running multiple ejb processes