-->
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.  [ 3 posts ] 
Author Message
 Post subject: @ManyToOne with @IndexColumn. Deleting from the List
PostPosted: Tue Jun 06, 2006 4:26 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
I have a Menu entity, which has a ManyToOne relationship with itself.

That is a Menu has sub menus.

So I have

Code:
   @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   @IndexColumn(name="submenuIndex")
   public List<Menu> getSubMenus()
   {
      if (subMenus == null)
         subMenus = new ArrayList<Menu>();
      return subMenus;
   }
   
   public void setSubMenus(List<Menu> subMenus)
   {
      this.subMenus = subMenus;
   }


All great. The list is kept in order by the extra column "submenuIndex" in the link table, and I can get all submenus for a Menu.

But obviously there is another side of this relationship. I should be able to get the parent Menu of a Menu, using this same link table.

But when I add

Code:
   @ManyToOne
   public Menu getParent()
   {
      return parent;
   }
   public void setParent(Menu parentMenu)
   {
      this.parent = parent;
   }


It adds a new column, "parent_uuid" (According to our NamingStrategy), but does not keep this updated. I dno't want to set the parent, I update the List of submenus, and I want the parent to be inferred from that.

With there benig a OneToMany which already uses a link table, a ManyToOne to the same entity should just use that link table the other way round.

Is it possible to ask Hibernate to do this automatically?

Also, is it possible to have a Menu deleted when it is "orphaned"? That is it has been removed from all submenu lists - no Menu now owns it.


Last edited by Animal on Wed Jun 07, 2006 6:44 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 6:43 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
Also, it's creating a duplicate key error if I "move" the entries in the submenu List.

The Menu maintenance JSP page sends back a complex update because it's a complex, n-tier tree of menus->submenus->submenus...

So, if in one Menu, an entry in it's submenu List moves to a new position in the list, the List is obviously build from scratch because the updating servlet can't know about what's moved where.

So it goes through the List, and puts in a freshly read from the database Menu at each point in the List (It might be the same Menu that was there before, but whatever, we just populate the List to match what was sent back)

When the update comes, submenu "X" has moved up the List, it tries to create the entry in the link table, from the main Menu to submenu "X", and it already exists - just with a higher index.

It should not do that. It just doesn't work. I should be able to put new entries into the List, change entries, delete entries, and it should keep the database up to date with that!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 3:57 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
So how do I get it to update correctly?

If I remove an item from the top of a list (and shuffle the other entries up - I don't want gaps), then it gets a duplicate key error.

Code:
07:44:33,656 INFO  [STDOUT] Hibernate: update Menu_subMenus set subMenus_uuid=? where Menu_uuid=? and submenuIndex=?
07:44:33,671 WARN  [JDBCExceptionReporter] SQL Error: 1062, SQLState: 23000
07:44:33,671 ERROR [JDBCExceptionReporter] Duplicate key or integrity constraint violation message from server: "Duplicate entry '149' for key 1"
07:44:33,671 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session


I'm updating using

Code:
<node className="com.fcl.greenfield.entity.Menu" description="App. Data" uuid="74">
  <node className="com.fcl.greenfield.entity.Menu" description="AppComponent" uuid="149"/>
  <node className="com.fcl.greenfield.entity.Menu" description="AppTaxNoValidation" uuid="150"/>
</node>


Now, there was an entry at the top of the list, with a uuid of 148 which I removed. I moved the two subsequent entries up by one position. Then I asked the EntityManager to persist the top level Menu object.

Now, 149 is at position 0 in the list, and 150 is at position 1.

It should just delete the link table entry from parent Menu 74 to subMenu 148, and then change the index column of subsequent entries. It's not, it's changing entry 0 in the link table to point to subMenu 149, and that causes the error, because entry 1 already points to it.

What it needs to do is go down the 74->* entries in the link table, just changing every linked-to uuid to match what's in the List. Then delete any left over at the end. That's it. Do I have to do it manually?

How do I get it to update the list correctly? Or do I have to set deleted entries in ordered Lists 0 in the List to null, and put up with gaps in retrieved Lists? If people update a lot there could be enormous gaps! That solution is not good enough!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.