I've been trying to figure this out for hours now. Everything is there and it seems like it should work, but for some reason it is just not. Can someone please help me? I'll keep this as brief as possible. Here is my mapping:
Code:
<hibernate-mapping auto-import="true" default-lazy="true">
<class name="package.domain.pojo.request.server.ServerInRequest" table="server_in_request">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="hostname" column="hostname"/>
<property name="ip" column="ip"/>
<property name="usernamePassword" column="username_password"/>
<property name="location" column="location"/>
<joined-subclass name="package.domain.pojo.request.server.ServerForAppInstall" table="app_request_server">
<key column="server_in_request_id"/>
<set name="applicationInfos" cascade="all">
<key column="app_request_server_id" not-null="true"/>
<one-to-many class="package.domain.pojo.request.server.ApplicationInformation"/>
</set>
</joined-subclass>
<class name="package.domain.pojo.request.server.ApplicationInformation" table="app_request_server_app">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="applicationName" column="name"/>
<property name="applicationVersion" column="version"/>
<property name="applicationServicePackLevel" column="service_pack_level"/>
</class>
</hibernate-mapping>
When I do this straightforward code (shortened for brevity), it gets an error:
Code:
ServerForAppInstall s1 = new ServerForAppInstall();
...
ApplicationInformation info = new ApplicationInformation();
...
s1.getApplicationInfos().add(info);
dao.save(s1);
This is what Hibernate tells me from the debug:
Code:
Hibernate: insert into server_in_request (hostname, ip, username_password, location) values (?, ?, ?, ?)
Hibernate: insert into app_request_server (app_request_id, server_in_request_id) values (?, ?)
Hibernate: insert into app_request_server_app (name, version, service_pack_level, app_request_server_id) values (?, ?, ?, ?)
- SQL Error: 1216, SQLState: 23000
- Cannot add or update a child row: a foreign key constraint fails
- Rolled back transaction after test execution
Although the second INSERT was successful, the third one fails because
app_request_server_id is a bad value. Although it shouldn't because it should be the generated primary key of the second INSERT. I've been trying to figure out what value Hibernate is trying to insert, but I haven't been able to figure that out either.
Also, as far the database is concerned, the app_request_server_app.app_request_server_id REFERENCES the app_request_server.id. Yet, it still INSERTs the wrong value. Does anyone see anything wrong? Thank you. I really appreciate this.