Help - Hibernate is munching on my foreign keys

View: New views
11 Messages — Rating Filter:   Alert me  

Help - Hibernate is munching on my foreign keys

by Mark Dzmura :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, Trails'ers:

This is really a hibernate question, but the online resources I can
find seem to be targeted at XML configuration and not at Hibernate
annotations,
and all of my work bootstraps off of the Trails environment.  It's
probably an annotation problem.  However, I have had this problem for
a week while
I work on other things, and I cannot seem to make it go away.

There are one or two on-line references to similar problems.  One
claims to have been resolved by fully defining a 1-M relationship
as bidirectional instead of just unidirectional.  Another claims to
have been resolved by indicating that the foreign key reference is
nullable.
The first doesn't work for me because my relationship is already
bidirectional.  The second just doesn't work.

I have a parent class with four 1-M bidirectional relationships to
child classes.  All four are annotated the same way.  The first three
work fine, but the fourth one - recently added - exhibits bizarre behavior.

The broken relationship is mapped like this:

In the parent object:

     @OneToMany(cascade=CascadeType.ALL)
     @JoinColumn(name="episodeId")
     @Collection(child=true, inverse="episode")
     @OrderBy("measure")
     @PropertyDescriptor(index=34)
     @LazyCollection(LazyCollectionOption.FALSE)
     public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()

In the child object:

    @ManyToOne
    @JoinColumn(name="episodeId")
    @PropertyDescriptor(index=1, readOnly=true)
    public HbipsEpisode getEpisode()

In response to a parent object query using the persistence service,
Hibernate generates a TON of update statements of the form:

    update HbipsMeasureResultIndicator set episodeId=null where episodeId=?

HbipsMeasureResultIndicator is the child class, and episodeid is the
foreign key into the parent class.
The key values in the update statements are the same as the parent
episodes returned by the query.

The result is to sever the relationship between the children and their parents.

The intention of my code is to perform a read-only operation -
returning a collection of episodes.  This works
correctly, but with the destrucitve side effect of zeroing out the FK.

Does anybody have a clue what might cause this?

Thanks,
Mark

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Help - Hibernate is munching on my foreign keys

by Alejandro Scandroli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Mark

I've seen this issue over and over.
Here is a simple solution.

 In the parent object:

      @OneToMany(mappedBy = "episode", cascade=CascadeType.REMOVE)
      @Collection(child=true)
      @OrderBy("measure")
      @PropertyDescriptor(index=34)
      @LazyCollection(LazyCollectionOption.FALSE)
      public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()

In the child object:

     @ManyToOne
     @PropertyDescriptor(index=1, readOnly=true)
     public HbipsEpisode getEpisode()


This solution has its own "cons". In the parent object
measureResultIndicators will be read only, this means that
setMeasureResultIndicators(...) won't produce any SQL updates. Trails
has its own heuritic to work aorund this issue, but If you are using
elaborated bussiness methods (other than Trails) with these entities
you may need a more elaborated mapping solution.
Try it and let me know if it works.

Saludos.
--
Alejandro Scandroli
Amneris: We build process-driven web applications.
http://www.amneris.es


On Nov 27, 2007 12:31 PM, Mark Dzmura <mark.dzmura@...> wrote:

> Hi, Trails'ers:
>
> This is really a hibernate question, but the online resources I can
> find seem to be targeted at XML configuration and not at Hibernate
> annotations,
> and all of my work bootstraps off of the Trails environment.  It's
> probably an annotation problem.  However, I have had this problem for
> a week while
> I work on other things, and I cannot seem to make it go away.
>
> There are one or two on-line references to similar problems.  One
> claims to have been resolved by fully defining a 1-M relationship
> as bidirectional instead of just unidirectional.  Another claims to
> have been resolved by indicating that the foreign key reference is
> nullable.
> The first doesn't work for me because my relationship is already
> bidirectional.  The second just doesn't work.
>
> I have a parent class with four 1-M bidirectional relationships to
> child classes.  All four are annotated the same way.  The first three
> work fine, but the fourth one - recently added - exhibits bizarre behavior.
>
> The broken relationship is mapped like this:
>
> In the parent object:
>
>      @OneToMany(cascade=CascadeType.ALL)
>      @JoinColumn(name="episodeId")
>      @Collection(child=true, inverse="episode")
>      @OrderBy("measure")
>      @PropertyDescriptor(index=34)
>      @LazyCollection(LazyCollectionOption.FALSE)
>      public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
>
> In the child object:
>
>     @ManyToOne
>     @JoinColumn(name="episodeId")
>     @PropertyDescriptor(index=1, readOnly=true)
>     public HbipsEpisode getEpisode()
>
> In response to a parent object query using the persistence service,
> Hibernate generates a TON of update statements of the form:
>
>     update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
>
> HbipsMeasureResultIndicator is the child class, and episodeid is the
> foreign key into the parent class.
> The key values in the update statements are the same as the parent
> episodes returned by the query.
>
> The result is to sever the relationship between the children and their parents.
>
> The intention of my code is to perform a read-only operation -
> returning a collection of episodes.  This works
> correctly, but with the destrucitve side effect of zeroing out the FK.
>
> Does anybody have a clue what might cause this?
>
> Thanks,
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Lazy Initialization Exception adn reattach ?

