hibernate
在hibernate中,可以在数据库中增加和查询数据,但是无法删除和修改数据。更奇怪的是不报错,但是就是不能在数据库中看到删除了或修改了数据。
这是删除的代码:
public class DeleteProduct {
//删除指定产品信息
public static void main(String[] args) {
Session session = null; //声明Session对象
try {
//Hibernate的持久化操作
session = HibernateInitialize.getSession();//获取Session
Product product = (Product) session.get(Product.class, new Integer("1"));//装载对象
session.delete(product);//删除持久化信息
session.flush();//强制刷新提交
} catch (Exception e) {
System.out.println("对象删除失败");
e.printStackTrace();
} finally{
HibernateInitialize.closeSession();//关闭Session
}
}
}
控制台显示:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Sun Oct 16 14:20:28 GMT+08:00 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate:
select
product0_.id as id0_0_,
product0_.name as name0_0_,
product0_.price as price0_0_,
product0_.factory as factory0_0_,
product0_.remark as remark0_0_
from
tab_product product0_
where
product0_.id=?
Hibernate:
delete
from
tab_product
where
id=?
这是修改的代码:
public class UpdateProduct {
//修改指定产品信息
public static void main(String[] args) {
Session session = null; //声明Session对象
try {
//Hibernate的持久化操作
session = HibernateInitialize.getSession();//获取Session
Product product = (Product) session.load(Product.class, new Integer("10"));//装载对象
product.setName("Java Web编程词典");//修改商品名称
product.setRemark("无");//修改备注信息
session.update(product );
session.flush();
} catch (Exception e) {
System.out.println("对象修改失败");
e.printStackTrace();
} finally{
HibernateInitialize.closeSession();//关闭Session
}
}
}
控制台显示:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Sun Oct 16 14:20:56 GMT+08:00 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate:
select
product0_.id as id0_0_,
product0_.name as name0_0_,
product0_.price as price0_0_,
product0_.factory as factory0_0_,
product0_.remark as remark0_0_
from
tab_product product0_
where
product0_.id=?
Hibernate:
update
tab_product
set
name=?,
price=?,
factory=?,
remark=?
where
id=?