Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2206
  • Last Modified:

Could Not Resolve Property Hibernate

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
Phreak3eb
Asked:
Phreak3eb
  • 4
  • 4
1 Solution
 
mrjoltcolaCommented:
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
 
Phreak3ebAuthor Commented:
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
 
Phreak3ebAuthor Commented:
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
Technology Partners: 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!

 
mrjoltcolaCommented:
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
 
Phreak3ebAuthor Commented:
So my get/set's should NOT have _ in them?  Just getUsername/setUsername?
0
 
mrjoltcolaCommented:
>>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
 
gudii9Commented:
Seems to be annotated column name mapping issue only. Please check and follow proper naming standards, match exact table column names
0
 
Phreak3ebAuthor Commented:
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
 
mrjoltcolaCommented:
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now