Java org.hibernate.hql.ast.QuerySyntaxException:表未映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23756363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.hql.ast.QuerySyntaxException: table not mapped
提问by burning
I know many people have asked this question but I just started Java so I couldn't figure out still.
我知道很多人都问过这个问题,但我刚刚开始使用 Java,所以我仍然无法弄清楚。
So here is my problem:
I am writing RESTful webservices using Javarestlet
. Here is the snippet of my DAO
file.
所以这是我的问题:我正在使用Javarestlet
. 这是我的DAO
文件的片段。
try {
session.beginTransaction();
String query = "select number from blockedcli";
@SuppressWarnings("rawtypes")
List list = session.createQuery(query).list(); //.setString("sId", businessId)
logger.error("*******************list*****************************************");
logger.error(list);
logger.error("*******************listend*****************************************");
@SuppressWarnings("rawtypes")
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
blockedcli = (BlockedCli) iterator.next();
}
session.getTransaction().commit();
}
Correspondingly my entity class looks like .
相应地,我的实体类看起来像 .
@Entity
@Table(name = "blockedcli")
public class BlockedCli implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="idBlockedCLI", nullable = false, unique=true)
private Integer idBlockedCLI;
@Column(name = "number",nullable = false, length=45)
private String number;
@Column(name = "type", nullable = false)
private Integer type;
.
I have placed a BlackListedN.hbm.xml
file in my config directory with following text .
我BlackListedN.hbm.xml
在我的 config 目录中放置了一个文件,其中包含以下文本。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="tecd.persistence.entity.BlockedCli" table="blockedcli">
<id name="idBlockedCLI" type="long" unsaved-value="null">
<column name="idBlockedCLI" not-null="true"/>
<generator class="identity"/>
</id>
<property name="number">
<column name="number" not-null="true" />
</property>
</class>
</hibernate-mapping>
As I want to display only number .
因为我只想显示 number 。
And Here is the DB table .
这是数据库表。
+--------------+--------------+--------+------+---------------------+---------------------+---------------------------------------+
| idBlockedCLI | number | status | type | createdDT | updatedDT | BusinessDirectory_idBusinessDirectory |
+--------------+--------------+--------+------+---------------------+---------------------+---------------------------------------+
| 1 | 919845611234 | 1 | 1 | 2014-03-24 13:31:20 | 2014-03-24 13:31:20 | 1 |
+--------------+--------------+--------+------+---------------------+---------------------+---------------------------------------+
But when I ran this Everytime it says
但是当我运行这个 Everytime 它说
org.hibernate.hql.ast.QuerySyntaxException: blockedcli is not mapped [select number from blockedcli]
Please help me out to rectify this issue .
请帮我解决这个问题。
This is my first Java program so I am not sure what else is required to elaborate this however do let me know if anything else required.
这是我的第一个 Java 程序,所以我不确定还需要什么来详细说明这个,但是如果需要其他任何东西,请告诉我。
回答by Aleksander Blomsk?ld
When using HQL, you need to refer to correctly capitalized entity names/properties, not the table names/column names. Simply change your query to select number from BlockedCli
, and it should work.
使用 HQL 时,您需要引用正确大写的实体名称/属性,而不是表名称/列名称。只需将您的查询更改为select number from BlockedCli
,它应该可以工作。
回答by Wundwin Born
Try changing entity name in query
尝试更改查询中的实体名称
select b.number from BlockedCli b
OR if you don't want to change query name, you can set name
attribute of Entity
annotation
或者,如果您不想更改查询名称,则可以设置注释的name
属性Entity
@Entity(name="blockedcli")
回答by Shanky Munjal
I faced this issue but in my case the problem was due to gradle version. When I changed my system from linux to mac then I had to switch from gradle-1.0-milestone-3 to gradle-1.0-milestone-4 as milestone-3 does not work in OSX. And in gradle-1.0-milestone-4 I faced the same issue then I had to degrade my gradle version to gradle-1.0-milestone-1. Now it is working fine
我遇到了这个问题,但就我而言,问题是由于 gradle 版本。当我将系统从 linux 更改为 mac 时,我不得不从 gradle-1.0-milestone-3 切换到 gradle-1.0-milestone-4,因为里程碑 3 在 OSX 中不起作用。在 gradle-1.0-milestone-4 中,我遇到了同样的问题,然后我不得不将我的 gradle 版本降级为 gradle-1.0-milestone-1。现在它工作正常
回答by Vivek Gulati
Also check for hibernate-cfg.xml
whether it contains entry for mapping java object.
还要检查hibernate-cfg.xml
它是否包含映射 java 对象的条目。
<mapping class="com.yourpackage.App" />
<mapping class="com.yourpackage.App" />
回答by CrazyJavaLearner
Set a name for Entity annotation
为实体注释设置名称
@Entity(name="blockedcli")
Then set your query as below.
然后设置您的查询如下。
String query = "select b.number from blockedcli b";