7

I used NanedQueries (as follows) in my JPA project Eclipselink as persistence provider:

@Entity
@Table(name = "login")
@NamedQueries({
    @NamedQuery(name = "Login.random", query = "SELECT l FROM Login l WHERE l.pk = :randomPk"),
    @NamedQuery(name = "Login.max", query = "SELECT MAX(l.pk) FROM Login l")
})

But after I change Hibernate as my persistence provider, I get the following error:

java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char: '{' [SELECT...

I use Hibernate 3.2.5 (MySQL dialect)

5
  • Can you share your persistence.xml? Commented Dec 7, 2011 at 9:39
  • 4
    Could you show more of the exception that is thrown? I see "select" at the end of the exception, maybe the whole query is shown or something other relevent. Commented Jun 1, 2012 at 16:40
  • Try to see into the arguments you are passing through the query, perhaps there is something wrong with its value, debug your code or add a syso before executing the query. Commented Jun 14, 2012 at 21:25
  • for someone to help you, would need the persistence.xml and the stack trace.. post those two and you will really help yourself Commented Jun 27, 2012 at 10:36
  • have you also checked what imports you are using, maybe there is some conflict there (importing the wrong thing)? Commented Jun 27, 2012 at 10:42

1 Answer 1

1

Not having your exact configuration makes this difficult. But I didn't have an issue with hibernate 3.2.5.GA

I created the following Login Domain object:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;

@Entity
@Table(name = "login")
@NamedQueries({
        @NamedQuery(name = "Login.random", query = "select l FROM Login l WHERE l.pk = :randomPk"),
        @NamedQuery(name = "Login.max", query = "select max(l.pk) from Login l")
})
public class Login implements Serializable {

    private Long pk;
    private String username;

    public Login() {}

    public Login(Long pk, String username) {
        this.pk = pk;
        this.username = username;
    }

    @Id
    public Long getPk() {
        return pk;
    }

    public void setPk(Long pk) {
        this.pk = pk;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

and test class:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import sky.sns.domain.Login;

import static org.junit.Assert.assertEquals;

public class AnnotationTest {

    private static SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @BeforeClass
    public static void initialise() {
        sessionFactory = new AnnotationConfiguration().configure("/hibernate-annotation.cfg.xml").buildSessionFactory();
    }

    @Before
    public void setUp() {
        session = sessionFactory.getCurrentSession();
        transaction = session.beginTransaction();
    }

    @After
    public void tearDown() {
        transaction.rollback();
    }

    @Test
    public void createUser() {
        Login login = new Login(1L, "foo");
        session.save(login);
        session.flush();
        session.clear();
        Login persistedLogin = (Login)session.get(Login.class, 1L);
        assertEquals(login.getPk(), persistedLogin.getPk());
    }

    @Test
    public void obtainUserByIdNamedQuery() {
        Login login = new Login(1L, "foo");
        session.save(login);
        session.flush();
        session.clear();
        Login persistedLogin = (Login)session.getNamedQuery("Login.random").setLong("randomPk", 1L).uniqueResult();
        assertEquals(login.getPk(), persistedLogin.getPk());
    }

    @Test
    public void obtainMaxUserIdNamedQuery() {
        Login login = new Login(1L, "foo");
        session.save(login);
        session.flush();
        session.clear();
        Long maxId = (Long)session.getNamedQuery("Login.max").uniqueResult();
        assertEquals(login.getPk(), maxId);
    }
}

My hibernate-annotation.hbm.xml file is as follows:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_owner</property>
        <property name="hibernate.connection.username">hibernate_owner</property>
        <property name="hibernate.connection.password">hibernate</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable second-level cache. -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <property name="hibernate.format_sql">false</property>
        <property name="hibernate.use_sql_comments">false</property>

        <mapping class="sky.sns.domain.Login" />

    </session-factory>
</hibernate-configuration>

Can you try this in your environment and let me know how you get on

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.