1

Well, the question explains itself, here is how i tried to do it. I think the problem might be that the Set is empty, and those methods need at least one element to return the class.

views/product/_form.gsp

<% cl = UnidadProductiva.get(params.unidadProductiva?.id).producto %>
<p>${cl}</p>

returns []

if i add this method/attributes after producto:

.getCandidateClass() : No signature of method:
org.hibernate.collection.PersistentSet.getCandidateClass() is applicable for argument types: () values: []

.class :
class org.hibernate.collection.PersistentSet

.getElementType() : No signature of method:
org.hibernate.collection.PersistentSet.getElementType() is applicable for argument types: () values: [] Possible solutions: getElement(java.lang.Object)

.properties :

{
    clearQueueEnabled=true,
    session=SessionImpl(PersistenceContext[
        entityKeys=[
            EntityKey[
                planificador.UnidadProductiva#1
            ]
        ],
        collectionKeys=[
            CollectionKey[
                unidadesProductivas.Cocimiento.producto#1
            ],
            CollectionKey[
                planificador.UnidadProductiva.grupoRecursos#1
            ],
            CollectionKey[
                planificador.UnidadProductiva.lineaProduccion#1
            ]
        ]
    ];ActionQueue[
        insertions=[

        ]updates=[

        ]deletions=[

        ]collectionCreations=[

        ]collectionRemovals=[

        ]collectionUpdates=[

        ]
    ]),
    unreferenced=false,
    role=unidadesProductivas.Cocimiento.producto,
    directlyAccessible=false,
    empty=true,
    storedSnapshot={

    },
    operationQueueEnabled=false,
    value=[

    ],
    owner=unidadesProductivas.Cocimiento: 1,
    cachedSize=-1,
    class=classorg.hibernate.collection.PersistentSet,
    rowUpdatePossible=false,
    snapshot={

    },
    key=1,
    putQueueEnabled=false,
    dirty=false
}

i am expecting a method or a property that returns CaldoMadre

And this are the classes i am using

class UnidadProductiva {...}

class Cocimiento extends UnidadProductiva {
    static hasMany = [producto:CaldoMadre];
}

class Producto {
    static belongsTo = [unidadProductiva:UnidadProductiva]
}

class CaldoMadre extends Producto {...}

I am a chilean noob to grails, please be patient if i dont undestand.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • 1
    its not immediately obvious to me what `CandidateClass` is/means here. Can you clarify? Also, the results you get from your attempt might shed some light on the problem. – Brian Henry Dec 20 '12 at 20:03
  • Looks like a duplicate of http://stackoverflow.com/questions/801940/deriving-class-from-generic-t – Aaron Kurtzhals Dec 20 '12 at 21:34
  • @AaronKurtzhals, correct me if i am wrong, but the solution to that post, applied to my problem would be adding a parameter to cocimiento like `String productClass = "CaldoMadre"' – user1919828 Dec 20 '12 at 21:59

1 Answers1

0

I'm able to get this to work on a Cocimiento but not a UnidadProductiva since there is no declared field producto in the UnidadProductiva. It's a little awkward to use in a gsp but maybe you could do it in the controller and pass it out:

//in controller
import java.lang.reflect.*

//in show action or whichever makes sense
Field field = Cocimiento.class.getDeclaredField("producto");
ParameterizedType pt = (ParameterizedType) field.getGenericType();
Type concreteType = pt.getActualTypeArguments()[0];
println concreteType.getName()

Will print out com.CaldoMadre with my package setup.

And it doesn't rely on there being anything in the Set

Kelly
  • 3,709
  • 4
  • 20
  • 31
  • when u use pt.getActualTypeArguments()[0];, that get the type of the first element right?, the problem is that starts empty. a possible solution would be adding a parameter to cocimiento like `String productClass = "CaldoMadre"', but it is the worst way to do it. – user1919828 Dec 21 '12 at 13:50
  • No - it does not get the type of the first element. When I tested I didn't instantiate any actual domain objects. Notice I'm not using `Cocimiento.get(id)`. What the `hasMany` ends up like is `Set = new HashSet`. So the `pt.getActualTypeArguments()[0]` is getting the first parameter to the generic type - in this case ``. If you had something like `HashMap` then `pt.getActualTypeArguments()[0]` would be `CaldoMadre` and `pt.getActualTypeArguments()[1]` would be `Integer`. – Kelly Dec 21 '12 at 17:23
  • Thanks, that would be exacltly what i needed – user1919828 Dec 21 '12 at 18:33