gpt4 book ai didi

java - hibernate : Class is not mapped

转载 作者:行者123 更新时间:2023-12-01 12:32:52 26 4
gpt4 key购买 nike

Hibernate 抛出一个奇怪的错误

Hibernate: select user0_.userID as userID0_, user0_.password as password0_ from user user0_ where user0_.userID='admin' and user0_.password='admin' org.hibernate.hql.ast.QuerySyntaxException: action is not mapped [from action where id = 'MADEX240214'] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)

我的代码有什么问题吗?

public String insertActions(ArrayList<Action> data , String[] check)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = null;
try
{
for (int i = 0; i < data.size(); i++)
{
if(check[i].compareTo("Update")==0)
{
query = session.createQuery("update Action set Last = :Last ,"
+ " Adjusted_Last =:Adjusted_Last where idAction = :idAction");

query.setDouble("Last", data.get(i).getLast());
query.setDouble("Adjusted_Last", data.get(i).getAdjustLast());
query.setParameter("idAction", data.get(i).getIdAction());
int result = query.executeUpdate();
}
else
{
session.save(data.get(i));
}
}
}
catch (HibernateException e)
{
e.printStackTrace();
session.getTransaction().rollback();
return "ERROR";
}
session.getTransaction().commit();
return "SUCCESS";
}

最佳答案

从Hibernate 5.1开始我们必须以这种方式编写HibernateUtil

公共(public)类 HibernateUtil {

private static final SessionFactory SESSION_FACTORY;

static{
try {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
SESSION_FACTORY = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session openSession() {
return SESSION_FACTORY.openSession();
}

}

关于java - hibernate : Class is not mapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25809656/

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