-->
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.  [ 11 posts ] 
Author Message
 Post subject: many-to-one field types as proxy
PostPosted: Mon Apr 26, 2004 4:05 am 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
Hello everyone,

I tried to solve my problem myself, read the documentation, google, and forums whatever. But if I make a duplicate entry sorry for wasting your time.

I m using hibernate 2.1. I made a quite big(~50 classes) model for my bookstore. Some big classes have proxy interfaces for cglib lazy initialization. (which is addvised.)

Now I m confused that am I missing sth. For example think of a Book class extending from Product (which has BookProxy as interface). Book has a publisher field. A publisher is still big class which has a PublisherProxy interface.

Now question arrives. First according to my model, I planned Book's publisher field is type PublisherProxy. When I try this, I come to a point that PublisherProxy is not known by hibernate(is not declared as class).

So I think that, whatever, it will be allready lazy initited, so I can make publisher property of Book as type Publisher. But the problem did not solved again. This time I get an exception about propertyAccessAcception coming from cglib. Hibernate is trying to set the property, the value is cglib created one, and it cant be.

In forums its talket about null values of primitive types but, as in this example; while trying to set the publisher field of Book problem arises.

So my questions are, can I use Proxy interfaces as many-to-one types? and, how can I get out of this situation.

thanx for your help, and leaving time for this
Ahmet


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I have never understood why this is difficult, but some people find it so.

It is as simple as:

Code:
<class name="FooImpl" proxy="Foo">....<class>



Code:
<many-to-one name="foo" class="FooImpl"/>



Code:
private Foo foo;
public void setFoo(Foo foo) {
   this.foo=foo;
}
public Foo getFoo() {
   return foo;
}


I dunno, seems incredibly intuitive to me.

I think people's brains have just been fried by too much experience with 2.1-style entiity beans.

Looks like we need to add a FAQ on this.


Top
 Profile  
 
 Post subject: this is exacty what I did second time
PostPosted: Mon Apr 26, 2004 4:33 am 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
Hi Gavin,

Thanx for your reply. I must admit that it was too fast. The example you give was exactly what I did second time. But tis time when I try to load the entity, I get an exception "PropertyAccessAcception".

foo property(in my case publisher) is tried set by an cglib proxy and exception occurs. So whats the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I have no idea. Use your debugger to find your bug.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try disabling cglib reflection optimizer. Also are you sure your classes really implement the proxy interface?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 1:20 pm 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
In my hibernate.properties file(which is on web-inf\classes) it says:

## enable CGLIB reflection optimizer (enabled by default)
hibernate.cglib.use_reflection_optimizer=false

so it should be disabled.

and yes, the Publisher class implements PublisherProxy.

Here's detailed info even if no one asked:

my mapping document is like:

<class name="tr.com.bin984.model.Publisher"
proxy="tr.com.bin984.model.PublisherProxy"
table="publisher">
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="128"/>
<property name="companyName" length="256"/>
<property name="URL" length="128"/>
<property name="email" length="64"/>
<list name="series" inverse="true">
<key column="publisher"/>
<index column="posn"/>
<one-to-many class="tr.com.bin984.model.PublisherSerie"/>
</list>
<list name="addresses">
<key column="publisher_id"/>
<index column="posn"/>
<one-to-many class="tr.com.bin984.model.address.Address"/>
</list>

</class>

<class name="tr.com.bin984.model.PublisherSerie" table="publisher_serie">
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="128"/>
<many-to-one name="publisher" class="tr.com.bin984.model.Publisher"/>
</class>


and classes are :

public class Publisher implements PublisherProxy {

private String companyName;
private List series;
private List addresses;
private String URL;
private String email;

// proxy fields
private Long id;
private String name;

public Publisher() {
}

public String getURL() { return URL; }
public void setURL(String uRL) { this.URL = uRL; }

public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }

public String getCompanyName() { return companyName; }
public void setCompanyName(String companyName) { this.companyName = companyName; }

