-->
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.  [ 13 posts ] 
Author Message
 Post subject: Ambiguous match found
PostPosted: Tue Jun 27, 2006 9:16 am 
Beginner
Beginner

Joined: Thu Jun 01, 2006 11:27 am
Posts: 28
I'm using NHibernate 1.2.0 Alpha and i have defined all the public properties like virtual. The problem is when i try to use reflection on my entities, i get "Ambiguous match found" when i use for example: "ob.GetType().GetProperty(name)" . I saw in the debugger all properties and i found how they are duplicates "ob.GetType().GetProperties()".
How can i solve this??

Many Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 10:17 am 
Beginner
Beginner

Joined: Sat Dec 10, 2005 6:22 pm
Posts: 28
Location: Chicago, IL
It would help if you posted the code for your class.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:09 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Actually, I've run into this when trying to get properties from proxies. The best solution is to get the underlying type that the proxy represents using NHibernateUtil.GetClass(proxy) method to get the real type. After that using the code that you're using will probably work.

Symon.


Top
 Profile  
 
 Post subject: Re: Ambiguous match found
PostPosted: Mon Aug 14, 2006 11:13 am 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
Bit late response, but only just saw this topic. Did you try:

GetProperty(name,
BindingFlags.DeclaredOnly|BindingFlags.Public|BindingFlags.Instance);


Top
 Profile  
 
 Post subject: Dupe property defs on proxies cause AmbiguousMatchException
PostPosted: Thu Dec 14, 2006 11:57 pm 
Newbie

Joined: Thu Dec 14, 2006 11:14 pm
Posts: 6
I have found that nHibernate 1.2 Beta 2 runtime generated proxies still result in duplicate property definitions in the System.Type.GetProperties() collection. This is causing problems for me specifically when trying to databind to nHibernate data objects using Spring (1.1 Preview 3). Spring uses:

proxy.GetProperty("MyProp", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.IgnoreCase)

The suggestion in this thread to add BindingFlags.DeclaredOnly would address this ambiguous match but would introduce other problems since non peristent properties on the data object are not created on the proxy and therefore would not be found...

Fundamentally, I don't understand why duplicate sets of properties are on the GetProperties() collection of an nHibernate proxy. When I mock up a very simple concept for a proxy class, I only see 1 property on (new TestAmbiguousProxy()).GetType().GetProperties() and consequently, the AmbiguousMatchException doesn't occur:

class TestAmbigous
{
protected string simpleValue;

public virtual string SimpleValue
{
get { return simpleValue; }
set { simpleValue = value; }
}
}

class TestAmbigousProxy : TestAmbigous
{
public override string SimpleValue
{
get { return base.SimpleValue; }
set { base.SimpleValue = value; }
}
}

What is different about nHibernate's proxies that causes the duplicate PropertyInformation and do they really need to be that way since this is a major obstacle in my use of Spring (and potentially other reflection-based code/frameworks).

Thanks.

Pete


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 2:34 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I don't think DynamicProxy does anything specifically to hinder your use of Spring. I just think that what you are seeing is caused by property overriding. If you try TestAmbiguousProxy().GetType().GetProperty("SimpleValue", SameFlagsAsInSpring), you should get the same error.

Anyway, this is either a problem with DynamicProxy or with Spring.NET, not with NHibernate so this forum is not the most appropriate place...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 11:41 am 
Newbie

Joined: Thu Dec 14, 2006 11:14 pm
Posts: 6
I had tried exactly the line of code you suggested (along with enumerating all properties as mentioned in my previous post), and the issue doesn't appear with my TestAmbiguousProxy instance.

Your point is well taken about Castle.DynamicProxy, so I constructed a test case with just that library and was able to reproduce this "problem" of duplicate PropertyInfos on a proxy. I'll post my followup questions in the Castle forums.

For those curious, I also dug deeper into the Spring code base and it looks like they actually explicitly handle the scenario of an AmbiguousMatchException and it looks like my real failure is happening downstream from there.

Thanks for your quick reply.


Top
 Profile  
 
 Post subject: AmbiguousMatchException example code
PostPosted: Thu Jun 21, 2007 9:02 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I have encountered the same problem that swtechno reported on version 1.2.0.4000.

