Has anybody done anything like this an shed light on it? Any input would be appreciated. thanks.
First, 2 classes, and a mapping. The key thing is a many-to-one relationship single ended, from one to the other.
Code:
public class First{
Long ID;
Long Name;
Second second;
}
public class Second{
Long ID;
String code;
Long location;
String other;
}
Code:
<class name="Second" dynamic-update="false" table="SECONDS">
<cache usage="read-write"/>
<id name="ID" column="ID" type="long">
<generator class="native"/>
</id>
<natural-id mutable="false">
<property name="code" column="CODE"/>
<property name="location" column="LOCATION"/>
</natural-id>
<property name="other" column="OTHER"/>
</class>
<class name="First" table="FIRSTS">
<id name="ID" column="ID" type="long">
<generator class="native"/>
</id>
<many-to-one name="second" class="Second" column="SECOND_ID"/>
<property name="name" column="NAME"/>
</class>
Will second level cache support both Primary and Natural/Business Key Lookup. It doesn't appear so.
Also, i notice that when i insert First, it will issue a SQL Insert, then an Update on it to update the FK for Second, even if Second exists (that is you won't even see an insert of Second, though you will see the select). This only happens in this many-to-one relationship, all others in our system sequence it properly. Not sure if it relates to the cache and the business key.
Cache usage is as follows:
We use the naturalID at runtime to do cached lookups, then we store the FK relationship using the primary key along with any containing object.
So when we do a cached natural key query, and it is placed in the second level cache, will it also be in second level by primaryID? If not, is that a restriction?
Like thus:
Code:
Second cached = (Second)hsess.createCriteria(Second.class))
.add( Restrictions.naturalId()
.set("code", “acode”) // discriminator
.set("location", “allocation”) //code
).setCacheable(true).setFlushMode(FlushMode.MANUAL)
.uniqueResult();
So, say I reteive a Second with ID = 2
Will a
hsess.load(Second.class, new Long(2)); // pull this from cache?
Hence will this cache be used when writing out First and resolving the FK?
We have a small number of Seconds, but they are used all over the place, so this is what we need.