{
if (entity == null)
{
return null;
}
EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(pd.getKunderaMetadata(),
entity.getClass());
// entity metadata could be null.
if (entityMetadata == null)
{
throw new IllegalArgumentException(
"Entity object is invalid, operation failed. Please check previous log message for details");
}
Object id = PropertyAccessorHelper.getId(entity, entityMetadata);
// Generate and set Id if @GeneratedValue present.
if (((Field) entityMetadata.getIdAttribute().getJavaMember()).isAnnotationPresent(GeneratedValue.class))
{
if (!isIdSet(id))
{
id = idGenerator.generateAndSetId(entity, entityMetadata, pd, pd.getKunderaMetadata());
}
}
if (!validator.isValidEntityObject(entity, entityMetadata))
{
throw new IllegalArgumentException(
"Entity object is invalid, operation failed. Please check previous log message for details");
}
// id = PropertyAccessorHelper.getId(entity, entityMetadata);
String nodeId = ObjectGraphUtils.getNodeId(id, entity.getClass());
Node node = graph.getNode(nodeId);
// If this node is already there in graph (may happen for bidirectional
// relationship, do nothing and return null)
if (node != null)
{
if (node.isGraphCompleted())
{
return node;
}
return null;
}
// Construct this Node first, if one not already there in Persistence
// Cache
Node nodeInPersistenceCache = persistenceCache.getMainCache().getNodeFromCache(nodeId, pd);
// Make a deep copy of entity data
if (nodeInPersistenceCache == null)
{
node = new Node(nodeId, entity, initialNodeState, persistenceCache, id, pd);
}
else
{
node = nodeInPersistenceCache;
node.setPersistenceCache(persistenceCache);
// Determine whether this node is dirty based on comparison between
// Node data and entity data
// If dirty, set the entity data into node and mark it as dirty
if (!DeepEquals.deepEquals(node.getData(), entity))
{
node.setDirty(true);
}
else if (node.isProcessed())
{
node.setDirty(false);
}
node.setData(entity);
// If node is NOT in managed state, its data needs to be
// replaced with the one provided in entity object
}
// Put this node into object graph
graph.addNode(nodeId, node);
// Iterate over relations and construct children nodes
for (Relation relation : entityMetadata.getRelations())
{
if (relation != null)
{
// Child Object set in this entity
Object childObject = PropertyAccessorHelper.getObject(entity, relation.getProperty());
if (childObject != null && !ProxyHelper.isProxy(childObject))
{
EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(pd.getKunderaMetadata(),
PropertyAccessorHelper.getGenericClass(relation.getProperty()));
if (metadata != null && relation.isJoinedByPrimaryKey())
{
PropertyAccessorHelper.setId(childObject, metadata,