Hello,
I dont understand how second-level cache works with lazy=false.
I have a Media class with a many-to-many relation ("tags") to the Tag class :
- Media is second-level cached
- Tag is second-level cached
- the many-to-many relation from media to tag is second-level cached.
- the many-to-many relation from media to tag IS NOT lazy-loaded, but is loaded with lazy=false and fetch=join.
My problem is : I create a media and add tags to it.
Later,when this media is loaded by id, it is found in the second level cache, but the relation to tags is not, resulting in a query. How is this possible?
Thanks in advance.
Hibernate version:
3.2.3.ga
Mapping documents:
Code:
<class name="org.mycompany.Media" table="t_Media" lazy="true" batch-size="20">
<cache usage="read-write"/>
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="reference" type="java.lang.String" column="c_reference"/>
<property name="title" type="java.lang.String" column="c_title"/>
<property name="caption" type="java.lang.String" column="c_caption"/>
<property name="source" type="java.lang.String" column="c_source"/>
<property name="objectName" type="java.lang.String" column="c_objectName"/>
<property name="category" type="java.lang.String" column="c_category"/>
<property name="credit" type="java.lang.String" column="c_credit"/>
<property name="byline" type="java.lang.String" column="c_byline"/>
<property name="city" type="java.lang.String" column="c_city"/>
<property name="country" type="java.lang.String" column="c_country"/>
<property name="width" type="java.lang.Integer" column="c_width"/>
<property name="height" type="java.lang.Integer" column="c_height"/>
<property name="date" type="java.sql.Timestamp" column="c_date"/>
<property name="creationDate" type="java.sql.Timestamp" column="c_creationDate"/>
<property name="specialInstructions" type="java.lang.String"
column="c_specialInstructions"/>
<property name="rating" type="java.lang.Integer" column="c_rating"/>
<many-to-one name="type" class="org.mycompany.MediaType">
<column name="type_id"/>
</many-to-one>
<many-to-one name="representedby" class="org.mycompany.Media">
<column name="representedby_id"/>
</many-to-one>
<many-to-one name="original" class="org.mycompany.File">
<column name="original_id"/>
</many-to-one>
<set name="permissions" inverse="true">
<key>
<column name="media_id"/>
</key>
<one-to-many class="org.mycompany.Permission"/>
</set>
<set name="lightBoxes" inverse="true">
<key>
<column name="media_id"/>
</key>
<one-to-many class="org.mycompany.LightBox"/>
</set>
<set name="files" cascade="all-delete-orphan" lazy="false" fetch="join"
inverse="true">
<key>
<column name="media_id"/>
</key>
<one-to-many class="org.mycompany.File"/>
</set>
<set name="tags" table="MediaTag" lazy="false" fetch="join">
<cache usage="read-write"/>
<key>
<column name="media_id"/>
</key>
<many-to-many column="tag_id" class="org.mycompany.Tag"/>
</set>
<set name="parents" table="MediaHierarchy" lazy="false" fetch="join">
<cache usage="read-write"/>
<key>
<column name="child_id"/>
</key>
<many-to-many column="parent_id" class="org.mycompany.Media"/>
</set>
<set name="authors" table="MediaAuthor">
<cache usage="read-write"/>
<key>
<column name="media_id"/>
</key>
<many-to-many column="author_id" class="org.mycompany.Author"/>
</set>
<set name="stocks" table="MediaStock" lazy="false" fetch="join">
<cache usage="read-write"/>
<key>
<column name="media_id"/>
</key>
<many-to-many column="stock_id" class="org.mycompany.Stock"/>
</set>
<set name="collections" table="MediaCollection" lazy="false" fetch="join">
<cache usage="read-write"/>
<key>
<column name="media_id"/>
</key>
<many-to-many column="collection_id" class="org.mycompany.Collection"/>
</set>
<set name="zones" table="MediaZone">
<cache usage="read-write"/>
<key>
<column name="media_id"/>
</key>
<many-to-many column="zone_id" class="org.mycompany.GeographicalZone"/>
</set>
<set name="children" table="MediaHierarchy">
<key>
<column name="parent_id"/>
</key>
<many-to-many column="child_id" class="org.mycompany.Media"/>
</set>
</class>
</hibernate-mapping>
<class name="org.mycompany.Tag" table="t_Tag" lazy="true">
<cache usage="read-write"/>
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment"/>
</id>
<version name="timestamp" type="timestamp" column="ts"/>
<property name="name" type="java.lang.String" column="c_name"/>
<many-to-one name="type" class="org.mycompany.TagType">
<column name="type_id"/>
</many-to-one>
<set name="medias" table="MediaTag">
<key>
<column name="tag_id"/>
</key>
<many-to-many column="media_id" class="org.mycompany.Media"/>
</set>
</class>
</hibernate-mapping>
Name and version of the database you are using:postgresql 8.2
The generated SQL (show_sql=true):When creating a new media :
Hibernate: insert into t_Media (c_reference, c_title, c_caption, c_source, c_objectName, c_category, c_credit, c_byline, c_city, c_country, c_width, c_height, c_date, c_creationDate, c_specialInstructions, c_rating, type_id, representedby_id, original_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into MediaTag (media_id, tag_id) values (?, ?)
When loading the media by ID then trying to access its tags a query is made. Why?:
Hibernate: select tags0_.media_id as media1_1_, tags0_.tag_id as tag2_1_, tag1_.id as id25_0_, tag1_.ts as ts25_0_, tag1_.c_name as c3_25_0_, tag1_.type_id as type4_25_0_ from MediaTag tags0_ left outer join t_Tag tag1_ on tags0_.tag_id=tag1_.id where tags0_.media_id=?
Thanks for your help,
Antoine
Code:
Code: