Solved

Could Not Resolve Property Hibernate

Posted on 2009-07-01
9
2,052 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

752 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