Hibernate - How to persist a new item in a Collection without loading the entire Collection

前端 未结 4 1406
悲哀的现实
悲哀的现实 2020-12-05 14:20

I have a collection in my model that contains a set of \'previous versions\' of my root domain object. The previous versions are therefore \'immutable\' and we will never wa

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 14:55

    The way I typically do this is to define the collection as "inverse".

    That roughly means: the primary definition of the 1-N association is done at the "N" end. Tf you want to add something to the collection you alter the associated object of the detail data.

    A small XML example:

    
        
            
        
        
        
            
            
        
     
    
     
        
        
        
       
    

    then the update is done exclusively in Address:

    Address a = ...;
    a.setPerson(me);
    a.setStreet("abc");
    Session s = ...;
    s.save(a);
    

    Done. You did not even touch the collection. Consider it read-only, which may be very practical for querying with HQL, and iterating and displaying it.

提交回复
热议问题