by Toby78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I got a Lazy Initialization Exception, so I tried to use the reattach method of the HibernatePersistenceService.

...but I still get it.

How would you solve something like that?

public void addToList(IRequestCycle cycle) {
               
                Item item = getItem();
               
                HibernatePersistenceService service = (HibernatePersistenceService)this.getPersistenceService();
               
                User user = this.getCurrentUser();
                service.reattach(user);
               
                Set<Item> items= user.getItems();

                items.add(this.getItem());
               
                service.save(user);
           
                cycle.activate(this);
               
        }


Thanks!

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Lazy Initialization Exception adn reattach ?

by Alejandro Scandroli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Tobias

This is bug in Trails 1.1.1 (http://jira.codehaus.org/browse/TRAILS-116)
To workaround it you need to create your own transactional method and
then implement your logic inside that method.
You can extend from HibernatePersistenceService, or you can create
your own service class. As long as you mark your service method as
@Transactional and inject your service class into the page via Spring
you shouldn't have any problems making it work.

--
Alejandro Scandroli
Amneris: We build process-driven web applications.
http://www.amneris.es


On Nov 27, 2007 3:22 PM,  <superoverdrive@...> wrote:

> I got a Lazy Initialization Exception, so I tried to use the reattach method of the HibernatePersistenceService.
>
> ...but I still get it.
>
> How would you solve something like that?
>
> public void addToList(IRequestCycle cycle) {
>
>                 Item item = getItem();
>
>                 HibernatePersistenceService service = (HibernatePersistenceService)this.getPersistenceService();
>
>                 User user = this.getCurrentUser();
>                 service.reattach(user);
>
>                 Set<Item> items= user.getItems();
>
>                 items.add(this.getItem());
>
>                 service.save(user);
>
>                 cycle.activate(this);
>
>         }
>
>
> Thanks!
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Lazy Initialization Exception adn reattach ?

by Toby78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks!

I also found 2 other work-arounds:

1. using Eager instead of Lazy in the Annotations if it is possible

2. when using Eager the following code works also:

HibernatePersistenceService service =
(HibernatePersistenceService)this.getPersistenceService();

                 User user = this.getCurrentUser();
                 Set<Item> items= user.getItems();
                 items.add(this.getItem());

User dbUser = service.loadInstance(User.class,currentUser.getId());
dbUser.setItems(items);
service.save(dbUser );
cycle.activate(this);



       
-------- Original-Nachricht --------
> Datum: Tue, 27 Nov 2007 16:00:45 +0100
> Von: "Alejandro Scandroli" <alejandroscandroli@...>
> An: users@...
> Betreff: Re: [trails-users] Lazy Initialization Exception adn reattach ?

> Hi Tobias
>
> This is bug in Trails 1.1.1 (http://jira.codehaus.org/browse/TRAILS-116)
> To workaround it you need to create your own transactional method and
> then implement your logic inside that method.
> You can extend from HibernatePersistenceService, or you can create
> your own service class. As long as you mark your service method as
> @Transactional and inject your service class into the page via Spring
> you shouldn't have any problems making it work.
>
> --
> Alejandro Scandroli
> Amneris: We build process-driven web applications.
> http://www.amneris.es
>
>
> On Nov 27, 2007 3:22 PM,  <superoverdrive@...> wrote:
> > I got a Lazy Initialization Exception, so I tried to use the reattach
> method of the HibernatePersistenceService.
> >
> > ...but I still get it.
> >
> > How would you solve something like that?
> >
> > public void addToList(IRequestCycle cycle) {
> >
> >                 Item item = getItem();
> >
> >                 HibernatePersistenceService service =
> (HibernatePersistenceService)this.getPersistenceService();
> >
> >                 User user = this.getCurrentUser();
> >                 service.reattach(user);
> >
> >                 Set<Item> items= user.getItems();
> >
> >                 items.add(this.getItem());
> >
> >                 service.save(user);
> >
> >                 cycle.activate(this);
> >
> >         }
> >
> >
> > Thanks!
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


RE: Help - Hibernate is munching on my foreign keys

by nhhockeyplayer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Have you looked at the trails classic roster demo 1-M modelings?
 
They seem to work fine for me.
 
If your looking for pure readonly then you may want to alter your cascade. ALL implies read/write.
 
Just an initial 0.02 centavos

Best regards
Ken in nashua



> Date: Tue, 27 Nov 2007 06:31:25 -0500
> From: mark.dzmura@...
> To: users@...
> Subject: [trails-users] Help - Hibernate is munching on my foreign keys
>
> Hi, Trails'ers:
>
> This is really a hibernate question, but the online resources I can
> find seem to be targeted at XML configuration and not at Hibernate
> annotations,
> and all of my work bootstraps off of the Trails environment. It's
> probably an annotation problem. However, I have had this problem for
> a week while
> I work on other things, and I cannot seem to make it go away.
>
> There are one or two on-line references to similar problems. One
> claims to have been resolved by fully defining a 1-M relationship
> as bidirectional instead of just unidirectional. Another claims to
> have been resolved by indicating that the foreign key reference is
> nullable.
> The first doesn't work for me because my relationship is already
> bidirectional. The second just doesn't work.
>
> I have a parent class with four 1-M bidirectional relationships to
> child classes. All four are annotated the same way. The first three
> work fine, but the fourth one - recently added - exhibits bizarre behavior.
>
> The broken relationship is mapped like this:
>
> In the parent object:
>
> @OneToMany(cascade=CascadeType.ALL)
> @JoinColumn(name="episodeId")
> @Collection(child=true, inverse="episode")
> @OrderBy("measure")
> @PropertyDescriptor(index=34)
> @LazyCollection(LazyCollectionOption.FALSE)
> public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
>
> In the child object:
>
> @ManyToOne
> @JoinColumn(name="episodeId")
> @PropertyDescriptor(index=1, readOnly=true)
> public HbipsEpisode getEpisode()
>
> In response to a parent object query using the persistence service,
> Hibernate generates a TON of update statements of the form:
>
> update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
>
> HbipsMeasureResultIndicator is the child class, and episodeid is the
> foreign key into the parent class.
> The key values in the update statements are the same as the parent
> episodes returned by the query.
>
> The result is to sever the relationship between the children and their parents.
>
> The intention of my code is to perform a read-only operation -
> returning a collection of episodes. This works
> correctly, but with the destrucitve side effect of zeroing out the FK.
>
> Does anybody have a clue what might cause this?
>
> Thanks,
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
> http://xircles.codehaus.org/manage_email
>



You keep typing, we keep giving. Download Messenger and join the i’m Initiative now. Join in!
Best regards
Ken in nashua

Re: Help - Hibernate is munching on my foreign keys

by Mark Dzmura :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alejandro:

Hmmm... I made the changes you suggested, and the problem of
spontaneous deletion of foreign keys has gone away.
However, I still have the peculiar behavior that queries against the
parent object do not populate the children of THIS 1-M
relationship.  All of the others are populated.

Looking at the SQL generated by hibernate, it seems to be generating
select's to retrieve the child objects -
if I copy the SQL into a database session and run it with a PK to the
parent object (episode) for the "?",
it brings back child objects...

If I go to the trails screen for the child object, it shows me the
parent relationships.

So I'm at a loss..

-Mark

On Nov 27, 2007 8:58 AM, Alejandro Scandroli
<alejandroscandroli@...> wrote:

> Hi Mark
>
> I've seen this issue over and over.
> Here is a simple solution.
>
>  In the parent object:
>
>       @OneToMany(mappedBy = "episode", cascade=CascadeType.REMOVE)
>       @Collection(child=true)
>       @OrderBy("measure")
>       @PropertyDescriptor(index=34)
>       @LazyCollection(LazyCollectionOption.FALSE)
>       public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
>
> In the child object:
>
>      @ManyToOne
>      @PropertyDescriptor(index=1, readOnly=true)
>      public HbipsEpisode getEpisode()
>
>
> This solution has its own "cons". In the parent object
> measureResultIndicators will be read only, this means that
> setMeasureResultIndicators(...) won't produce any SQL updates. Trails
> has its own heuritic to work aorund this issue, but If you are using
> elaborated bussiness methods (other than Trails) with these entities
> you may need a more elaborated mapping solution.
> Try it and let me know if it works.
>
> Saludos.
> --
> Alejandro Scandroli
> Amneris: We build process-driven web applications.
> http://www.amneris.es
>
>
>
> On Nov 27, 2007 12:31 PM, Mark Dzmura <mark.dzmura@...> wrote:
> > Hi, Trails'ers:
> >
> > This is really a hibernate question, but the online resources I can
> > find seem to be targeted at XML configuration and not at Hibernate
> > annotations,
> > and all of my work bootstraps off of the Trails environment.  It's
> > probably an annotation problem.  However, I have had this problem for
> > a week while
> > I work on other things, and I cannot seem to make it go away.
> >
> > There are one or two on-line references to similar problems.  One
> > claims to have been resolved by fully defining a 1-M relationship
> > as bidirectional instead of just unidirectional.  Another claims to
> > have been resolved by indicating that the foreign key reference is
> > nullable.
> > The first doesn't work for me because my relationship is already
> > bidirectional.  The second just doesn't work.
> >
> > I have a parent class with four 1-M bidirectional relationships to
> > child classes.  All four are annotated the same way.  The first three
> > work fine, but the fourth one - recently added - exhibits bizarre behavior.
> >
> > The broken relationship is mapped like this:
> >
> > In the parent object:
> >
> >      @OneToMany(cascade=CascadeType.ALL)
> >      @JoinColumn(name="episodeId")
> >      @Collection(child=true, inverse="episode")
> >      @OrderBy("measure")
> >      @PropertyDescriptor(index=34)
> >      @LazyCollection(LazyCollectionOption.FALSE)
> >      public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> >
> > In the child object:
> >
> >     @ManyToOne
> >     @JoinColumn(name="episodeId")
> >     @PropertyDescriptor(index=1, readOnly=true)
> >     public HbipsEpisode getEpisode()
> >
> > In response to a parent object query using the persistence service,
> > Hibernate generates a TON of update statements of the form:
> >
> >     update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
> >
> > HbipsMeasureResultIndicator is the child class, and episodeid is the
> > foreign key into the parent class.
> > The key values in the update statements are the same as the parent
> > episodes returned by the query.
> >
> > The result is to sever the relationship between the children and their parents.
> >
> > The intention of my code is to perform a read-only operation -
> > returning a collection of episodes.  This works
> > correctly, but with the destrucitve side effect of zeroing out the FK.
> >
> > Does anybody have a clue what might cause this?
> >
> > Thanks,
> > Mark
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Help - Hibernate is munching on my foreign keys

by Mark Dzmura :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, Ken... I have been meaning to go through the examples, but
with deadline pressure I haven't had a chance yet.
I don't want to risk making major changes to a lot of code which is
mostly working at this point...even if it could benefit
from some better modeling.

Thanks,
Mark

On Nov 27, 2007 11:41 AM, Ken in nashua <nhhockeyplayer@...> wrote:

>
>  Have you looked at the trails classic roster demo 1-M modelings?
>
>  They seem to work fine for me.
>
>  If your looking for pure readonly then you may want to alter your cascade.
> ALL implies read/write.
>
>  Just an initial 0.02 centavos
>
> Best regards
> Ken in nashua
>
>
>
> > Date: Tue, 27 Nov 2007 06:31:25 -0500
> > From: mark.dzmura@...
> > To: users@...
> > Subject: [trails-users] Help - Hibernate is munching on my foreign keys
>
>
> >
> > Hi, Trails'ers:
> >
> > This is really a hibernate question, but the online resources I can
> > find seem to be targeted at XML configuration and not at Hibernate
> > annotations,
> > and all of my work bootstraps off of the Trails environment. It's
> > probably an annotation problem. However, I have had this problem for
> > a week while
> > I work on other things, and I cannot seem to make it go away.
> >
> > There are one or two on-line references to similar problems. One
> > claims to have been resolved by fully defining a 1-M relationship
> > as bidirectional instead of just unidirectional. Another claims to
> > have been resolved by indicating that the foreign key reference is
> > nullable.
> > The first doesn't work for me because my relationship is already
> > bidirectional. The second just doesn't work.
> >
> > I have a parent class with four 1-M bidirectional relationships to
> > child classes. All four are annotated the same way. The first three
> > work fine, but the fourth one - recently added - exhibits bizarre
> behavior.
> >
> > The broken relationship is mapped like this:
> >
> > In the parent object:
> >
> > @OneToMany(cascade=CascadeType.ALL)
> > @JoinColumn(name="episodeId")
> > @Collection(child=true, inverse="episode")
> > @OrderBy("measure")
> > @PropertyDescriptor(index=34)
> > @LazyCollection(LazyCollectionOption.FALSE)
> > public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> >
> > In the child object:
> >
> > @ManyToOne
> > @JoinColumn(name="episodeId")
> > @PropertyDescriptor(index=1, readOnly=true)
> > public HbipsEpisode getEpisode()
> >
> > In response to a parent object query using the persistence service,
> > Hibernate generates a TON of update statements of the form:
> >
> > update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
> >
> > HbipsMeasureResultIndicator is the child class, and episodeid is the
> > foreign key into the parent class.
> > The key values in the update statements are the same as the parent
> > episodes returned by the query.
> >
> > The result is to sever the relationship between the children and their
> parents.
> >
> > The intention of my code is to perform a read-only operation -
> > returning a collection of episodes. This works
> > correctly, but with the destrucitve side effect of zeroing out the FK.
> >
> > Does anybody have a clue what might cause this?
> >
> > Thanks,
> > Mark
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> > http://xircles.codehaus.org/manage_email
> >
>
>
> ________________________________
> You keep typing, we keep giving. Download Messenger and join the i'm
> Initiative now. Join in!

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Help - Hibernate is munching on my foreign keys

by Alejandro Scandroli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Mark

What do you mean by:
> queries against the parent object do not populate the children of THIS 1-M relationship.

What type of queries? Can you show me an example?
What happens if you add back
@LazyCollection(LazyCollectionOption.FALSE) to the code I sent you?
Tell me as much about your context as you can.

Cheers.
--
Alejandro Scandroli
Amneris: We build process-driven web applications.
http://www.amneris.es


On Nov 27, 2007 9:00 PM, Mark Dzmura <mark.dzmura@...> wrote:

> Alejandro:
>
> Hmmm... I made the changes you suggested, and the problem of
> spontaneous deletion of foreign keys has gone away.
> However, I still have the peculiar behavior that queries against the
> parent object do not populate the children of THIS 1-M
> relationship.  All of the others are populated.
>
> Looking at the SQL generated by hibernate, it seems to be generating
> select's to retrieve the child objects -
> if I copy the SQL into a database session and run it with a PK to the
> parent object (episode) for the "?",
> it brings back child objects...
>
> If I go to the trails screen for the child object, it shows me the
> parent relationships.
>
> So I'm at a loss..
>
> -Mark
>
> On Nov 27, 2007 8:58 AM, Alejandro Scandroli
>
> <alejandroscandroli@...> wrote:
> > Hi Mark
> >
> > I've seen this issue over and over.
> > Here is a simple solution.
> >
> >  In the parent object:
> >
> >       @OneToMany(mappedBy = "episode", cascade=CascadeType.REMOVE)
> >       @Collection(child=true)
> >       @OrderBy("measure")
> >       @PropertyDescriptor(index=34)
> >       @LazyCollection(LazyCollectionOption.FALSE)
> >       public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> >
> > In the child object:
> >
> >      @ManyToOne
> >      @PropertyDescriptor(index=1, readOnly=true)
> >      public HbipsEpisode getEpisode()
> >
> >
> > This solution has its own "cons". In the parent object
> > measureResultIndicators will be read only, this means that
> > setMeasureResultIndicators(...) won't produce any SQL updates. Trails
> > has its own heuritic to work aorund this issue, but If you are using
> > elaborated bussiness methods (other than Trails) with these entities
> > you may need a more elaborated mapping solution.
> > Try it and let me know if it works.
> >
> > Saludos.
> > --
> > Alejandro Scandroli
> > Amneris: We build process-driven web applications.
> > http://www.amneris.es
> >
> >
> >
> > On Nov 27, 2007 12:31 PM, Mark Dzmura <mark.dzmura@...> wrote:
> > > Hi, Trails'ers:
> > >
> > > This is really a hibernate question, but the online resources I can
> > > find seem to be targeted at XML configuration and not at Hibernate
> > > annotations,
> > > and all of my work bootstraps off of the Trails environment.  It's
> > > probably an annotation problem.  However, I have had this problem for
> > > a week while
> > > I work on other things, and I cannot seem to make it go away.
> > >
> > > There are one or two on-line references to similar problems.  One
> > > claims to have been resolved by fully defining a 1-M relationship
> > > as bidirectional instead of just unidirectional.  Another claims to
> > > have been resolved by indicating that the foreign key reference is
> > > nullable.
> > > The first doesn't work for me because my relationship is already
> > > bidirectional.  The second just doesn't work.
> > >
> > > I have a parent class with four 1-M bidirectional relationships to
> > > child classes.  All four are annotated the same way.  The first three
> > > work fine, but the fourth one - recently added - exhibits bizarre behavior.
> > >
> > > The broken relationship is mapped like this:
> > >
> > > In the parent object:
> > >
> > >      @OneToMany(cascade=CascadeType.ALL)
> > >      @JoinColumn(name="episodeId")
> > >      @Collection(child=true, inverse="episode")
> > >      @OrderBy("measure")
> > >      @PropertyDescriptor(index=34)
> > >      @LazyCollection(LazyCollectionOption.FALSE)
> > >      public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> > >
> > > In the child object:
> > >
> > >     @ManyToOne
> > >     @JoinColumn(name="episodeId")
> > >     @PropertyDescriptor(index=1, readOnly=true)
> > >     public HbipsEpisode getEpisode()
> > >
> > > In response to a parent object query using the persistence service,
> > > Hibernate generates a TON of update statements of the form:
> > >
> > >     update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
> > >
> > > HbipsMeasureResultIndicator is the child class, and episodeid is the
> > > foreign key into the parent class.
> > > The key values in the update statements are the same as the parent
> > > episodes returned by the query.
> > >
> > > The result is to sever the relationship between the children and their parents.
> > >
> > > The intention of my code is to perform a read-only operation -
> > > returning a collection of episodes.  This works
> > > correctly, but with the destrucitve side effect of zeroing out the FK.
> > >
> > > Does anybody have a clue what might cause this?
> > >
> > > Thanks,
> > > Mark
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe from this list please visit:
> > >
> > >     http://xircles.codehaus.org/manage_email
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Help - Hibernate is munching on my foreign keys

by Mark Dzmura :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Alejandro:

You are the MAN.  The original problem was real, and your solution fixed it.

When things still didn't work right, I was quick to continue to blame
hibernate.  However,
when I looked at my code with a magnifying glass, I found the following:

Somehow the formal parameter of my setter method for the child class
did not have an "s" on the end:

    public void
setMeasureResultIndicators(List<HbipsMeasureResultIndicator>
measureResultIndicator*s*)
          {
          this.measureResultIndicators = measureResultIndicators;
          }

So I assume that Hibernate was trying to use the setter, and its
results were "dropped on the floor" when the
assignment statement basically set "x = x", ignoring the formal parameter.

Or does Hibernate use BCEL or something to set the property directly?

Anyway, problem resolved, and I am greatful for your effort and your knowledge.

Regards,
Mark






On Nov 28, 2007 4:59 AM, Alejandro Scandroli
<alejandroscandroli@...> wrote:

> Hi Mark
>
> What do you mean by:
> > queries against the parent object do not populate the children of THIS 1-M relationship.
>
> What type of queries? Can you show me an example?
> What happens if you add back
> @LazyCollection(LazyCollectionOption.FALSE) to the code I sent you?
> Tell me as much about your context as you can.
>
> Cheers.
> --
> Alejandro Scandroli
> Amneris: We build process-driven web applications.
> http://www.amneris.es
>
>
>
> On Nov 27, 2007 9:00 PM, Mark Dzmura <mark.dzmura@...> wrote:
> > Alejandro:
> >
> > Hmmm... I made the changes you suggested, and the problem of
> > spontaneous deletion of foreign keys has gone away.
> > However, I still have the peculiar behavior that queries against the
> > parent object do not populate the children of THIS 1-M
> > relationship.  All of the others are populated.
> >
> > Looking at the SQL generated by hibernate, it seems to be generating
> > select's to retrieve the child objects -
> > if I copy the SQL into a database session and run it with a PK to the
> > parent object (episode) for the "?",
> > it brings back child objects...
> >
> > If I go to the trails screen for the child object, it shows me the
> > parent relationships.
> >
> > So I'm at a loss..
> >
> > -Mark
> >
> > On Nov 27, 2007 8:58 AM, Alejandro Scandroli
> >
> > <alejandroscandroli@...> wrote:
> > > Hi Mark
> > >
> > > I've seen this issue over and over.
> > > Here is a simple solution.
> > >
> > >  In the parent object:
> > >
> > >       @OneToMany(mappedBy = "episode", cascade=CascadeType.REMOVE)
> > >       @Collection(child=true)
> > >       @OrderBy("measure")
> > >       @PropertyDescriptor(index=34)
> > >       @LazyCollection(LazyCollectionOption.FALSE)
> > >       public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> > >
> > > In the child object:
> > >
> > >      @ManyToOne
> > >      @PropertyDescriptor(index=1, readOnly=true)
> > >      public HbipsEpisode getEpisode()
> > >
> > >
> > > This solution has its own "cons". In the parent object
> > > measureResultIndicators will be read only, this means that
> > > setMeasureResultIndicators(...) won't produce any SQL updates. Trails
> > > has its own heuritic to work aorund this issue, but If you are using
> > > elaborated bussiness methods (other than Trails) with these entities
> > > you may need a more elaborated mapping solution.
> > > Try it and let me know if it works.
> > >
> > > Saludos.
> > > --
> > > Alejandro Scandroli
> > > Amneris: We build process-driven web applications.
> > > http://www.amneris.es
> > >
> > >
> > >
> > > On Nov 27, 2007 12:31 PM, Mark Dzmura <mark.dzmura@...> wrote:
> > > > Hi, Trails'ers:
> > > >
> > > > This is really a hibernate question, but the online resources I can
> > > > find seem to be targeted at XML configuration and not at Hibernate
> > > > annotations,
> > > > and all of my work bootstraps off of the Trails environment.  It's
> > > > probably an annotation problem.  However, I have had this problem for
> > > > a week while
> > > > I work on other things, and I cannot seem to make it go away.
> > > >
> > > > There are one or two on-line references to similar problems.  One
> > > > claims to have been resolved by fully defining a 1-M relationship
> > > > as bidirectional instead of just unidirectional.  Another claims to
> > > > have been resolved by indicating that the foreign key reference is
> > > > nullable.
> > > > The first doesn't work for me because my relationship is already
> > > > bidirectional.  The second just doesn't work.
> > > >
> > > > I have a parent class with four 1-M bidirectional relationships to
> > > > child classes.  All four are annotated the same way.  The first three
> > > > work fine, but the fourth one - recently added - exhibits bizarre behavior.
> > > >
> > > > The broken relationship is mapped like this:
> > > >
> > > > In the parent object:
> > > >
> > > >      @OneToMany(cascade=CascadeType.ALL)
> > > >      @JoinColumn(name="episodeId")
> > > >      @Collection(child=true, inverse="episode")
> > > >      @OrderBy("measure")
> > > >      @PropertyDescriptor(index=34)
> > > >      @LazyCollection(LazyCollectionOption.FALSE)
> > > >      public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> > > >
> > > > In the child object:
> > > >
> > > >     @ManyToOne
> > > >     @JoinColumn(name="episodeId")
> > > >     @PropertyDescriptor(index=1, readOnly=true)
> > > >     public HbipsEpisode getEpisode()
> > > >
> > > > In response to a parent object query using the persistence service,
> > > > Hibernate generates a TON of update statements of the form:
> > > >
> > > >     update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
> > > >
> > > > HbipsMeasureResultIndicator is the child class, and episodeid is the
> > > > foreign key into the parent class.
> > > > The key values in the update statements are the same as the parent
> > > > episodes returned by the query.
> > > >
> > > > The result is to sever the relationship between the children and their parents.
> > > >
> > > > The intention of my code is to perform a read-only operation -
> > > > returning a collection of episodes.  This works
> > > > correctly, but with the destrucitve side effect of zeroing out the FK.
> > > >
> > > > Does anybody have a clue what might cause this?
> > > >
> > > > Thanks,
> > > > Mark
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe from this list please visit:
> > > >
> > > >     http://xircles.codehaus.org/manage_email
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe from this list please visit:
> > >
> > >     http://xircles.codehaus.org/manage_email
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Help - Hibernate is munching on my foreign keys

by Alejandro Scandroli :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Mark

I'm glad it worked :)

> Or does Hibernate use BCEL or something to set the property directly?

If you are using field access (field annotations) Hibernate will
access your field directly, if you are using property access
(accessors annotations) Hibernate will use your getters and setters to
access your fields.

It's good to know the diference between them as well as the pros and
cons for both.
I've noticed that Trails doens't have an example of field access, I
will add one next week.

Saludos.

--
Alejandro Scandroli
Amneris: We build process-driven web applications.
http://www.amneris.es


On Nov 28, 2007 2:59 PM, Mark Dzmura <mark.dzmura@...> wrote:

> Alejandro:
>
> You are the MAN.  The original problem was real, and your solution fixed it.
>
> When things still didn't work right, I was quick to continue to blame
> hibernate.  However,
> when I looked at my code with a magnifying glass, I found the following:
>
> Somehow the formal parameter of my setter method for the child class
> did not have an "s" on the end:
>
>     public void
> setMeasureResultIndicators(List<HbipsMeasureResultIndicator>
> measureResultIndicator*s*)
>           {
>           this.measureResultIndicators = measureResultIndicators;
>           }
>
> So I assume that Hibernate was trying to use the setter, and its
> results were "dropped on the floor" when the
> assignment statement basically set "x = x", ignoring the formal parameter.
>
> Or does Hibernate use BCEL or something to set the property directly?
>
> Anyway, problem resolved, and I am greatful for your effort and your knowledge.
>
> Regards,
> Mark
>
>
>
>
>
>
> On Nov 28, 2007 4:59 AM, Alejandro Scandroli
>
> <alejandroscandroli@...> wrote:
> > Hi Mark
> >
> > What do you mean by:
> > > queries against the parent object do not populate the children of THIS 1-M relationship.
> >
> > What type of queries? Can you show me an example?
> > What happens if you add back
> > @LazyCollection(LazyCollectionOption.FALSE) to the code I sent you?
> > Tell me as much about your context as you can.
> >
> > Cheers.
> > --
> > Alejandro Scandroli
> > Amneris: We build process-driven web applications.
> > http://www.amneris.es
> >
> >
> >
> > On Nov 27, 2007 9:00 PM, Mark Dzmura <mark.dzmura@...> wrote:
> > > Alejandro:
> > >
> > > Hmmm... I made the changes you suggested, and the problem of
> > > spontaneous deletion of foreign keys has gone away.
> > > However, I still have the peculiar behavior that queries against the
> > > parent object do not populate the children of THIS 1-M
> > > relationship.  All of the others are populated.
> > >
> > > Looking at the SQL generated by hibernate, it seems to be generating
> > > select's to retrieve the child objects -
> > > if I copy the SQL into a database session and run it with a PK to the
> > > parent object (episode) for the "?",
> > > it brings back child objects...
> > >
> > > If I go to the trails screen for the child object, it shows me the
> > > parent relationships.
> > >
> > > So I'm at a loss..
> > >
> > > -Mark
> > >
> > > On Nov 27, 2007 8:58 AM, Alejandro Scandroli
> > >
> > > <alejandroscandroli@...> wrote:
> > > > Hi Mark
> > > >
> > > > I've seen this issue over and over.
> > > > Here is a simple solution.
> > > >
> > > >  In the parent object:
> > > >
> > > >       @OneToMany(mappedBy = "episode", cascade=CascadeType.REMOVE)
> > > >       @Collection(child=true)
> > > >       @OrderBy("measure")
> > > >       @PropertyDescriptor(index=34)
> > > >       @LazyCollection(LazyCollectionOption.FALSE)
> > > >       public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> > > >
> > > > In the child object:
> > > >
> > > >      @ManyToOne
> > > >      @PropertyDescriptor(index=1, readOnly=true)
> > > >      public HbipsEpisode getEpisode()
> > > >
> > > >
> > > > This solution has its own "cons". In the parent object
> > > > measureResultIndicators will be read only, this means that
> > > > setMeasureResultIndicators(...) won't produce any SQL updates. Trails
> > > > has its own heuritic to work aorund this issue, but If you are using
> > > > elaborated bussiness methods (other than Trails) with these entities
> > > > you may need a more elaborated mapping solution.
> > > > Try it and let me know if it works.
> > > >
> > > > Saludos.
> > > > --
> > > > Alejandro Scandroli
> > > > Amneris: We build process-driven web applications.
> > > > http://www.amneris.es
> > > >
> > > >
> > > >
> > > > On Nov 27, 2007 12:31 PM, Mark Dzmura <mark.dzmura@...> wrote:
> > > > > Hi, Trails'ers:
> > > > >
> > > > > This is really a hibernate question, but the online resources I can
> > > > > find seem to be targeted at XML configuration and not at Hibernate
> > > > > annotations,
> > > > > and all of my work bootstraps off of the Trails environment.  It's
> > > > > probably an annotation problem.  However, I have had this problem for
> > > > > a week while
> > > > > I work on other things, and I cannot seem to make it go away.
> > > > >
> > > > > There are one or two on-line references to similar problems.  One
> > > > > claims to have been resolved by fully defining a 1-M relationship
> > > > > as bidirectional instead of just unidirectional.  Another claims to
> > > > > have been resolved by indicating that the foreign key reference is
> > > > > nullable.
> > > > > The first doesn't work for me because my relationship is already
> > > > > bidirectional.  The second just doesn't work.
> > > > >
> > > > > I have a parent class with four 1-M bidirectional relationships to
> > > > > child classes.  All four are annotated the same way.  The first three
> > > > > work fine, but the fourth one - recently added - exhibits bizarre behavior.
> > > > >
> > > > > The broken relationship is mapped like this:
> > > > >
> > > > > In the parent object:
> > > > >
> > > > >      @OneToMany(cascade=CascadeType.ALL)
> > > > >      @JoinColumn(name="episodeId")
> > > > >      @Collection(child=true, inverse="episode")
> > > > >      @OrderBy("measure")
> > > > >      @PropertyDescriptor(index=34)
> > > > >      @LazyCollection(LazyCollectionOption.FALSE)
> > > > >      public List<HbipsMeasureResultIndicator> getMeasureResultIndicators()
> > > > >
> > > > > In the child object:
> > > > >
> > > > >     @ManyToOne
> > > > >     @JoinColumn(name="episodeId")
> > > > >     @PropertyDescriptor(index=1, readOnly=true)
> > > > >     public HbipsEpisode getEpisode()
> > > > >
> > > > > In response to a parent object query using the persistence service,
> > > > > Hibernate generates a TON of update statements of the form:
> > > > >
> > > > >     update HbipsMeasureResultIndicator set episodeId=null where episodeId=?
> > > > >
> > > > > HbipsMeasureResultIndicator is the child class, and episodeid is the
> > > > > foreign key into the parent class.
> > > > > The key values in the update statements are the same as the parent
> > > > > episodes returned by the query.
> > > > >
> > > > > The result is to sever the relationship between the children and their parents.
> > > > >
> > > > > The intention of my code is to perform a read-only operation -
> > > > > returning a collection of episodes.  This works
> > > > > correctly, but with the destrucitve side effect of zeroing out the FK.
> > > > >
> > > > > Does anybody have a clue what might cause this?
> > > > >
> > > > > Thanks,
> > > > > Mark
> > > > >
> > > > > ---------------------------------------------------------------------
> > > > > To unsubscribe from this list please visit:
> > > > >
> > > > >     http://xircles.codehaus.org/manage_email
> > > > >
> > > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe from this list please visit:
> > > >
> > > >     http://xircles.codehaus.org/manage_email
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe from this list please visit:
> > >
> > >     http://xircles.codehaus.org/manage_email
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >     http://xircles.codehaus.org/manage_email
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email