语法查询异常,对象无法映射

时间:2014-08-16 04:58:24

标签: java hibernate hql

所以我正在使用带注释的hibernate创建一个简单的Hello World。

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

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <!-- <property name="connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433/ProgAp3_B</property> -->
        <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/ProgAp3_B</property>
        <property name="connection.username">username</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>

        <!-- Disable the 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>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.Message"/>
    </session-factory>

</hibernate-configuration>

Message.java

package com;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity (name="Messages")
public class Message {
@Id @GeneratedValue
private Long id;
private String text;
@ManyToOne (cascade = CascadeType.ALL)
private Message nextMessage;

public Message() { }
public Message(String text) {
    this.text = text;
}
//geters and setters
}

HelloWorld.java

package com;

import java.util.*;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class HelloWorld {

    public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello world");
session.save(message);
tx.commit();
Transaction newTransaction = session.beginTransaction();
List<Message> messages = session.createQuery("from Message m order by m.text asc").list();
System.out.println( messages.size() + " message(s) found:" );
for (Iterator<Message> iterator = messages.iterator(); iterator.hasNext();) {
    Message loaded = iterator.next();
    System.out.println(loaded.getText());
}
newTransaction.commit();
session.close();
}

}

除第二笔交易外,一切正常。

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Message is not mapped [from Message m order by m.text asc]

由于HQL是面向对象的,我使用Message,因为它是我要映射的类。为什么没有映射Message对象? HQL有什么问题?

1 个答案:

答案 0 :(得分:1)

让我们讨论@Entity@Table

例如,我可以为name属性

设置相同的值
@Entity(name = "someThing")
@Table(name = "someThing")

我可以为同一个班级提供不同的名字

 @Entity(name = "someThing")
 @Table(name = "otherThing")



@Entity(name = "someThing") => this name will be used to name Entity
@Table(name = "someThing")  => this name will be used to name a table in DB

因此,在第一种情况下,您的表格和实体将具有相同的名称,这样您就可以在撰写HQLJPQL时访问与实体同名的表格。

在第二种情况下,在编写查询时,您必须使用 @Entity 中给出的名称,并且 @Table 中给出的名称将用于在DB中命名该表。

所以在HQL中你的 someThing 会引用数据库中的 otherThing

查询应该是:from Messages m order by m.text asc