Announcement Announcement Module
Collapse
No announcement yet.
Hibernate issue with one-to-one mapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate issue with one-to-one mapping

    Hi Everyone,

    I was hoping someone could help me with a problem we have with hibernate, whereas the delete-orphan does not appear to working correctly. I currently have two objects with the following mapping files

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping default-lazy="false">
    	<class name="com.mycompany.project.mqmessage.MqMessage" table="pending_mqmessages">
    
    		<id type="long" unsaved-value="null" column="id" name="id">
    			<generator class="native" />
    		</id>
    
    		<property name="processed" column="processed" type="boolean" not-null="true"/>
            
    		<property name="status" column="status" not-null="true" insert="true" update="true">
    			<type name="com.mycompany.common.persistence.hibernate.StringEnumUserType">
    				<param name="enumClassName">com.mycompany.project.poc.BookingStatus</param>
    			</type>
    		</property>
            
    		<property name="incidentType" column="claim_type" type="string" />
    		<property name="policyNumber" column="policy_number" type="string" not-null="true" unique-key="UniqueClaimKey"/>
    		<property name="claimReferenceNumber" column="refnum" type="string" unique-key="UniqueClaimKey"/>
    
            <one-to-one name="bookingDetails" cascade="all-delete-orphan" property-ref="mqMessage"/>
    	</class>
    </hibernate-mapping>
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping default-lazy="false">
    	<class name="com.mycompany.project.claims.BookingDetails" table="claim_details">
    		<id type="long" unsaved-value="null" column="id" name="id">
    			<generator class="native" />
    		</id>
    		
    		<many-to-one name="mqMessage" column="mqmessage_id" class="com.mycompany.project.mqmessage.MqMessage" cascade="all" unique-key="uniqueClaimKey" unique="true" not-null="false"/>
    		<many-to-one name="claim" column="claim_id" class="com.mycompany.project.claims.Claim"  cascade="all" unique-key="uniqueClaimKey" unique="true" not-null="false"/>
    
    			<property name="dateOfIncident" column="date_of_incident" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
    			<property name="timeOfIncident" column="time_of_incident" />
    
    			<property name="locationOfIncident" column="location_of_incident" />
    			<property name="description" column="description" type="string" />
    			
    			<property name="policeOfficerInvolved" column="officer_involved" type="boolean" />
    			<property name="policeOfficerName" column="officer_name" type="string" />
    			<property name="policeOfficerNumber" column="officer_number" type="string" />
    			<property name="policeOfficerStation" column="officer_station" type="string" />
    			<property name="crimeReferenceNumber" column="crime_reference" type="string" />
    
    			<!-- Insured Vehicle -->
    			<property name="registrationNumber" column="registration" type="string" />
    			<property name="make" column="make" type="string" />
    			<property name="model" column="model" type="string" />
    			<property name="yearOfManufacture" column="year_of_manufacture" type="string" />
    			<property name="purpose" column="purpose" type="string" />
    			<property name="pointOfInitialImpact" column="impact_point" type="string" />
    
    			<property name="registeredOwner" column="is_registered_owner" type="boolean" />
    			<property name="registeredKeeper" column="registered_keeper" type="string" />
    			<property name="isAnyoneInjured" column="anyone_injured" type="boolean" />
    			<property name="anyoneInjured" column="is_anyone_injured" type="string" />
    
    			<property name="anyVechiclesInvolved" column="other_vehicle_count" type="string" />
    			<bag name="vehiclesInvolved" table="other_vehicle" cascade="all">
    				<key column="claim_details_id" />
    				<composite-element class="com.mycompany.project.claims.ClaimVehicleInvolved">
    					<property column="registration_number" name="registrationNumber" not-null="false" />
    					<property column="make" name="make" not-null="false" />
    					<property column="model" name="model" not-null="false" />
    					<property column="year_of_manufacture" name="yearOfManufacture" not-null="false" />
    					<property column="initial_impact" name="initialImpact" not-null="false" />
    					<property column="damage_details" name="damageDetails" not-null="false" />
    					<property column="third_party_contact" name="thirdPartyContactDetails" not-null="false" />
    					<property column="third_party_insurance_contact" name="thirdPartyInsurerDetails" not-null="false" />
    					<property column="number_of_occupants" name="numberOfOccupants" not-null="false" />
    					<property column="any_injury" name="anyoneInjured" type="yes_no" not-null="false" />	
    					<property column="injury_details" name="injuryDetails" not-null="false" />
    				</composite-element>
    			</bag>
    
    			<!-- Details of any other property involved -->
    			<property name="propertyOwnerDetails" column="property_owner_details" type="string" />
    			<property name="propertyDescription" column="property_description" type="string" />
    			<property name="propertyApparentDamage" column="property_damage" type="string" />
    
    			<!-- Person driving or person last in charge of the insured vehicle -->
    			<property name="firstName" column="first_name" type="string" />
    			<property name="surname" column="surname" type="string" />
    			<property name="dateOfBirth" column="dob" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
    			<property name="occupation" column="occupation" type="string" />
    			<property name="address" column="address" type="string" />
    			<property name="typeOfLicense" column="license_type" type="string" />
    			<property name="yearsLicenseHeld" column="years_license_held" type="string" />
    			
    			<property name="isAllowLiability" column="is_allow_liability" type="boolean" />
    			<property name="allowLiability" column="allow_liability" type="string" />
    
    			<property name="anyWitnesses" column="any_witnesses" type="boolean" />
    			<bag name="witnesses" table="witness" cascade="all">
    				<key column="claim_details_id" />
    				<composite-element class="com.mycompany.project.claims.ClaimWitness">
    					<property column="name" name="name" not-null="false" />
    					<property column="address" name="address" not-null="false" />
    					<property column="phone" name="phone" not-null="false" />
    					<property column="email" name="email" not-null="false" />
    				</composite-element>
    			</bag>
    
    			<property name="driverMainUser" column="is_main_user" type="boolean" />
    			<property name="motoringClaimHistory" column="claim_history" type="string" />
    			<property name="motoringConvictionExcludingParkingOffense" column="convictions" type="string" />
    			<property name="driverSuspensionHistory" column="driving_suspension" type="string" />
    			<property name="driverMedicalCondition" column="medical_conditions" type="string" />
    			<property name="vehicleLastKnownLocation" column="vehicle_last_location" type="string" />
    			<property name="lastSeenBy" column="vehicle_last_seen_by" type="string" />
    			<property name="incidentDiscoveryTime" column="incident_discovery_time" />
    			<property name="wasVehicleSecured" column="vehicle_secured" type="boolean" />
    			<property name="vehicleRecoveryDetail" column="vehicle_recovery_details" type="string" />
    			<property name="damageDetails" column="damage_details" type="string" />
    			<property name="vehicleDriveable" column="is_vehicle_driveable" type="string" />
    			<property name="vehicleCurrentLocation" column="current_vehicle_location" type="string" />
    			<property name="thiefIdentityKnown" column="thief_identity_known" type="boolean" />
    			<property name="anyoneApprehended" column="anyone_apprehended" type="boolean" />
    
    			<property name="anyAccidentInLast5Years" column="any_accidents_5yr" type="boolean" />
    			<property name="anyConvictionInLast5Years" column="any_convictions_5yr" type="boolean" />
    			<property name="anyDisqualification" column="any_disqualification" type="boolean" />
    			<property name="anyMedicalCondition" column="any_medical_condition" type="boolean" />
    
    			<!--Other Person(s) Involved -->
    			<property name="anyoneElseInjured" column="anyone_else_injured" type="boolean" />
    			<property name="injuredPersonDetails" column="injury_details" type="string" />
    
    			<property name="othersInvolvedRoleCyclist" column="role_cyclist" type="boolean" />
    			<property name="othersInvolvedRolePedestrian" column="role_pedestrian" type="boolean" />
    
    			<!-- <array name="othersInvolvedRole" cascade="all,delete-orphan" table="other_role"> 
    				<key column="claim_id"/> <index column="id"/> <element column="roles_involved" 
    				type="string" /> </array> -->
    
    			<!--Description of any articles stolen and price paid -->
    
    			<property name="anyItemStolen" column="is_stolen_item" type="boolean" />
    			<bag name="itemStolen" table="stolen_item" cascade="all">
    				<key column="claim_details_id" />
    				<composite-element class="com.mycompany.project.claims.ClaimStolenItem">
    					<property column="item_stolen" name="itemStolen" not-null="false" />
    					<property column="price_paid" name="pricePaid" not-null="false" />
    				</composite-element>
    			</bag>
    	</class>
    </hibernate-mapping>
    The probem I have is that upon receiving a MqMessage (which is a object to update BookingDetails), hibernate is adding a new row in the BookingDetails table

    Code:
     
    mqMessageDao.makePersistent(mqMessage);
    However what I want is for hibernate to update the existing row instead? Can anyone point me in the right direction?

    I dont understand why the MqMessage has a one-to-one mapping to BookingDetails and BookingDetails then has a many-to-one back to MQMessage?
Working...
X