Skip to content

Commit

Permalink
Fixes for Issue Impetus#293.
Browse files Browse the repository at this point in the history
Changes made:
1. Fixes test cases for lazy/eager fetching.
2. Added fix for proxy check over java assist instances.
3. Modified Schema configuration.java for export schema only after table infos for all persistence units are loaded.
4. Corrected javassist maven dependency.
  • Loading branch information
mevivs committed Jun 22, 2013
1 parent 0bde08c commit 1be805a
Show file tree
Hide file tree
Showing 37 changed files with 91 additions and 74 deletions.
2 changes: 1 addition & 1 deletion kundera-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.9.0.GA</version>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.impetus.kundera.Constants;
import com.impetus.kundera.PersistenceProperties;
import com.impetus.kundera.client.ClientResolver;
import com.impetus.kundera.configure.schema.ColumnInfo;
Expand Down Expand Up @@ -105,7 +106,10 @@ public void configure()
List<TableInfo> tableInfos = getSchemaInfo(persistenceUnit);

Map<String, EntityMetadata> entityMetadataMap = getEntityMetadataCol(appMetadata, persistenceUnit);

PersistenceUnitMetadata puMetadata = appMetadata.getPersistenceUnitMetadata(persistenceUnit);

if(puMetadata.getProperty(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE) != null)
// Iterate each entity metadata.
for (EntityMetadata entityMetadata : entityMetadataMap.values())
{
Expand Down Expand Up @@ -136,6 +140,7 @@ public void configure()
}

List<Relation> relations = entityMetadata.getRelations();

parseRelations(persistenceUnit, tableInfos, entityMetadata, tableInfo, relations);

if (!found)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void handleFind(NodeStateContext nodeStateContext)
{
// This entity has associated entities, find them recursively.
nodeData = reader.recursivelyFindEntities(ee.getEntity(), ee.getRelations(), entityMetadata,
nodeStateContext.getPersistenceDelegator());
nodeStateContext.getPersistenceDelegator(),false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client cli
* @return
*/
public Object handleAssociation(final Object entity, final Map<String, Object> relationsMap,
final EntityMetadata m, final PersistenceDelegator pd)
final EntityMetadata m, final PersistenceDelegator pd, boolean lazilyloaded)
{

for (Relation relation : m.getRelations())
Expand All @@ -107,22 +107,21 @@ public Object handleAssociation(final Object entity, final Map<String, Object> r
Object relationalObject = PropertyAccessorHelper.getObject(entity, relation.getProperty());

// TODO: Need to check if object is a collection instance but empty!
if (relationalObject == null || ProxyHelper.isHibernateProxy(relationalObject)
|| ProxyHelper.isPersistentCollection(relationalObject))
if (relationalObject == null || ProxyHelper.isProxyOrCollection(relationalObject))
{
onRelation(entity, relationsMap, m, pd, relation, relationType);
onRelation(entity, relationsMap, m, pd, relation, relationType,lazilyloaded);
}
}
return entity;
}

private void onRelation(final Object entity, final Map<String, Object> relationsMap, final EntityMetadata m,
final PersistenceDelegator pd, Relation relation, ForeignKey relationType)
final PersistenceDelegator pd, Relation relation, ForeignKey relationType,boolean lazilyloaded)
{

FetchType fetchType = relation.getFetchType();

if (fetchType.equals(FetchType.LAZY))
if (!lazilyloaded && fetchType.equals(FetchType.LAZY))
{
final Object entityId = PropertyAccessorHelper.getId(entity, m);
associationBuilder.setProxyRelationObject(entity, relationsMap, m, pd, entityId, relation);
Expand All @@ -140,7 +139,7 @@ private void onRelation(final Object entity, final Map<String, Object> relations
}
else
{
onRelation(entity, relationsMap, relation, m, pd);
onRelation(entity, relationsMap, relation, m, pd,lazilyloaded);
}
}
}
Expand All @@ -158,7 +157,7 @@ private void onRelation(final Object entity, final Map<String, Object> relations
* entity metadata.
*/
private void onRelation(Object entity, Map<String, Object> relationsMap, final Relation relation,
final EntityMetadata metadata, final PersistenceDelegator pd)
final EntityMetadata metadata, final PersistenceDelegator pd, boolean lazilyloaded)
{
final Object entityId = PropertyAccessorHelper.getId(entity, metadata);

Expand All @@ -179,17 +178,17 @@ private void onRelation(Object entity, Map<String, Object> relationsMap, final R
{
for (Object relationEntity : relationalEntities)
{
onParseRelation(entity, pd, targetEntityMetadata, relationEntity, relation);
onParseRelation(entity, pd, targetEntityMetadata, relationEntity, relation,lazilyloaded);
}
}

}

private void onParseRelation(Object entity, final PersistenceDelegator pd, EntityMetadata targetEntityMetadata,
Object relationEntity, Relation relation)
Object relationEntity, Relation relation,boolean lazilyloaded)
{
parseRelations(entity, getEntity(relationEntity), getPersistedRelations(relationEntity), pd,
targetEntityMetadata);
targetEntityMetadata,lazilyloaded);

