Solved

Could Not Resolve Property Hibernate

Posted on 2009-07-01
9
2,008 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 55
nextBoolean(double p) for Random class 3 35
web services creation SOAP vs REST 5 20
reverse digits of a number using for loop 5 36
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Creating and Managing Databases with phpMyAdmin in cPanel.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

17 Experts available now in Live!

Get 1:1 Help Now