Skip to content

Commit

Permalink
#1 Implement many-to-many
Browse files Browse the repository at this point in the history
  • Loading branch information
daowangli@gmail.com committed Dec 3, 2013
1 parent 708b183 commit 11ef8f3
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 521 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,8 @@ public Table addTable(
String subselect,
boolean isAbstract) {

name = StringHelper.getTableNameFromArchetypeId(name);
name = StringHelper.getTableNameFromArchetypeId(name);
name = StringHelper.getColumnNameFromArchetypePath(name);
name = getObjectNameNormalizer().normalizeIdentifierQuoting( name );
schema = getObjectNameNormalizer().normalizeIdentifierQuoting( schema );
catalog = getObjectNameNormalizer().normalizeIdentifierQuoting( catalog );
Expand Down
12 changes: 6 additions & 6 deletions hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1129,11 +1129,11 @@ else if ( columnElement.getName().equals( "formula" ) ) {
if ( column.isUnique() && ManyToOne.class.isInstance( simpleValue ) ) {
( (ManyToOne) simpleValue ).markAsLogicalOneToOne();
}
final String columnName = columnAttribute.getValue();
String logicalColumnName = mappings.getNamingStrategy().logicalColumnName(
columnName, propertyPath
);
column.setName( mappings.getNamingStrategy().columnName( columnName ) );
String columnName = columnAttribute.getValue();
columnName = StringHelper.getTableNameFromArchetypeId(columnName);
columnName = StringHelper.getColumnNameFromArchetypePath(columnName);
String logicalColumnName = columnAttribute.getValue();
column.setName(columnName);
if ( table != null ) {
table.addColumn( column ); // table=null -> an association - fill
// it in later
Expand Down Expand Up @@ -1757,7 +1757,7 @@ public static void bindOneToOne(Element node, OneToOne oneToOne, String path, bo

public static void bindOneToMany(Element node, OneToMany oneToMany, Mappings mappings)
throws MappingException {

oneToMany.setReferencedEntityName( getEntityName( node, mappings ) );

String embed = node.attributeValue( "embed-xml" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.pretty.MessageHelper;
import org.openehr.rm.common.archetyped.Locatable;
import org.openehr.rm.datastructure.itemstructure.representation.Element;
import org.openehr.rm.datatypes.text.DvText;

/**
* Represents state associated with the processing of a given {@link ResultSet}
Expand Down Expand Up @@ -162,7 +165,7 @@ public PersistentCollection getLoadingCollection(final CollectionPersister persi
*
* @param persister The persister for which to complete loading.
*/
public void endLoadingCollections(CollectionPersister persister) {
public void endLoadingCollections(List results, CollectionPersister persister) {
final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
if ( !loadContexts.hasLoadingCollectionEntries()
&& localLoadingCollectionKeys.isEmpty() ) {
Expand Down Expand Up @@ -216,6 +219,38 @@ else if ( lce.getResultSet() == resultSet && lce.getPersister() == persister ) {
// processing from the (sandbox/jdbc) jdbc-container code.
loadContexts.cleanup( resultSet );
}

if (matches != null && results != null) {
for (LoadingCollectionEntry lce : matches) {
String nodeNameOnetomany = lce.getPersister().getNodeName();
String nodeNameItems = nodeNameOnetomany.substring(0, nodeNameOnetomany.lastIndexOf('/')).concat("/items");
String key = (String) lce.getKey();
for (Object object : results) {
Locatable loc = (Locatable) object;
if (loc.getUid().getValue().equals(key)) {
Set set = (Set) loc.itemAtPath(nodeNameOnetomany);
if (set != null) {
List<Locatable> list = (List<Locatable>) loc.itemAtPath(nodeNameItems);
if (list == null) {
list = new ArrayList<Locatable>();
}
for (Object associatedObject : set) {
if (associatedObject instanceof Locatable) {
Locatable associatedLoc = (Locatable) associatedObject;
String associatedUid = associatedLoc.getUid().getValue();
DvText text = new DvText(associatedUid);
Element element = new Element("", associatedLoc.getArchetypeNodeId(), text);
list.add(element);
loc.getAssociatedObjects().put(associatedUid, associatedLoc);
}
}
loc.set(nodeNameOnetomany, null);
loc.set(nodeNameItems, list);
}
}
}
}
}
}

private void endLoadingCollections(CollectionPersister persister, List<LoadingCollectionEntry> matchedCollectionEntries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.hibernate.type.Type;
import org.openehr.am.archetype.Archetype;
import org.openehr.am.archetype.constraintmodel.CObject;
import org.openehr.am.archetype.ontology.ArchetypeOntology;
import org.openehr.am.archetype.ontology.ArchetypeTerm;
import org.openehr.rm.common.archetyped.Locatable;

/**
Expand Down Expand Up @@ -474,7 +476,11 @@ public static void setArchetypeValue(Locatable loc, String propertyPath, Object
);

Setter setter = propertyAccessor.getSetter(tempTarget.getClass(), pathSegment);
if (klass.isPrimitive() || ClassUtils.wrapperToPrimitive(klass) != null || String.class.equals(klass)) {
Getter getter = propertyAccessor.getGetter(tempTarget.getClass(), pathSegment);
if (klass.isPrimitive() ||
ClassUtils.wrapperToPrimitive(klass) != null ||
String.class.isAssignableFrom(klass) ||
Set.class.isAssignableFrom(klass)) {
if (propertyValue instanceof Locatable) {
String uid = ((Locatable) propertyValue).getUid().getValue();
setter.set(tempTarget, uid, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,8 @@ private static String cleanAlias(String alias) {

public static String unqualifyEntityName(String entityName) {
String result = unqualify(entityName);
int slashPos = result.indexOf( '/' );
if ( slashPos > 0 ) {
result = result.substring( 0, slashPos - 1 );
}
result = getTableNameFromArchetypeId(result);
result = getColumnNameFromArchetypePath(result);
return result;
}

Expand Down
18 changes: 11 additions & 7 deletions hibernate-core/src/main/java/org/hibernate/loader/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,8 @@ protected List processResultSet(
rs,
session,
queryParameters.isReadOnly( session ),
afterLoadActions
afterLoadActions,
results
);
if ( createSubselects ) {
createSubselects( subselectResultKeys, queryParameters, session );
Expand Down Expand Up @@ -1065,7 +1066,8 @@ private void initializeEntitiesAndCollections(
resultSetId,
session,
readOnly,
Collections.<AfterLoadAction>emptyList()
Collections.<AfterLoadAction>emptyList(),
null
);
}

Expand All @@ -1074,7 +1076,8 @@ private void initializeEntitiesAndCollections(
final Object resultSetId,
final SessionImplementor session,
final boolean readOnly,
List<AfterLoadAction> afterLoadActions) throws HibernateException {
List<AfterLoadAction> afterLoadActions,
List results) throws HibernateException {

final CollectionPersister[] collectionPersisters = getCollectionPersisters();
if ( collectionPersisters != null ) {
Expand All @@ -1085,7 +1088,7 @@ private void initializeEntitiesAndCollections(
//during loading
//TODO: or we could do this polymorphically, and have two
// different operations implemented differently for arrays
endCollectionLoad( resultSetId, session, collectionPersisters[i] );
endCollectionLoad( resultSetId, session, collectionPersisters[i], results );
}
}
}
Expand Down Expand Up @@ -1118,7 +1121,7 @@ private void initializeEntitiesAndCollections(
//the entities, since we might call hashCode() on the elements
//TODO: or we could do this polymorphically, and have two
// different operations implemented differently for arrays
endCollectionLoad( resultSetId, session, collectionPersisters[i] );
endCollectionLoad( resultSetId, session, collectionPersisters[i], results );
}
}
}
Expand Down Expand Up @@ -1147,12 +1150,13 @@ private void initializeEntitiesAndCollections(
private void endCollectionLoad(
final Object resultSetId,
final SessionImplementor session,
final CollectionPersister collectionPersister) {
final CollectionPersister collectionPersister,
List results) {
//this is a query and we are loading multiple instances of the same collection role
session.getPersistenceContext()
.getLoadContexts()
.getCollectionLoadContext( ( ResultSet ) resultSetId )
.endLoadingCollections( collectionPersister );
.endLoadingCollections( results, collectionPersister );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ private void endLoadingArray(CollectionPersister persister) {
session.getPersistenceContext()
.getLoadContexts()
.getCollectionLoadContext( resultSet )
.endLoadingCollections( persister );
.endLoadingCollections( null, persister );
}
}
}
Expand Down Expand Up @@ -733,7 +733,7 @@ private void endLoadingCollection(CollectionPersister persister) {
session.getPersistenceContext()
.getLoadContexts()
.getCollectionLoadContext( resultSet )
.endLoadingCollections( persister );
.endLoadingCollections( null, persister );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
import org.hibernate.internal.FilterConfiguration;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.EmptyIterator;
import org.hibernate.type.CollectionType;
import org.hibernate.type.Type;
import org.openehr.am.archetype.Archetype;
import org.openehr.build.RMObjectBuilder;

/**
* Mapping for a collection. Subclasses specialize to particular collection styles.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.openehr.am.archetype.Archetype;
import org.openehr.build.RMObjectBuilder;

/**
* A mapping for a one-to-many association
Expand Down
18 changes: 0 additions & 18 deletions hibernate-core/src/main/java/org/hibernate/mapping/ToOne.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.mapping;
import java.util.Map;

import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.cfg.Mappings;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.type.Type;
import org.openehr.am.archetype.Archetype;
import org.openehr.am.archetype.constraintmodel.CObject;
import org.openehr.am.archetype.ontology.ArchetypeOntology;
import org.openehr.am.archetype.ontology.ArchetypeTerm;

/**
* A simple-point association (ie. a reference to another entity).
Expand Down Expand Up @@ -88,18 +82,6 @@ public void setTypeUsingReflection(String className, String propertyName)
}
}

public void setArmTypeUsingReflection(Archetype archetype, String propertyName)
throws MappingException {
if (referencedEntityName==null) {
ArchetypeOntology ontology = archetype.getOntology();
String language = archetype.getOriginalLanguage().getCodeString();
String code = propertyName.substring(propertyName.lastIndexOf('[') + 1, propertyName.lastIndexOf(']'));
ArchetypeTerm term = ontology.termDefinition(language, code);
String archetypeId = term.getItem("description");
referencedEntityName = archetypeId;
}
}

public boolean isTypeSpecified() {
return referencedEntityName!=null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.hibernate.engine.spi.Mapping;
import org.hibernate.type.Type;
import org.openehr.am.archetype.Archetype;
import org.openehr.build.RMObjectBuilder;

/**
* A value is anything that is persisted by value, instead of
Expand Down
Loading

0 comments on commit 11ef8f3

Please sign in to comment.