Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2009-2014 Pavel Ponec
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.ujorm.implementation.orm;
 
 import java.util.List;
 import org.ujorm.Ujo;
 import static org.ujorm.extensions.PropertyModifier.*;
 import static org.ujorm.orm.ao.LazyLoading.*;

The relation 1:N to another UJO type items

Author(s):
Pavel Ponec
See also:
org.ujorm.core.UjoIterator
 
 public class RelationToMany<UJO extends ExtendedOrmUjo, ITEM extends ExtendedOrmUjo>
         extends AbstractCollectionProperty<UJO, UjoIterator<ITEM>, ITEM> {
    
Logger
 
     private static final UjoLogger LOGGER = UjoLoggerFactory.getLogger(RelationToMany.class);

    
Constructor

Parameters:
name optional
itemType optional
 
     public RelationToMany(String name) {
         this(namenullfalse);
     }

    
Constructor

Parameters:
name optional
itemType optional
 
     public RelationToMany(String nameClass<ITEM> itemType) {
         this(nameitemTypefalse);
     }

    
Constructor

Parameters:
name Property name.
itemType The type of item (optional)
index An key order (optional)
 
     @SuppressWarnings("unchecked")
     public RelationToMany(String nameClass<ITEM> itemTypeint indexboolean lock) {
         super((Class<UjoIterator<ITEM>>) (ClassUjoIterator.class);
         initItemType(itemType);
         init(index);
         init(name);
         init(lock);
     }

    
An alias for the method of(Ujo). The null value is not replaced for a default one.

 
     @SuppressWarnings("unchecked")
     @Override
     public UjoIterator<ITEM> of(final UJO ujo) {
         final Session mySession = ujo.readSession();  // maybe readSession() is better?
 
         if (mySession != null
         &&  mySession.getHandler().isPersistent(this)) {
 
             if (.equalsTo(mySession.getLazyLoading())) {
                 throw new IllegalStateException("The lazy loading is disabled in the current Session.");
             }
 
             if (mySession.isClosed()) {
                 IllegalStateException e = null;
                 switch (mySession.getLazyLoading()) {
                     default:
                         throw new IllegalStateException("The lazy loading is disabled in the closed Session.");
                     case :
                         if (.isLoggable(.)) {
                             e = new IllegalStateException(mySession.getLazyLoading().name());
                         }
                     case :
                         if (.isLoggable(.)) {
                             .log(."The lazy loading on closed session on the key " + toStringFull(), e);
                         }
                     case :
                        // open temporary session if it's closed ;) - because of lazy-loading of detached objects (caches, etc.)
                        final Session tempSession = mySession.getHandler().createSession();
                        try {
                            List<ITEM> list = (ListtempSession.iterateInternal((RelationToManythisujo).toList();
                            return UjoIterator.of(list);
                        } finally {
                            tempSession.close();
                        }
                }
            } else {
                return mySession.iterateInternal((RelationToManythisujo);
            }
            // Don't save the result!
        } else {
            return (UjoIterator<ITEM>) ujo.readValue(this);
        }
    }
New to GrepCode? Check out our FAQ X