Solved

Creating Entity bean for a 'table' without PrimaryKey

Posted on 2004-09-17
6
364 Views
Last Modified: 2013-11-24
Hi,

I have a relation table without primarykey. My table name is "favorite" and it has:
worker_id   int   (FK to "worker" table)
task_id   int   (FK to "tasks" table)
priority   int

If i want to create an Entity Bean, we usually create all the 'columns' with getter and setter along with the column type. For example, (just an example):
           
     /**
       * @author bj 17.09.2004
       * Name
       *
       * @ejb.interface-method view-type = "both"
       * @ejb.persistence column-name = "name"
       * @return
       */
      public abstract String getName();

      /**
       * @ejb.interface-method view-type = "both"
       * @param name
       */
      public abstract void setName(String name);


The method ejbCreate() should have the return type of PrimaryKey. My every other table has a PK field and i do like:
     public Integer ejbCreate() throws CreateException {
            return null;
    }

Since my "Favorite" table doesn't have a PK field, how can i create en Entity bean for that table? How to solve the problem of ejbCreate() return type?
0
Comment
Question by:ldbkutty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12083032
It does have a primary key.  The primary key is a composite of:

  worker_id and task_id

isn't it?

Have a look at this:  http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/CMP8.html (for CMP) and this http://java.sun.com/j2ee/1.4/docs/tutorial/doc/BMP4.html (for BMP)

0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12083168
I understand that PK is combination of worker_id and task_id, but i cannot/shouldn't  create the FK fields(getter, setter) in my Entity Bean. Instead I have to make the relation for those FK's.

For example, in my "Tasks" table, i have 'taskworker_id' FK column to "TaskWorker" table. In my Entity bean, i have:

     /**
       * @author bj 16.09.2004
       *
       * Task TaskWorker Relation
       *
       * @ejb.interface-method
       *       view-type = "local"
       *
       * @ejb.relation
       *       name = "taskworker_task"
       *          role-name = "task from taskworker"
       *
       * @jboss.relation
       *       fk-column = "'taskworker_id"
       *       related-pk-field = "id"
       *       fk-constraint = "true"
       */
      public abstract TaskWorkerLocal getTaskWorker();

      /**
       * @ejb.interface-method view-type = "local"
       * @param taskWorker
       */
      public abstract void setTaskWorker(TaskWorkerLocal taskWorker);
0
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 12083273
> Instead I have to make the relation for those FK's.

But you need to be able to access the "priority" field, so you will need to create an Entity bean for that table...

public class FavouritePK implements Serializable
{
    Worker worker ;
    Task task ;

    public FavouritePK() {}
    public FavouritePK( Worker worker, Task task )
    {
        this.worker = worker ;
        this.task = task ;
    }

    public Worker getWorker()
    {
        return worker ;
    }
   
    public Worker getTask()
    {
        return task ;
    }

    public boolean equals( Object o )
    {
        if( o == null || !(o instanceof FavouritePK ) )
            return false ;
        FavouritePK other = (FavouritePK)o ;
        return this.worker.equals( other.worker ) && this.task.equals( other.task ) ;
    }
 
    public int hashCode()
    {
        return worker.hashCode() ^ task.hashCode() ;
    }
}

Then, in your Entity bean:

public abstract class FavouriteBean implements javax.ejb.EntityBean
{
    Worker worker ;
    Task task ;
    int priority ;

    public FavouritePK ejbCreate( Worker w, Task t )
    {
        setWorker( worker ) ;
        setTask( task ) ;
        setPriority( priority ) ;
        return null ;
    }

    ...rest of your code...
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:TimYates
ID: 12083274
>     public FavouritePK ejbCreate( Worker w, Task t )

should be

    public FavouritePK ejbCreate( Worker w, Task t, int priority )
0
 
LVL 32

Author Comment

by:ldbkutty
ID: 12114278
sorry for late...but that worked great.

Thanks.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12114386
:-)  Cool :-)

Good luck with it :-)

Tim
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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