views:

73

answers:

1

Hi all,

I'd like to know if it is possible to fill up a class data from database using the hibernate hbm (mapping).

For instance:

public class someClass {  
    List<OtherClass> otherClasses;  
    List<YetAnotherClass> yetAnotherClasses;  
    //Constructors ?  

    class OtherClass {  
        String name;  
        //setters, getters  
    }  

    class YetAnotherClass {  
        String name;  
        //setters, getters  
    }
    //setters, getters  
}

Using an hbm can I fill in the data from tables OTHER_CLASS_TABLE and YET_ANOTHER_CLASS_TABLE?

I have no such SOME_CLASS_TABLE since this info is for viewing only.

I've been playing with the <join table=""><subselect> and different constructors... But it is not working

Thanks!

Sorry for my english!

+2  A: 

So you have a class which seems not to be a real entity, since it does not have identity to start with, right? Then I woulf guess you can't map it directly.

Depending on how do you actually use someClass, I can think of the following workarounds:

  • if it is part of an entity, you could try mapping it as component - but then again, you could simply map the contained lists directly in this case
  • if it is simply used to store query results, you could put together a scalar query to return the contents of the two lists, then assemble your objects from the results
Péter Török
@Péter: actually it is a result of a complicated SQL query. I wonder if I could map it in hibernate and then retrieveit as an instance.
Udo Fholl
@Udo, there _might_ be a way to map it but I am not aware of it (then again, I am not a Hibernate guru). But assembling the object from a scalar query result should work, even if it may not be an elegant solution.
Péter Török

related questions