gpt4 book ai didi

java - Hibernate中如何从数据库中获取数据

转载 作者:行者123 更新时间:2023-12-05 01:09:19 25 4
gpt4 key购买 nike

这是我从数据库中获取数据的类

package com.javatpoint.mypackage;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;

public class retrive {
public static void main(String args[]) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the
// configuration file

// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();

// creating session object
Session session = factory.openSession();

// creating transaction object
Transaction t = session.beginTransaction();

Query query = session.createQuery("from EMPLOYEE");
java.util.List list = query.list();
System.out.println(list);
t.commit();
session.close();
}
}

这是我的 Emplouyee.hbm.xml 文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 5 Dec, 2013 12:09:18 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping default-lazy="false">
<class name="com.javatpoint.mypackage.Employee" table="EMPLOYEE">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property generated="never" lazy="false" name="firstName"
type="java.lang.String">
<column name="FIRSTNAME" />
</property>
<property generated="never" lazy="false" name="lastName"
type="java.lang.String">
<column name="LASTNAME" />
</property>
</class>
</hibernate-mapping>

当我运行这个程序然后出现异常请帮助我如何修复它我是 Hibernate 的新手并尝试学习但被卡住了。

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: 
EMPLOYEE is not mapped [from EMPLOYEE]

虽然我能够将数据存储在数据库中,但我有 2 个类,第一个用于 String data 和第二个获取数据 获取数据时出现问题请帮忙。

最佳答案

让我引用一下:

Hibernate 创造了一种名为 Hibernate Query Language (HQL) 的新语言,语法与数据库 SQL 语言非常相似。两者的主要区别是 HQL 使用类名而不是表名,使用属性名而不是列名。

据我所知,您使用的是表名。

所以应该是这样的:

Query query = session.createQuery("from Employee");

关于java - Hibernate中如何从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20781286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com