0

i am trying to do my first steps in Java, and i am blocked with the next problem: i have this hieararchy class "User -> Traveller".

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class User {
   // Some properties and accessors.
}

@Entity
@PrimaryKeyJoinColumn(name="user_id")
public class Traveller extends User{ 
   // Some properties and accessors.
}

So, i need search a traveller by your username which is a User class property. I try this in my TravellerDao:

public Traveller findByUsername(String username){

    EntityManager em = PersistenceHelper.getEm();
    TypedQuery<Traveller> query = em.createQuery("FROM Traveller WHERE username = ?1", Traveller.class);
    query.setParameter(1, username);

    return query.getSingleResult();
}

But, a javax.persitence.NoResultException is catched. Where is the problem ? In the "where" clausule ? In the join ?. Any ideas ?

Notes:

1) The hierarchy works fine with "find" method. And the database is generated correctly.

2) I have an entry in the Traveller table that links up to one in User table.

3) In the database exists an user with the username that i am using to test.

4) The method get correctly the "username" parameter.

EDIT: Now works. The problem was the execution order of test cases. The query works fine.

ramiromd
  • 2,019
  • 8
  • 33
  • 62

1 Answers1

1

I think your query is incorrect, I believe you are forced to name an alias for every entity you want to interact with. Have you tried this?

"SELECT t FROM Traveller t WHERE t.username = ?1", Traveller.class

Also you should have to cast the result when you call query.getSingleResult(). Because you pass in Traveller.class into the createQuery call which is paramaterized by Class<T> resultClass

Edit: Added a 1 after the ? as shown here. http://www.objectdb.com/java/jpa/query/parameter#Ordinal_Parameters_index_

 public Country getCountryByName(EntityManager em, String name) {
    TypedQuery<Country> query = em.createQuery(
        "SELECT c FROM Country c WHERE c.name = ?1", Country.class);
    return query.setParameter(1, name).getSingleResult();
  } 
Jazzepi
  • 5,259
  • 11
  • 55
  • 81
  • Change the query, but the results is the same. – ramiromd Nov 29 '14 at 03:30
  • Have you verified your database has the correct data in it by looking at an SQL query? Do you see a entry in the Traveller table that links up to one in User table? Do they have the username that you expect? Have you checked to make sure that you're passing the right username into the findByUsername method? I would do a system.out.println() of the username right before the SQL runs for something quick and dirty. – Jazzepi Nov 29 '14 at 03:32
  • Yes, i have an user with the username wanted. And yes, the parameter is ok. – ramiromd Nov 29 '14 at 04:11
  • Have you tried placing a 1 after the ? I'm not sure your parameter is getting hooked up to your query correctly. It looks like you need to do ?1 to match up your setParameter(1, blah) with the ? in your query. I updated my answer to match. – Jazzepi Nov 29 '14 at 07:35
  • Sorry :( I'm stumped. Everything you're doing looks right to me. – Jazzepi Nov 29 '14 at 17:47
  • You may want to turn on the Hibernate debugging so that you can see the generated query. – Jazzepi Nov 29 '14 at 17:48
  • Pretty good explanation of how to turn on your query logging here. You can also see the real SQL parameters inserted. http://stackoverflow.com/questions/2536829/hibernate-show-real-sql – Jazzepi Nov 29 '14 at 18:01
  • Thanks for your time ! I put in my bookmarks the link, but i found the solution. When run the TestCases to test my TravellerDao, the method findByUsernameTest is excecuted before that createTest. So, Hibernate search a Traveller that has not been persisted yet. – ramiromd Nov 29 '14 at 18:09