2 Replies Latest reply on Jul 27, 2006 7:28 PM by holgerprause

    @OneToMany Question

      Hello,

      i have a site where the uses can select TaskItems (entities from the database)
      and set it to a task.

      I use a OneToMany relationship (a task hast many taskitems)
      When i set the TaskItems to the task and save it, everything will be ok.


      But when i save another task whith the same TaskItems, it will fail becourse the primary key of the temporary join table "task_taskItems"
      is the foreign key to the task item, this means in my case when then taskitems were set to a task, no other other task can have these taskitems

      So the question is:

      How can i specify the primary key of the temporary table?

      As Workaround i could write a Class which wraps around the TaskItems
      and inside there i can use @JoinColumn but it will mess up my code :-/


       @OneToMany
       @JoinTable(
       name="task_taskItems",
       joinColumns = { @JoinColumn( name="task_id", unique = false), @JoinColumn( name="task_taskItems_id", unique = true) },
       inverseJoinColumns = @JoinColumn( name="taskItem_id", unique = false)
       )
       public List<TaskItem> getTaskItems() {
       return this.taskItems;
       }
       public void setTaskItems(List<TaskItem> taskItems) {
       this.taskItems = taskItems;
       }
      


      Thank u very much,

      Holger

        • 1. Re: @OneToMany Question

          This is really an EJB3/JPA question, not a Seam one. You may get better support if you post your question in that forum. It sounds like you want a ManyToMany relation. If Tasks can have many TaskItems and a TaskItem can be associated with more than one Task this is a ManyToMany relation, not a OneToMany.

          • 2. Re: @OneToMany Question

            Hello


            It sounds like you want a ManyToMany relation. If Tasks can have many TaskItems and a TaskItem can be associated with more than one Task this is a ManyToMany relation, not a OneToMany.


            Hmm but the TaskItem dont have any information about the task it owns
            (no getTask() method) and its not biderectional.I shoudl learn sth about database design.

            Thx for giving me the right hint i changed my code to:

             @ManyToMany(
             targetEntity=TaskItem.class,
             cascade={CascadeType.PERSIST, CascadeType.MERGE}
             )
             @JoinTable(
             name="task_taskItems",
             joinColumns = { @JoinColumn( name="task_id", unique = false)},
             inverseJoinColumns = @JoinColumn( name="taskItem_id", unique = false)
             )
            


            and now its working - no complaining anymore about duplicated entry.

            Thank u very much, helped me a lot and sorry for posting in wrong forum.

            Bye,

            Holger