aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Basic Error - Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Basic Error - " Watch "Hibernate Basic Error - " New topic
Author

Hibernate Basic Error -

nilesh Katakkar
Ranch Hand

Joined: Oct 27, 2004
Posts: 35
As part of evaluating feasiblity of hibernate to use in our new project ran across following error -

Adding isXXX method where XXX is a string field throws ClassCastException. Might have to with the way hibernate do reflection. However, seems quite basic error.

<hibernate-mapping>
<class name="AccountType" table="ACCOUNT_TYPE">
<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">
SQ_ACCT_TYPE_ID</param>
</generator>
</id>
<property name="description" type="string"
column="ACCOUNT_DESC"/>
<property name="active" type="string" column="ACTIVE"/>

</class>
</hibernater-mapping>

Active is a varchar2(1) column defined in DB (Oracle 9ir2) and has a check constraint on it to check the inserted / updated value is either Y or N. The persistent POJO class has correspoding get and set for field active. However, to avoid string comparision with Y or N in all other classes, it makes sense to provide utility method in this class to find if account type is active or not.

Class Definition ..

public class AccountType {

private static String EMPTY="", YES="Y", NO="N";
private long id;

private string description, active;

public long getId(){ ...}
public void setId(long id){.....}
public String getDescription(){.....}
public void setDecription(String desc){....}
public String getActive(){...}
public void setActive(String s){...}
/// utility method

public boolean isActive(){

if(active == null || active.equals(EMPTY)) return false;

if(active.equals(YES)) return true;

return false;

}

}

Adding above utility method isActive throws classcastexception. If I change the method name from isActive to isTypeActive or something else , it works fine. So it might have to with the way reflection works in hibernate.

However, if mapping file says active is of type String, and as long as there are right getActive and setActive methods defined correctly, why should it matter to hibernate if there's another isActive or whatever. Sounds like a pretty basic error to me.


nilesh<br />neilindallas@hotmail.com
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
According to JavaBeans naming conventions isXXX and getXXX are used for the same property. As stated in Hibernate doc (if I remember well) your POJOs must conform to JavaBeans spec. I agree with you that this seems pretty easy, but also it is very easy to avoid .

./pope


blog - InfoQ.com
nilesh Katakkar
Ranch Hand

Joined: Oct 27, 2004
Posts: 35
Yeah it's easy to avoid by changing the method name.

However point is not that. As per Java Bean specifications, isXXX methods are for boolean variables. When a mapping file clearly says type is String, Hibernate should be looking for getXXX / setXXX . Why should isXXX method matter when there's no boolean property mention in mapping ? kinda looks silly...especially when you are evaluating feasibility of a product for your project. I am just hoping we don't run across any such basic errors.. or I would be hard pressed to sell it to my team to use Hibernate !
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Indeed you are noticing this with Hibernate. But the real problem comes from non-respecting the JB spec. You have a string field and 1 correct setter, 1 correct getter and 1 wrong getter for the same property. JB is based on naming conventions and not on type conventions.

I really thing that the feasability of a product does not really depends on this. Moreover when you break a rule you must expect undetermined behavior.

./pope
nilesh Katakkar
Ranch Hand

Joined: Oct 27, 2004
Posts: 35
So lets say I have a correct JB specification adhering class, are you saying that I don't need mapping file ? If hibernate was smart enough to figure out what properties I "have" in mind to persist, it wouldn't ask me for a mapping file. No one can do that. Now if it's asking me for a mapping file, then it better be looking at it, and decide what to do exactly based on that NOT on what mathods I have in my class.

Whats the purpose of providing Mapping File ? Well.. if I understand it correctly, it's so that hibernate can manage persistance for your class and generate SQL statements at run time (i.e dynamic - insert/update = true) Now, hibernate docs say that your persistant class can be any Plain Old Java Class. You can write it as per JB specification , or just use existing normal java class AND map it through mapping file.If mapping file says string it better be looking for string. What should matter if I have isXXX method for same parameter or not.
Agreed JB specification go by naming conventions. And it says that if a property is boolean then method name has to be isXXX. I may be wrong, but does it also mean that if there's a method isXXX, it automatically means that there's a corresponding boolean property ? Does JB specs say that I cannot have any isXXX method in my class for some string parameter along with get / set for my convenience to avoid certain redundant operation? What if my property is derived from certain state variables ?
Hibernate never ever says in the doc that every method in you JB or Java class has to map to persistent parameter, does it ? Parameter map to a set/get method. So when I mention parameter as string why should it bother to it that I have isXXX method? ( please correct me if I am wrong. I am pretty new to hibernate. )If mapping file is required and it says String.. try explaning why hibernate still looks for isXXX method. If I am missing something, I will be really glad to find out that this is my own stupid mistake.

As far as feasibility goes.. well, tell your manager that we found such and such basic errors.. BUT there are "work arounds" (which for your
(d)amager mean no clean solution) So overall we think that though this product doesnt support SP for integrating with existing legacy system (where data can only be retrieved through SP call)..though this product has few basic errors with work arounds .. we should still use it !!

And if you read my first post correctly, I said I am hoping we don't find any more limitations / errors in what we are doing. You choose a product to suit your needs.... not a product to suit its need !

In all our application we do not allow deletes of master data. Soft deletes are available that is column "Active" maintains the state. So I can easily go to my team and tell them.. hey not a big deal.. just change method name.. and change corresponding dependent classes.. cause the product you chose need it.
Thanks mate.. you are basically asking me to look for another job

Peace.

Lack of support for store procs is a major issue integrating with legacy apps. But I hear its done in 3.0 )

[ November 15, 2004: Message edited by: nilesh Katakkar ]
[ November 15, 2004: Message edited by: nilesh Katakkar ]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995

So lets say I have a correct JB specification adhering class, are you saying that I don't need mapping file ?


I wasn't. Moreover I can read in chapter 4.1.1:

Cat declares accessor methods for all its persistent fields. Many other ORM tools directly persist instance variables.
We believe it is far better to decouple this implementation detail from the persistence mechanism. Hibernate
persists JavaBeans style properties, and recognizes method names of the form getFoo, isFoo and set-
Foo.
Properties need not be declared public - Hibernate can persist a property with a default, protected or private
get / set pair.



Agreed JB specification go by naming conventions. And it says that if a property is boolean then method name has to be isXXX. I may be wrong, but does it also mean that if there's a method isXXX, it automatically means that there's a corresponding boolean property ?


If the property is boolean the method can be isXXX. Obviously it is the other way around true.


As far as feasibility goes.. well, tell your manager that we found such and such basic errors.. BUT there are "work arounds" (which for your
(d)amager mean no clean solution)


I am not getting you on the above.


You choose a product to suit your needs.... not a product to suit its need !


Fully agree with you. With a completion: if the product does not suit your needs you try to suggest corrections or you pass to another one.


So I can easily go to my team and tell them.. hey not a big deal.. just change method name.. and change corresponding dependent classes.. cause the product you chose need it.
Thanks mate.. you are basically asking me to look for another job


Again I am not getting you.

Now in order to fix the first problem you probably can use: property mapping with access set to field and not property. This would solve the problem without breaking your code.


./pope
[ November 15, 2004: Message edited by: Ali Pope ]
 
Don't get me started about those stupid light bulbs.
 
subject: Hibernate Basic Error -