Hi,
When I debug a open source project, I found that the underlying hibernate works weirdly: A simple query will cause related tables rows be deleted and re-inserted.
Maybe this behavior is to keep data in memory and database synchronized?
But this behavior will cause performance problem and unexpected result, I think.
Can anyone give some hints to avoid the unnecessary delete/insert?
Thanks,
Xiong
Hibernate version:3.1.3
Mapping documents: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.blojsom.blog.database.DatabaseBlog" table="Blog" lazy="false">
<id name="id"> <column name="id" sql-type="integer" not-null="true" unique="true"/> <generator class="identity"/> </id>
<property name="blogId"> <column name="blog_id" sql-type="string" not-null="true" unique="true"/> </property>
<map name="templates" table="Template" cascade="all" inverse="false" lazy="false"> <key column="blog_id" not-null="true"/> <map-key column="template_flavor" type="string"/> <element column="template_value" type="string"/> </map>
<map name="plugins" table="Plugin" cascade="all" inverse="false" lazy="false"> <key column="blog_id" not-null="true"/> <map-key column="plugin_flavor" type="string"/> <element column="plugin_value" type="string"/> </map>
<map name="properties" table="Properties" cascade="all" inverse="false" lazy="false"> <key column="blog_id" not-null="true"/> <map-key column="property_name" type="string"/> <element column="property_value" type="text"/> </map>
</class>
<sql-query name="blog.id.list"> select blog_id from Blog </sql-query>
</hibernate-mapping>
CREATE TABLE `Plugin` ( `blog_id` int(11) NOT NULL, `plugin_flavor` varchar(50) NOT NULL, `plugin_value` varchar(4096) default NULL, `plugin_id` int(11) NOT NULL auto_increment, PRIMARY KEY(`plugin_id`), KEY `plugin_blog_blogidfk` (`blog_id`), CONSTRAINT `plugin_blog_blogidfk` FOREIGN KEY (`blog_id`) REFERENCES `Blog` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Properties` ( `blog_id` int(11) NOT NULL, `property_name` varchar(255) NOT NULL, `property_value` longtext, `property_id` int(11) NOT NULL auto_increment, PRIMARY KEY(`property_id`), KEY `properties_blog_blogidfk` (`blog_id`), CONSTRAINT `properties_blog_blogidfk` FOREIGN KEY (`blog_id`) REFERENCES `Blog` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Template` ( `blog_id` int(11) NOT NULL, `template_flavor` varchar(50) NOT NULL, `template_value` varchar(255) default NULL, `template_id` int(11) NOT NULL auto_increment, PRIMARY KEY(`template_id`), KEY `template_blog_blogidfk` (`blog_id`), CONSTRAINT `template_blog_blogidfk` FOREIGN KEY (`blog_id`) REFERENCES `Blog` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Code between sessionFactory.openSession() and session.close(): Session session = _sessionFactory.openSession(); Transaction tx = null;
Blog blog = null;
try { tx = session.beginTransaction(); Criteria blogCriteria = session.createCriteria(org.blojsom.blog.database.DatabaseBlog.class); blogCriteria.add(Restrictions.eq("blogId", blogId)); blog = (Blog) blogCriteria.uniqueResult();
tx.commit();
if (blog == null) { throw new FetcherException("Blog id: " + blogId + " not found"); } } catch (HibernateException e) { if (tx != null) { tx.rollback(); }
if (_logger.isErrorEnabled()) { _logger.error(e); }
throw new FetcherException(e); } finally { session.close(); }
Name and version of the database you are using:mysql 5.0.27-community-nt
The generated SQL (show_sql=true): 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - select this_.id a id10_0_, this_.blog_id as blog2_10_0_ from Blog this_ where this_.blog_id=? 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - select properties _.blog_id as blog1_0_, properties0_.property_value as property2_0_, properties0 .property_name as property3_0_ from Properties properties0_ where properties0_. log_id=? 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - select plugins0_. log_id as blog1_0_, plugins0_.plugin_value as plugin2_0_, plugins0_.plugin_flav r as plugin3_0_ from Plugin plugins0_ where plugins0_.blog_id=? 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - select templates0 .blog_id as blog1_0_, templates0_.template_value as template2_0_, templates0_.t mplate_flavor as template3_0_ from Template templates0_ where templates0_.blog_ d=? 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - delete from Templ te where blog_id=? 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - delete from Plugi where blog_id=? 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - insert into Templ te (blog_id, template_flavor, template_value) values (?, ?, ?) ... 五月 10 17:24:37 DEBUG [http-8080-Processor25] hibernate.SQL - insert into Plugi n (blog_id, plugin_flavor, plugin_value) values (?, ?, ?) ...
|