Hibernate抛出一个奇怪的错误
Hibernate:选择user0_.userID作为userID0_,user0_.password as 来自用户user0_的password0_,其中user0_.userID =' admin'和 user0_.password ='管理' org.hibernate.hql.ast.QuerySyntaxException: 动作未映射[来自id =' MADEX240214']的动作 org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) 在 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";
}
答案 0 :(得分:1)
从Hibernate 5.1开始,我们必须以这种方式编写HibernateUtil
public class 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();
}
}
答案 1 :(得分:0)
什么是完整的堆栈跟踪?它似乎不是来自该部分代码。错误是从“操作”中选择,而不是更新。
我注意到在您的更新语句中,您将表引用为“Action”。 HQL查询区分大小写。检查你的映射。 'action'!='行动'