Solved

Could Not Resolve Property Hibernate

Posted on 2009-07-01
9
2,088 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

624 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