Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.0
Name and version of the database you are using: MySQL 4.1.14 (InnoDB Engine)
Hi, I'm a relatively new hibernate user so it's possible i'm doing
something wrong - sorry for that.
I have several probs with hibernate:
(1) a rollback probem
I need to save objects with unique names and the names should follow a naming rule,
something like fname_[day_of_year]_[i], where [i] is sequental, starting at 1 every new
day. I work in a concurrent environment so I use the following method to store names:
1. I declared the property as an unique key in a table
<property name="fname" unique="true" column="FNAME" />
2. as several process can access the same db at the same time i use the following
technique to ensure uniqueness of the field and to avoid db locks:
tx = null;
i = 1;
while(true) {
try {
tx = new Tx();
obj.fname = getNextName(i++);
session.save(obj);
tx.commit();
break;
} catch(HibernateExcpetion) {
if(tx) {
tx.rollback();
// session.evict(obj);
}
}
}
this code works only for the first object. the problem is that hibernate doesn't
evict object from it's cache/queues when transaction fails (even after a rollback), and next time i try to commit()
it fails, as object(s) with an invalid field value is still there (in the insert queue).
But another problem is that uncommenting the evict() line doesn't help, as after that inalid
object(s) is still in sesson.actionQueue.insertions. Is it so by design and any ideas/suggestions
about how should i implement my case?
(2) polymorphical select problem:
i use table per-subclass method
<?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="controller.dto.SupplierPostDTO" table="SUPPLIERPOSTS">
<id name="id" type="int" column="id">
<generator class="hilo">
<param name="table">supplierposts_dto_hi_value</param>
<param name="column">supplierposts_dto_next_value</param>
<param name="max_lo">100</param>
</generator>
</id>
<discriminator column="POST_TYPE" type="string"/>
<many-to-one name="supplier" lazy="false" class="controller.dto.SupplierDTO" column="SUPPLIER_ID"/>
<set name="instances" lazy="false" table="SUPPLIER_POST_INSTANCES">
<key column="SUPPLIER_POST_ID"/>
<many-to-many column="INSTANCE_ID" class="controller.dto.InstanceDTO"/>
</set>
<subclass name="controller.dto.OttoSupplierPostDTO" discriminator-value="controller.dto.OttoSupplierPostDTO">
<join table="OTTO_SUPPLIERPOSTS">
<key column="id"/>
<property name="filename" unique="true" column="FILENAME" />
<property name="message" column="MESSAGE" />
</join>
</subclass>
</class>
</hibernate-mapping>
and do the following query:
SELECT {s.*} FROM supplierposts s
here's what hibernate generates:
Hibernate: SELECT s.id as id0_, s.SUPPLIER_ID as SUPPLIER3_10_0_, s_1_.FILENAME as FILENAME12_0_, s_1_.MESSAGE as MESSAGE12_0_, s.POST_TYPE as POST2_0_ FROM supplierposts s
of course there's no 's_1_' table/alias defined
(3) you probably noticed lazy = 'false' in object description
this is due to another problem: I need use serialization and when load-on-demand is enabled, it
(java serialization code) serializes those wrappers instead of real referenced objects. In fact I think it's
a java problem but wonder maybe you can comment this.
Great Thanks in advance
Vladimir