用于自动更新的mysql中的触发器出错

时间:2014-02-25 06:48:13

标签: java mysql date

My Previous question 的答案是对的。触发器出错

这里我在我的程序中使用它发生以下错误

2014-02-25T11:51:52.737+0530  ERROR  line 1:2: unexpected token: CREATE
2014-02-25T11:51:52.737+0530  ERROR  line 1:2: unexpected token: CREATE
line 1:2: unexpected token: CREATE

这是代码

@SuppressWarnings("unchecked")
public static void expMember(){


Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.setFlushMode(FlushMode.MANUAL);
session.beginTransaction();

java.util.Date utilDate=new java.util.Date();
java.sql.Date sqldate=new java.sql.Date(utilDate.getTime());

List<Member> members = (List<Member>) session.createQuery(                  
        "from Member where exp_status = 'false' ").list();;

for(Member i : members){

Date exp=i.getRenewDate();

try {

    members = (List<Member>) session.createQuery(   
" CREATE  TRIGGER autoupdation_"+ sqldate +" BEFORE UPDATE  ON tbl-member FOR EACH
      ROW BEGIN IF"+ exp .compareTo(sqldate)+" THEN  SET NEW.status = 1;   
      ELSE SET NEW.status = 0; END IF;END;").list();

}

catch(Exception exception){

}
}
}

1 个答案:

答案 0 :(得分:4)

您正在尝试运行本机SQL查询,因此请使用createSQLQuery代替createQuery

另外为什么要在飞行中创建触发器。在循环中创建触发器的逻辑是错误的。您只需要创建一个可以在数据库创建时完成的触发器。

The MySQL Table Editor in MYSQL workbench  provides a work space that has tabs used to perform these actions:

Columns: Add or modify columns

Indexes: Add or modify indexes

Foreign Keys: Add or modify foreign keys

Triggers: Add or modify triggers

Partitioning: Manage partitioning

Options: Add or modify various general, table, and row options

请检查 https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html http://www.mkyong.com/hibernate/hibernate-native-sql-queries-examples/

https://dev.mysql.com/doc/workbench/en/wb-table-editor.html