Why is an object found by id in JPA, but not through a JPQL query?

I have a JUnit 4 test case with the Spring @Transactional annotation that saves an object, and then attempts to find it. The test case passes when I use this implementation:

@Override public EventSummary findEventSummaryById(Integer id) { return em.find(EventSummary.class, id); }

It fails when I use this implementation (and then change which method I call in the test case):

@Override public EventSummary findEventSummary(Integer id) { Query query = em.createQuery("select es from EventSummary as es where es.id = :id"); query.setParameter("id", id); EventSummary result = (EventSummary) query.getSingleResult(); return result; }

-------------Problems Reply------------

If you are using the default flush mode (AUTO) and if you are executing your query within a transaction, the JPA specification guarantees that a Query will not return stale or incorrect data:

3.6.2 Queries and FlushMode

The flush mode setting affects the result of a query as follows.

When queries are executed within a transaction, if FlushModeType.AUTO is set on the Query object, or if the flush mode setting for the persistence context is AUTO (the default) and a flush mode setting has not been specified for the Query object, the persistence provider is responsible for ensuring that all updates to the state of all entities in the persistence context which could potentially affect the result of the query are visible to the processing of the query. The persistence provider implementation may achieve this by flushing those entities to the database or by some other means. If FlushModeType.COMMIT is set, the effect of updates made to entities in the persistence context upon queries is unspecified.

public enum FlushModeType {

If there is no transaction active, the persistence provider must not flush to the database.

Assuming that you are using AUTO, check the transactional side.

Тhe entity is in the current session (entity manager) - it is in persistent state, waiting to be flushed. The get method first checks the contents of the session, and if not found turns to the underlying database. In your case, the entity has just been saved in the same session, so it is found and returned.

Update: it turned out the problem is using an incorrect transaction manager, hence the entity hasn't been flushed to the database. See Pascal's explanation.

in the first case, the id is an Integer

in the second, the id is a String

an Integer will never equal a String

Category:hibernate Views:0 Time:2010-08-05

Related post

  • Hibernate (JPA) how to do an eager query, loading all child objects 2008-09-16

    Relating to my earlier question, I want to ensure all the child objects are loaded as I have a multiple threads that may need to access the data (and thus avoid lazy loading exceptions). I understand the way to do this is to use the "fetch" keyword i

  • Storing Objects in columns using Hibernate JPA 2010-01-23

    Is it possible to store something like the following using only one table? Right now, what hibernate will do is create two tables, one for Families and one for people. I would like for the familymembers object to be serialized into the column in the

  • Duplicate an AppEngine Query object to create variations of a filter without affecting the base query 2010-05-08

    In my AppEngine project I have a need to use a certain filter as a base then apply various different extra filters to the end, retrieving the different result sets separately. e.g.: base_query = MyModel.all().filter('mainfilter', 123) Then I need to

  • Transfer objects only once via EJB / JPA(Hibernate) 2010-09-30

    I am using the Java Enterprise Edition with JPA(Hibernate) for a Server-Client application (via EJB). The program stores a lot of data in a database that needs to be transfered to the client. Because I want to transfer as less data as possible I do n

  • Object Reference Not Set to an Instance within LINQ to SQL Query 2011-06-30

    I'm having a problem with a LINQ to SQL query, the data being returned from the server could be null so I've set up an if statement within the query but the query is still throwing an exception. This is a shortened down version of the query code var

  • JPA / Hibernate: CriteriaBuilder - How to create query using relationship object? 2012-01-25

    I have the following four tables: SCHEDULE_REQUEST TABLE: ID, APPLICATION_ID (FK) APPLICATION TABLE: ID. CODE USER_APPLICATION TABLE: APPLICATION_ID (FK), USER_ID (FK) USER TABLE: ID, NAME Now I wanted to create Criteria builder where condition is to

  • How to retrieve an object with filtered children through JPA 2012-02-10

    I have a specific problem I can't seem to find an answer to. I'm currently working in a Java Spring MVC environment, using Hibernate JPA. In this situation, simplified, you could say I have following hierarchy: Form entity with @OneToMany List<Pag

  • Returning JSON object or String from a JAX-RS method depending on a query parameter 2012-03-01

    I have a JAX-RS servlet and I would like to return a JSON object for GET queries to the URL /upgrade/somePath but a String for GET queries to /upgrade/somePath?count=true. My problem is that this method cannot return two different types based on the

  • Select object from joined table with JPA Criteria-API 2012-02-17

    I'm fighting about two days with the following problem and hope you can give me a push into the right direction. The tutorials and examples I found during my research always only showed how to easy joins work in the criteria api. First of all I have

  • Is data in Django's request.POST object sanitised, at least enough for direct use in an ORM query? 2012-02-28

    I’m building a web forum using Django, including the built-in authentication module. I’m using the built-in UserCreationForm to register users. However, as I‘ve decided to use e-mail addresses as the sole way to identify users, I’m generating a usern

  • Deserialize Object using JDBC stored w/ JPA 2012-03-21

    I have a class which serializes an Image, and stores & retrieves it from a database using JPA. I've decided to use Base64 instead of a serialization to ease up the process for other developers, so I am implementing at the startup of my applicatio

  • Methods for caching PHP objects to file? 2008-09-24

    In ASPNET, I grew to love the Application and Cache stores. They're awesome. For the uninitiated, you can just throw your data-logic objects into them, and hey-presto, you only need query the database once for a bit of data. By far one of the best AS

  • Passing Objects via QueryString 2008-10-01

    I have object A which in turn has a property of type Object B Class A property x as Object B End Class On my ASP.NET page when I select a gridview item which maps to an object of type A I serialize the object onto the QueryString and pass it to the n

  • Java: Save objects in a textfile? Are there readymade solutions? 2008-10-04

    I want to save the objects I generated in a program. After restart the App should load automaticly all Objects in an Array. I want to write them in a file and parse them after restart. Are the other smarter possibilities than do it by hand? Thank you

  • Can Hibernate return a collection of result objects OTHER than a List? 2009-01-06

    Does the Hibernate API support object result sets in the form of a collection other than a List? For example, I have process that runs hundreds of thousands of iterations in order to create some data for a client. This process uses records from a Val

  • TypeError: 'NoneType' object is not iterable 2009-02-09

    I need to process mysql data one row at a time and i have selected all rows put them in a tuple but i get the error above. what does this mean and how do I go about it? --------------Solutions------------- Provide some code. You probably call some fu

  • Return related objects in select linq to sql 2009-03-06

    I have 2 tables Account and Address which has 1:1 relationsip; account has addressid in it. I have created the association in the dbml file. Now I want to write a query to select the account for given accountid and the resulting account should contai

  • Order a parent object by a child's property in LINQ 2009-03-11

    How do I order by child objects in LINQ? Classes A, B, and C. A has a collection of B and B has a collection of C. I want to order object A by the Ordinal (int) property of C. var query = from a in db.A orderby a.Bs.OrderBy(x=> x.C.Ordinal) <--

  • How do I add an object with the entity framework when it has a foreign key object that already exists 2009-03-31

    I have 2 tables: tblAuthors - id - username tblPosts - id - authorid So if I create a new Post var post = new Post { Id=Guid.NewId(), Author=new Author { UserName="username", Id="id in database } }; The author of the post is already in the database,

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.094 (s). 11 q(s)