HQL - INSERT data to one table with mysql ,hibernate
HQL - INSERT data to one table with mysql ,hibernate
public static int add_Book(String title, String auth_name, String publisher, String genre) {
Transaction tx = session.beginTransaction();
Query query = session.createQuery
("INSERT INTO Book(title,auth_name,publisher,genre)"
+ "SELECT "+title+", "+auth_name+", "+publisher+", "+genre);
tx.commit();
return query.executeUpdate();
和这个错误
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalStateException: No data type for node:
org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode:
'q' {originalText=q}...
'q'是JTextField数据
插入 'q' 到 'title'
...帮助将不胜感激..
就去做
public static void addBook(String title, String authName, String publisher, String genre) {
session.save(new Book(title, authName, publisher, genre));
}
显然,您应该用 session/transaction 控制代码包装该方法。
而且,请始终使用 Java 命名约定!
auth_name, add_Book
是不正确的名称 — 请改用 authName, addBook
。
我觉得@v.ladynev的回答很好,不过你也可以这样试试:
public static void addBook(String title, String authName, String publisher, String genre) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
// create book Object
Book book = new Book();
book.setTitle(title);
book.setAuthName(authName);
book.setPublisher(publisher);
book.setGenre(genre);
try {
tx = session.beginTransaction();
// Save the book to database
session.save(book);
tx.commit();
}catch(Exception e){
if (tx!=null) {
tx.rollback();}
e.printStackTrace();
}finally{
// close your session
session.close();
}
}
至少使用 Bean class 来编码 "cleanly"。我们只在特殊情况下使用查询,否则我们最多必须在代码中避免它或至少使用捆绑资源。
任何其他检查你的 Book
bean class 你有 setAuthName
而不是 setAuth_Name
。
public static int add_Book(String title, String auth_name, String publisher, String genre) {
Transaction tx = session.beginTransaction();
Query query = session.createQuery
("INSERT INTO Book(title,auth_name,publisher,genre)"
+ "SELECT "+title+", "+auth_name+", "+publisher+", "+genre);
tx.commit();
return query.executeUpdate();
和这个错误
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode +-[IDENT] IdentNode: 'q' {originalText=q}...
'q'是JTextField数据
插入 'q' 到 'title'
...帮助将不胜感激..
就去做
public static void addBook(String title, String authName, String publisher, String genre) {
session.save(new Book(title, authName, publisher, genre));
}
显然,您应该用 session/transaction 控制代码包装该方法。
而且,请始终使用 Java 命名约定!
auth_name, add_Book
是不正确的名称 — 请改用 authName, addBook
。
我觉得@v.ladynev的回答很好,不过你也可以这样试试:
public static void addBook(String title, String authName, String publisher, String genre) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
// create book Object
Book book = new Book();
book.setTitle(title);
book.setAuthName(authName);
book.setPublisher(publisher);
book.setGenre(genre);
try {
tx = session.beginTransaction();
// Save the book to database
session.save(book);
tx.commit();
}catch(Exception e){
if (tx!=null) {
tx.rollback();}
e.printStackTrace();
}finally{
// close your session
session.close();
}
}
至少使用 Bean class 来编码 "cleanly"。我们只在特殊情况下使用查询,否则我们最多必须在代码中避免它或至少使用捆绑资源。
任何其他检查你的 Book
bean class 你有 setAuthName
而不是 setAuth_Name
。