I have the following schema:
Code:
CREATE TABLE system (
system_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE module (
module_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE module_inst (
system_id INTEGER REFERENCES system(system_id),
module_id INTEGER REFERENCES module(module_id),
active BOOLEAN not null
);
I am mapping the system table using the following hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="database.System" table="system" schema="test">
<id name="id" column="system_id"/>
<property name="name"/>
<property name="value"/>
<list name="moduleInstances" inverse="true">
<key column="system_id"/>
<list-index column="module_id"/>
<one-to-many class="database.ModuleInst"/>
</list>
<map name="moduleInstanceMap" >
<key column="system_id"/>
<map-key type="java.lang.String">
<formula>(select m.name from test.module m where m.module_id = module_id)</formula>
</map-key>
<one-to-many class="database.ModuleInst"/>
</map>
</class>
</hibernate-mapping>
The moduleInstanceMap property works well (system.moduleInstanceMap['mod1'] gives me the correct thing) however, if I attempt to do a query using this map:
Code:
q = session.createQuery("from System s where s.moduleInstanceMap[:mname] is not null and s.moduleInstanceMap[:mname].active = :active");
q.setString("mname", "mod1");
q.setBoolean("active",true);
System.out.println(q.list());
then an exception gets thrown because the s.moduleInstanceMap[:mname] elements of the query get mapped to "system0_.system_id=moduleinst1_.system_id and moduleinst1_.null = ?" which is obviously incorrect (there is no "null" column). I would assume this is because I'm using a formula as the <map-key>. Is there a better way to do this? Am I just missing something?
Thanks for the help.
-ds