public List getAddresses() { return addresses; }
public void setAddresses(List addresses) { this.addresses = addresses; }

public List getSeries() { return series; }
public void setSeries(List series) { this.series = series; }

// proxy methods
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }

public String getName() { return name; }
public void setName(String name) { this.name = name; }

}

public interface PublisherProxy {

public Long getId();
public void setId(Long id);
public String getName();
public void setName(String name);
}

public class PublisherSerie {

private Long id;
private String name;
private Publisher publisher;

public PublisherSerie(){}
public PublisherSerie(Publisher ps, String name){
this.publisher = ps;
this.name = name;
}

public Long getId() { return id; }
public void setId(Long id) { this.id = id; }

public String getName() { return name; }
public void setName(String name) { this.name = name; }

public Publisher getPublisher() { return publisher; }
public void setPublisher(Publisher publisher) { this.publisher = publisher; }

}

and the Book class has publisher field

<many-to-one name="publisher" class="tr.com.bin984.model.Publisher"/>

which is defined as Publisher type.


when I execute the code:

Session ses = PersistenceUtil.currentSession();
Query q = ses.createQuery("from Book");
q.setMaxResults(5);
List bookProxyList=q.list();

it throws this exc.

net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of tr.com.bin984.model.PublisherSerie.publisher
net.sf.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:68)
net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:222)
net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2174)
net.sf.hibernate.loader.Loader.doQuery(Loader.java:240)
net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
net.sf.hibernate.loader.Loader.loadEntity(Loader.java:831)
net.sf.hibernate.loader.Loader.loadEntity(Loader.java:851)
net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)
net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)
net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2081)
net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1955)
net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1917)
net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:68)
net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:215)
net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2169)
net.sf.hibernate.loader.Loader.doQuery(Loader.java:240)
net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
net.sf.hibernate.loader.Loader.doList(Loader.java:950)
net.sf.hibernate.loader.Loader.list(Loader.java:941)
net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834)
net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1512)
net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
tr.com.bin984.webapp.actions.SetReadCat.execute(SetReadCat.java:42)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)


So I didn't understand why this happens, it happens in hibernate code. Maybe I make a mistake somewhere but I cant understand how this become my bug ;)

Its our bug!

If I could solve this Neither I would be your headache, nor put myself in some stupid position; besides I would be happy to share it with the community.

But I cant understand why...

_________________
a y


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 2:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try enabling logging for net.sf.hibernate.property.BasicPropertyAccessor in your log4j config and take a look - should give you some more detailed error messages.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 3:07 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Generated proxy is not an instance of Publisher , it just implements PublisherProxy. Property must be declared as interface type or proxy must exetend Publisher "proxy=Publisher".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 3:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Man, you are right baliukas. I should not rely on people saying "I did exactly that" to skip reading their code ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:08 pm 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
I am ver Sorry, I misread Gavins code, take your time.

But didnt we come back to the starting point?

since bookproxy defines siganture as:

public PublisherProxy getPublisher();
public void setPublisher(PublisherProxy publisher);

and ok, so the Book. But since we defined in xml as:

<many-to-one name="publisher" class="tr.com.bin984.model.Publisher"/>

when I execute the code in jsp:

book.getPublisher().getName()

it gives

java.lang.NoSuchMethodError: tr.com.bin984.model.BookProxy.getPublisher()Ltr/com/bin984/model/Publisher;
at org.apache.jsp.pages.CategoryDetail_jsp._jspService(CategoryDetail_jsp.java:96)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)


Am I still missing something?

_________________
a y


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:24 pm 
Newbie

Joined: Mon Apr 26, 2004 3:48 am
Posts: 18
Location: Istanbul, Turkiye
And sorry again. After restarting Tomcat changes take effect. (Its seen that signature is different that I mention in stack trace, I even check the deployed class by decompiler to verify, but only restart worked to changes take effect.)

Thanx to everyone who pay attention, and leave some time.

This is the most active community I have ever seen.

_________________
a y


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