我在我的JPA项目Eclipselink中使用NanedQueries(如下所示)作为持久性提供程序:
@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")
})
但是在我将Hibernate更改为持久性提供程序之后,我收到以下错误:
java.lang.IllegalArgumentException: org.hibernate.QueryException: unexpected char: '{' [SELECT...
我使用Hibernate 3.2.5(MySQL方言)
答案 0 :(得分:1)
没有您的确切配置会让这很困难。但我对hibernate 3.2.5.GA没有问题
我创建了以下Login Domain对象:
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;
}
}
和测试类:
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);
}
}
我的hibernate-annotation.hbm.xml文件如下:
<?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>
你可以在你的环境中尝试这个吗,让我知道你是如何进行的