Hello All, I have a problem. I have a Customer with a Bank_Address that inherites from Address Class. When I create a new customer and new bank_address transient objects.
Code:
Customer customer = new Customer("2", ...);
customer.setBank_Address(new Bank_Address(...));
and save them calling save method of session as follow:
Code:
session.save(customer);
session.Flush();
This code crashes because hibernate insert customer, but later, it try update a bank_Address, as if it already was a persisted object.
SQL LOG-->
Quote:
Hibernate: INSERT INTO CUSTOMER (FIRST_NAME, SECOND_NAME, BANK_ADDRESS, ID) VALUES (@p0, @p1, @p2, @p3); @p0 = 'Jordi', @p1 = 'Cabré Roca', @p2 = '2', @p3 = '2'
Hibernate: UPDATE ADDRESS SET STREET = @p0, NUMBER_STREET = @p1, CITY = @p2, STATE = @p3 WHERE ID = @p4; @p0 = 'La Parra', @p1 = '15', @p2 = 'Riudecols', @p3 = 'Tarragona', @p4 = '2'
<-------> In this point program crashes, becaus in database this bank_address doesn't exist.
if code doesn't crashes the follow sentence is UPDATE BANK_ADDRESS ...
the correct SQL sentences should be:
INSERT INTO CUSTOMER(...)
INSERT INTO ADDRESS(...)
instead of UPDATE ADDRESS SET(...)INSERT INTO BANK_ADDRESS(...)
What Do I do wrong?
thanks in advanced.
Mapping files -->
Code:
<class name="Customer" table="CUSTOMER">
<id name="Id" column="ID" type="String">
<generator class="assigned" />
</id>
...
<many-to-one name="Bank_address" class="Model.BankAddress, ComponentsBroker" unique="true" cascade="all" column="BANK_ADDRESS" />
...
</class>
<class name="Address" table="ADDRESS">
<id name="Id" column="ID" type="String">
<generator class="assigned" />
</id>
<property name="Street" type="String" column="STREET"/>
<property name="Number_street" type="String" column="NUMBER_STREET"/>
<property name="City" type="String" column="CITY"/>
<property name="State" type="String" column="STATE"/>
<joined-subclass name="BankAddress" table="BANK_ADDRESS">
<key column="ADDRESS_ID"/>
<property name="BankName" type="String" column="BANK_NAME"/>
<property name="Account" type="String" column="ACCOUNT"/>
</joined-subclass>
<joined-subclass name="Model.ContactAddress, ComponentsBroker" table="CONTACT_ADDRESS">
<key column="ADDRESS_ID"/>
<component class="Name" name="ContactName">
<property name="First_name" column="FIRST_NAME" type="String"/>
<property name="Second_name" column="SECOND_NAME" type="String"/>
</component>
<property name="Phone" type="String" column="PHONE"/>
</joined-subclass>
</class>