Hibernate3使用?占位符:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
//?占位符使用
String sql = "update User t set t.username = ? where t.id = ?";
Query query = session.createQuery(sql);
query.setString(0, "hel");
query.setInteger(1, 22);
int result = query.executeUpdate();
session.getTransaction().commit();
System.out.println("result = " + result);
Hibernate4 占位符:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
//使用占位符
String sql = "update User t set t.username = :newname where t.id = :oldId";
Query query = session.createQuery(sql);
query.setString("newname", "hello");
query.setInteger("oldId", 22);
int result = query.executeUpdate();
session.getTransaction().commit();
System.out.println("result = " + result);