// if relation ship is unary, no problem else we need to add
setRelationToEntity(entity, relationEntity, relation);
Expand All @@ -207,8 +206,7 @@ private void setRelationToEntity(Object entity, Object relationEntity, Relation
else
{
Object associationObject = PropertyAccessorHelper.getObject(entity, relation.getProperty());
if (associationObject == null || ProxyHelper.isHibernateProxy(associationObject)
|| ProxyHelper.isPersistentCollection(associationObject))
if (associationObject == null || ProxyHelper.isProxyOrCollection(associationObject))
{
associationObject = PropertyAccessorHelper.getCollectionInstance(relation.getProperty());
PropertyAccessorHelper.set(entity, relation.getProperty(), associationObject);
Expand All @@ -220,15 +218,15 @@ private void setRelationToEntity(Object entity, Object relationEntity, Relation
}

private void parseRelations(final Object originalEntity, final Object relationEntity,
final Map<String, Object> relationsMap, final PersistenceDelegator pd, final EntityMetadata metadata)
final Map<String, Object> relationsMap, final PersistenceDelegator pd, final EntityMetadata metadata, boolean lazilyloaded)
{

for (Relation relation : metadata.getRelations())
{

FetchType fetchType = relation.getFetchType();

if (fetchType.equals(FetchType.LAZY))
if (!lazilyloaded && fetchType.equals(FetchType.LAZY))
{
final Object entityId = PropertyAccessorHelper.getId(relationEntity, metadata);
associationBuilder.setProxyRelationObject(relationEntity, relationsMap, metadata, pd, entityId,
Expand All @@ -244,8 +242,7 @@ private void parseRelations(final Object originalEntity, final Object relationEn

Object associationObject = PropertyAccessorHelper.getObject(relationEntity, relation.getProperty());
if (relation.getType().equals(ForeignKey.ONE_TO_ONE)
|| ((associationObject == null || ProxyHelper.isHibernateProxy(associationObject)) || ProxyHelper
.isPersistentCollection(associationObject)))
|| ((associationObject == null || ProxyHelper.isProxyOrCollection(associationObject))))
{
PropertyAccessorHelper.set(relationEntity, relation.getProperty(), originalEntity);
}
Expand Down Expand Up @@ -281,7 +278,7 @@ else if (relationsMap != null && relationsMap.containsKey(relation.getJoinColumn
// System.out.println("Here");
if (!compareTo(getEntity(immediateRelation), originalEntity))
{
onParseRelation(relationEntity, pd, targetEntityMetadata, immediateRelation, relation);
onParseRelation(relationEntity, pd, targetEntityMetadata, immediateRelation, relation,lazilyloaded);
}
}

Expand Down Expand Up @@ -334,6 +331,8 @@ else if (!relation.isUnary())
return relationalEntities;
}



/**
* Recursively fetches associated entities for a given <code>entity</code>
*
Expand All @@ -345,10 +344,10 @@ else if (!relation.isUnary())
* @return
*/
public Object recursivelyFindEntities(Object entity, Map<String, Object> relationsMap, EntityMetadata m,
PersistenceDelegator pd)
PersistenceDelegator pd, boolean lazilyLoaded)
{
associationBuilder = new AssociationBuilder();
return handleAssociation(entity, relationsMap, m, pd);
return handleAssociation(entity, relationsMap, m, pd,lazilyLoaded);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ else if (traversalRequired && associatedEntities != null)
for (Object associatedEntity : associatedEntities)
{
associatedEntity = childClient.getReader().recursivelyFindEntities(associatedEntity, null,
childMetadata, pd);
childMetadata, pd,false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public interface EntityReader
* entity meta data
* @param persistenceDelegeator
* persistence delegator.
* @param lazily loaded. true if invoked over lazily fetched object.
* @return populate entity.
* @throws Exception
* the exception
*/

Object recursivelyFindEntities(Object entity, Map<String, Object> relationsMap, EntityMetadata m,
PersistenceDelegator pd);
PersistenceDelegator pd, boolean lazilyLoaded);

/**
* Find by id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.ArrayList;
import java.util.List;

import javassist.util.proxy.ProxyFactory;

import com.impetus.kundera.proxy.collection.ProxyCollection;

/**
Expand Down Expand Up @@ -66,7 +68,7 @@ public static boolean isPersistentCollection(Object collection) {

public static boolean isProxy(Object o)
{
return isKunderaProxy(o) || isHibernateProxy(o);
return isKunderaProxy(o) || isHibernateProxy(o) || ProxyFactory.isProxyClass(o.getClass());
}

public static boolean isProxyCollection(Object o)
Expand All @@ -76,6 +78,6 @@ public static boolean isProxyCollection(Object o)

public static boolean isProxyOrCollection(Object o)
{
return isProxy(o) || isProxyCollection(o);
return isProxy(o) || isProxyCollection(o) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ public Relation getRelation() {

@Override
public Collection getDataCollection() {
return dataCollection;
return dataCollection != null && !dataCollection.isEmpty()?dataCollection:null;
}

/**
*
*/
protected void eagerlyLoadDataCollection()
{
if (dataCollection == null)
if (getDataCollection() == null)
{
EntityMetadata m = KunderaMetadataManager.getEntityMetadata(getOwner().getClass());
Object entityId = PropertyAccessorHelper.getId(getOwner(), m);
// Object entityId = PropertyAccessorHelper.getId(getOwner(), m);

// getPersistenceDelegator().getClient(m).getReader().recursivelyFindEntities(getOwner(), relationsMap, m, pd)
new AssociationBuilder().setConcreteRelationObject(getOwner(), getRelationsMap(), m,
getPersistenceDelegator(), entityId, getRelation());
getPersistenceDelegator().getClient(m).getReader().recursivelyFindEntities(getOwner(), relationsMap, m, getPersistenceDelegator(),true);
// new AssociationBuilder().setConcreteRelationObject(getOwner(), getRelationsMap(), m,
// getPersistenceDelegator(), entityId, getRelation());

dataCollection = (Collection) PropertyAccessorHelper.getObject(getOwner(), getRelation().getProperty());
PropertyAccessorHelper.set(getOwner(), getRelation().getProperty(), dataCollection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected List<Object> setRelationEntities(List enhanceEntities, Client client,
EnhanceEntity ee = (EnhanceEntity) e;

result.add(getReader().recursivelyFindEntities(ee.getEntity(), ee.getRelations(), m,
persistenceDelegeator));
persistenceDelegeator,false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void shutdown() throws SQLException
// db writes out to files and performs clean shuts down
// otherwise there will be an unclean shutdown
// when program ends
st.execute("SHUTDOWN");
st.execute("clean SHUTDOWN");
closeConnection();
}

Expand Down Expand Up @@ -172,6 +172,7 @@ public void createSchema(final String schemaName) throws SQLException
{
String sql = "CREATE schema " + schemaName + " AUTHORIZATION DBA";
update(sql);
// update("set " + schemaName);
}

public void dropSchema(final String schemaName) throws SQLException
Expand Down
26 changes: 13 additions & 13 deletions kundera-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,20 @@
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version> <scope>test</scope> </dependency> -->

<!-- <dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
<scope>test</scope>
</dependency> -->
<!-- <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId>
<version>2.2.9</version> <scope>test</scope> </dependency> -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>

</dependency>
<dependency>
<groupId>commons-collections</groupId>
Expand Down Expand Up @@ -201,11 +204,8 @@
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<!-- <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r08</version>
</dependency> -->
<!-- <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId>
<version>r08</version> </dependency> -->
<dependency>
<groupId>com.github.stephenc</groupId>
<artifactId>jamm</artifactId>
Expand Down Expand Up @@ -238,12 +238,12 @@
<artifactId>clhm-production</artifactId>
<version>UNKNOWN</version>
</dependency>
<dependency>
<!-- <dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.9.0.GA</version>
</dependency>

-->
<!-- Cassandra Dependencies -->
<dependency>
<groupId>org.apache.cassandra</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ protected void shutDownRdbmsServer() throws SQLException
try
{
cli.dropSchema(KEYSPACE);
cli.shutdown();
}
catch (Exception e)
{
Expand Down
Loading

0 comments on commit 1be805a

Please sign in to comment.