The problem might be related to:
http://groups.google.com/group/castle-project-devel/browse_thread/thread/5cf99e02f29e90d5?hl=en
or
http://groups.google.com/group/castle-project-devel/browse_thread/thread/69e302466658c873?hl=en

I have put together a code example demonstrating the problem at: http://www.bigupload.com/d=F0769524

(to see the problem in action, open the command prompt in the unzipped folder, and type 'nant')

Did anyone ever find a solution to this?

Thanks,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 12:50 pm 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
We ran into the same problem. Here's a workaround:

Code:
private PropertyInfo GetPropertyInfo(DomainObjectBase obj)
{
  Type t = obj.GetType();
  // Need to perform reflection in isolation on the object and its supertype due to a problem with Castle Dynamic
  // Proxy and lazy loading
  foreach (PropertyInfo info in t.BaseType.GetProperties())
  {
    if (info.Name == PropertyName)
    {
      return info;
    }
  }
  return t.GetProperty(PropertyName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
}


Top
 Profile  
 
 Post subject: AmbiguousMatchException example code
PostPosted: Fri Jun 22, 2007 4:59 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
That's great jagid. I've tried that, and it works a treat.

I've uploaded an example using the workaround here: http://www.bigupload.com/d=6FBF0850

Can someone confirm that this is a bug in NHibernate (or Dynamic Proxy)?

Thanks,
Richard


Top
 Profile  
 
 Post subject: AmbiguousMatchException
PostPosted: Sun Jun 24, 2007 1:07 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I've managed to narrow this down to DynamicProxy, and have posted a question on their forum to see if they can help: http://forum.castleproject.org/viewtopic.php?p=8290

Thanks,
Richard


Top
 Profile  
 
 Post subject: AmbiguousMatchException
PostPosted: Mon Jul 30, 2007 9:48 am 
Newbie

Joined: Mon Jul 30, 2007 8:58 am
Posts: 1
I've also got 'AmbiguousMatchException' problem when I try to use lazy loading with NHibernate 1.2 and access "nested" object property with WPF. As I understand, it happens due to Dynamic Proxy bug with reflection. I found several posts on this problem in different forums, but no solution. The work around suggested by jagid is great, but I cannot find a way to implement it since WPF use reflection but 'hide' it. In other words, with WPF binding how can I substitute "ob.GetType().GetProperty(name)" with "ob.GetType().GetPropertyInfo(name)" ? If anyone has any suggestions I'll greatly appreciate it.

As for Sergey's comment that this is not a Hibernate but rather Castle Dynamic proxy problem, I disagree. Yes, DynamicProxy is the cause of the problem, but NHibernate decide to use this library and I cannot take advantage of NHibernate features such as lazy loading because of this problem, so it should be concern of NHibernate as well.

More details on the problem I have:

My class definition:

class ParentEntity
{
[ManyToOne(Name = "ChildEntity", Column = "`ChildID`",
ClassType = typeof(ChildEntity), Access = "field.camelcase")]
[DataMember]
private ChildEntity childEntity;

public virtual ChildEntity ChildEntity {
get { return this.childEntity; }
set { this.childEntity = value; }
}


public virtual string ParentName {..}
}

On the front-end WPF

<TextBlock Text="{Binding Path=ParentName}" /> - no problem

<TextBlock Text="{Binding Path=ChildEntity.Name}" /> -'AmbiguousMatchException'


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 15, 2008 6:04 pm 
Beginner
Beginner

Joined: Tue Sep 04, 2007 12:36 pm
Posts: 23
Yes this is an ugly little side of of the dynamic proxy indeed.

I too ran into this bad surprise. I can offer a "fair" workaround...

Consider the following function:
Code:
public static object UnProxy(object obj)
      {
         if (obj is INHibernateProxy)
         {
            LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer
                    ((INHibernateProxy)obj);
            return li.GetImplementation();
         }
         return obj;
      }


If you have the ability to add this to the GETTER of the property you need to bind to, then youre all set. for example:
Code:
<GridViewColumn Header="Destination" DisplayMemberBinding="{Binding ParentClass.PropertyX}"/>


and in your child class:
Code:
public virtual ParentClass ParentClass
{
get{return UnProxy(m_ParentClass) as ParentClass;}
}

and the ambiguousmatchexception will go away, since the dynamic proxy is no longer a factor.

_________________
^^If this post helped you, make sure to rate it Thanks!


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