Solved

Could Not Resolve Property Hibernate

Posted on 2009-07-01
9
2,002 Views
Last Modified: 2013-12-15
I'll preface this question with the fact I'm new to Java and Hibernate, but have been using NHibernate for awhile with .Net.

All classes are mapped with annotations, not .hbm.xml files.  Although the mapping appears to be correct as hbm2ddl creates the database tables, columns, and relationships correctly.

I have this code that works.  It will do exactly what it says, save or update a user object that is passed to it:

public void Save(User user)
    {          
            Transaction transaction = _sessionManager.get_CurrentSession().beginTransaction();
        _sessionManager.get_CurrentSession().saveOrUpdate(user);
        transaction.commit();
    }

However, when I try to use this code, (function name and param will actually use an Id later, for now, I'm testing with username) I get " org.hibernate.QueryException: could not resolve property: Username of: com.domain.models.people.User".  I've tried using a different property as well, and get the same error.

public User GetUserById(String id)
      {
            Transaction transaction = _sessionManager.get_CurrentSession().beginTransaction();
            Criteria criteria = _sessionManager.get_CurrentSession().createCriteria(User.class);
            criteria.add(Restrictions.eq("Username", id));
            User user = (User)criteria.uniqueResult();
            transaction.commit();
            return user;
      }

I'm sure I'm doing something stupid and overlooking a simple cause of the problem, but for the life of me, can't figure it out.  Any ideas?

I can post more information if needed/requested.  Thanks.
0
Comment
Question by:Phreak3eb
  • 4
  • 4
9 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24754675
You have the setUsername/getUsername methods defined I assume.
Try lowercasing the property name in the criteria.

Restrictions.eq("username", ...)

I always use the standard notation and casing, which is lowercase for the property name.
0
 

Author Comment

by:Phreak3eb
ID: 24754938
Yes.  I changed everything to lowercase and it still doesn't work.  Here are the props.

       @Column(name = "username", nullable = false, length = 30)
      public String get_username()
      {
            return _username;
      }      

        public void set_username(String username)
      {
            this._username = username;
      }
0
 

Author Comment

by:Phreak3eb
ID: 24755213
So apparently Hibernate is looking at the members of the class.  I tried _username, which is a private member of the class User.  It finds the property that way.  I had no idea.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24755374
That is not the proper bean syntax.

In Java a property is:

foo = getFoo/setFoo


Hibernate wants to access via the standard Java bean standard naming convention.

so username = getUsername/setUsername not get_username/set_username

The rule is it will take the first character of the property name, upper case it, then add get|set to it.

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Phreak3eb
ID: 24755738
So my get/set's should NOT have _ in them?  Just getUsername/setUsername?
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24755791
>>So my get/set's should NOT have _ in them?  Just getUsername/setUsername?

If you expect to use the normal propertly name "username", then yes.

You are thinking more C# style properties, as they are mapped with underscores in the .NET engine if I recall.

The Java formula is uppercase the property first character, and prepend get|set to it.


Property: abc
Setter:   setAbc()
Getter:   getAbc()
0
 
LVL 7

Expert Comment

by:gudii9
ID: 24755884
Seems to be annotated column name mapping issue only. Please check and follow proper naming standards, match exact table column names
0
 

Author Comment

by:Phreak3eb
ID: 24756633
Ok.  I see what it's doing now.  I changed my gets/sets to leave out the _.  

@mrjoltcola:  yeah, I come from a C# background, so I guess it's bleeding through a bit and causing me problems. :)
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24756652
Also, a trick you might not know if you are new to Eclipse, you can define your member variables and then right click in the source file

Source -> Generate Getters and Setters

That will generate the default methods for you

Glad to help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now