0
votes

i will be specific as i can,so i want just ask for litle help with modification of this hbm.xml file

I have this file in my hibernate mappings

<hibernate-mapping>
<class name="sk.bantip.hotel.server.dao.book.Dealer" table="book_dealer">
    <id column="idBook_dealer" name="id" type="java.lang.Long">
        <generator class="identity" />
    </id>
    <many-to-one name="domain" column="idMain_domain"
        not-null="true" class="sk.bantip.hotel.server.dao.main.Domain" />
    <many-to-one name="activeData" column="idBook_dealerData"
        class="sk.bantip.hotel.server.dao.book.DealerData" />
    <set name="historyData" inverse="true">
        <key column="idBook_dealer" />
        <one-to-many class="sk.bantip.hotel.server.dao.book.DealerData" />
    </set>
    <property name="status" not-null="false" />
</class>

and this is associations:

<hibernate-mapping>
<class name="sk.bantip.hotel.server.dao.book.DealerData" table="book_dealerData">
    <id column="idBook_dealerData" name="id" type="java.lang.Long">
        <generator class="identity" />
    </id>
    <property name="name" not-null="true"/>
    <property name="registration" not-null="true"/>
    <property name="taxNumber" not-null="true"/>
    <property name="timestamp" not-null="true" />
    <property name="authorUserId" column="idAuthor" not-null="true" />
    <many-to-one name="dealer" column="idBook_dealer"
        not-null="true" class="sk.bantip.hotel.server.dao.book.Dealer" />
    <property name="channel" not-null="false">
        <type name="sk.bantip.core.enums.GenericEnumUserType">
            <param name="enumClass">sk.bantip.hotel.server.dao.book.Channel</param>
            <param name="identifierMethod">getValue</param>
            <param name="valueOfMethod">getByCode</param>
        </type>
    </property>
    <property name="street" not-null="false" />
    <property name="number" not-null="false" />
    <property name="zip" not-null="false" />
    <property name="city" not-null="false" />
    <property name="country" not-null="false" />
    <property name="telephone" not-null="false" />
    <property name="email" not-null="false" />
</class>

i found some guides which says using cascade operations, when i get

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:

but dont know which use, i know about cascade="all" but i dont know whether will be works in this case, i use this method to save/update:

public void ajaxNameListener(AjaxBehaviorEvent event) {         
     for(DealerListView diler : dealerList) {
      if(diler.getDealerId() == getSelectedDealerId()) {
         diler.setDealerId(selectedDealerId);   
         DealerData dilerko = new DealerData();
         dilerko.setName(selectedDealerName);
         Dealer dealer = BeanFactory
                 .getHotelDAOService(Dealer.class)
                 .findOne(selectedDealerId);    

          dealer.setActiveData(dilerko);
BeanFactory.getHotelDAOService(Dealer.class).update(dealer);// line 220 - in stacktrace is (at sk.bantip.hotel.web.controller.SalesController.ajaxNameListener(SalesController.java:220))       
   }  
    }
}

can somone give me a advice based on this situation? i think that some change must be made in these hbm.xml files,on some cascade operations but not sure about this which will be,Please left me here some post, I will be glad for any help Thank you and have a nice day.

Edit: and in child class DealerData i have a private properties, not sure about that but maybe will be need add this attribute access="field" in DealerData.hbm.xml? because this allow Hibernate to read the value from the private variable instead of the public property.

2

2 Answers

0
votes

Not being a strong advocate of cascades, as the decision you make in your HBM.XML whether to cascade or not has to be consistent through your entire application, plus you not being so sure about which type of cascade you want, I'd rather stick with a simpler approach. Would it be a bad idea in your use case to add something like :

someDealerDataDAO.persist(dilerko);

just before the statement that provokes the error.

0
votes

cascade is basically used in parent-child relationship (e.g. one-to-one, many-to-one, one-to-many, many-to-many associations).

While defining child relationship in Parent class' mapping file, if you specify cascade = "save-update", it will work like the following:

Whenever you save an instance of Parent class, if any child instances are associated with it, they also get saved (if transient) / updated (if already persisted). In this case you won't get the 'TransientObjectException'.

But if you have specified cascade = "refresh", it will work like the following:

Whenever you save a parent class' instance, only that instance will be saved. If any child class' instances are associated with that, they won't be saved/updated.

Having said this, if child class' instances are not persisted to database, it will simply throw 'TransientObjectException'. To avoid this, you have to save the child class' instances manually before saving the parent class' instance.

Have a look at the different cascade options with example here.