我在一个POJO类中用到了component-set,然后在控制台打印查询结果。
结果是出来了,但是我对hibernate调用的相关SQL语句不解。
Hibernate: delete from image where product_id=? and filename=? and path=? and image_size=?
Hibernate: delete from image where product_id=? and filename=? and path=? and image_size=?
Hibernate: insert into image (product_id, filename, path, image_size) values (?, ?, ?, ?)
Hibernate: insert into image (product_id, filename, path, image_size) values (?, ?, ?, ?)
一个简单的查询,为什么hibernate会调用delete和insert?
结果是出来了,但是我对hibernate调用的相关SQL语句不解。
Hibernate: delete from image where product_id=? and filename=? and path=? and image_size=?
Hibernate: delete from image where product_id=? and filename=? and path=? and image_size=?
Hibernate: insert into image (product_id, filename, path, image_size) values (?, ?, ?, ?)
Hibernate: insert into image (product_id, filename, path, image_size) values (?, ?, ?, ?)
一个简单的查询,为什么hibernate会调用delete和insert?
解决方案 »
- Sping @RequestParam 如何传递一个对象,在线等候,在这里先谢谢大侠了
- JasperReport报表求助(付费也接受)
- 问一个关于hibernate的问题
- 高手解答:同台服务器两个TomcatWeb程序单点登陆问题
- 如何控制在servlet中的多线程
- 为什么发html的邮件收不到?
- 如何改变tomcat中servlet的虚拟目录?
- 能不能实现远程删除文件或者目录!(例如我在客户端可以删除服务器端的文件,并且我有相应的删除文件的权限
- sybase11.0 与sybase12.0中文处理问题
- 淘宝登陆页面实现自动拖动滑块验证码
- 我们把监听器与下拉菜单项关联起来.方法仍然是使用add,但是我查找API文档,JMenuItem 没有add()方法,怎么办?
- 网站搜索 给个意见!
public void printProduct() {
Session session=SessionFactoryUtil.getSessionFactory().getCurrentSession();
session.beginTransaction(); Product p=(Product)session.load(Product.class, 1);
System.out.println(p.getProductid());
System.out.println(p.getDescription());
System.out.println(p.getName());
System.out.println(p.getCreateTime());
Set<Image> images=p.getImages();
for(Image image:images) {
System.out.println(image.getFilename());
System.out.println(image.getPath());
System.out.println(image.getImageSize());
}
session.getTransaction().commit();
} public static void main(String[] args) {
HibernateTest test = new HibernateTest();
test.printProduct();
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.szh.example.Product" table="product" catalog="test">
<id name="productid" type="java.lang.Integer">
<column name="productid" />
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" precision="22" scale="0" />
</property>
<property name="description" type="java.lang.String">
<column name="description" />
</property>
<property name="createTime" type="java.util.Date">
<column name="create_time" length="19" />
</property>
<set name="images" table="image">
<key column="product_id" foreign-key="product_id"></key>
<composite-element class="com.szh.example.Image">
<property name="filename" column="filename"></property>
<property name="path" column="path"></property>
<property name="imageSize" column="image_size"></property>
</composite-element>
</set>
</class>
</hibernate-mapping>
你把这个去掉
Set<Image> images=p.getImages();
for(Image image:images) {
System.out.println(image.getFilename());
System.out.println(image.getPath());
System.out.println(image.getImageSize());
}
你看看会输出什么
不知道为什么要这么映射 不过在事务提交的时候他更新了某些东西 可能会跟你这个FOR循环有关系吧
http://esffor.javaeye.com/blog/96313
因此就需要为Image类覆写hashCode和equals方法,让hibernate可以按业务正确判断是否重复。
为Product和Image类添加hashCode和equals方法后再查询,
Hibernate就没有调用delete和insert了。
感谢6楼,感谢大家对本问题的关注。
散分!