When forcing a version increment on an entity mapped with joined-subclass, the generated SQL will be incorrect if the subclass has a different ID column than the parent.
Test case:
public class Phone {
protected int id;
protected long version;
protected String name;
public int getId() { return id; }
public long getVersion() { return version; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
public class CellPhone extends Phone { }
<hibernate-mapping package="org.example" default-access="field">
<class name="Phone" table="HT_PHONE">
<id name="id" column="ID">
<generator class="org.hibernate.id.enhanced.SequenceStyleGenerator"/>
</id>
<version type="long" name="version" column="VERSION" />
<property name="name" />
<joined-subclass name="CellPhone" table="HT_CELLPHONE">
<key column="PHONE_ID" />
</joined-subclass>
</class>
</hibernate-mapping>
Configuration cfg = new Configuration()
.addResource("org/example/mapping.xml")
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
.setProperty("hibernate.connection.url", "")
.setProperty("hibernate.connection.username", "")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.hbm2ddl.auto", "create");
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction transaction = session.beginTransaction();
CellPhone phone = new CellPhone();
phone.setName("Droid");
session.save(phone);
session.flush();
session.buildLockRequest(LockOptions.UPGRADE)
.setLockMode(LockMode.PESSIMISTIC_FORCE_INCREMENT)
.lock(phone);
session.flush();
transaction.commit();
session.close();
sf.close();
Result: java.sql.SQLSyntaxErrorException: ORA-00904: "PHONE_ID": invalid identifier
The full stack trace can be found in the attached file. Basically, hibernate tries to use the ID column for the subclass table when building the update statement (PHONE_ID in this case) but since the update is being performed on the root table, the column does not exist and the update fails.
This bug was previously reported as
HHH-5237
but was closed for not including a test case. We have been using the patch attached to that bug successfully in our application.
|