Dynamic class generation in combination with O/R

Hello I am not so familiar with reflection, but I read the tutorial on the official Java page.

But what I can't find out is how to do this.

Say I want a class that is central to save all my persistable objects.

So instead of a saveUser, saveProfile, saveMachine, etc. i would only have saveObject.

But how do I do this?

What I want to accomplish is a function that does something like this (using hibernate):

--

public void saveObject( Object object)

{

...//get hibernate session

...

...//do something so that object is cast into either, User, Machine, Profile, etc.

...//so that it becomes object = (<good class name>) object;

...save(object);

...

...//close hibernate session

}

--

So it doesn't know what object it gets, but it should be known through reflection (or is there another way?) what it is.

I have thought about object.getClass() & forName() etc. but they all don't really seem to work for what i want.

Anyone has a suggestion or a solution?

Thank you.

edit:

example of what doesn't work:

...

Machine machine = new Machine();

Object o = machine;

Class c = o.getClass();

c object = (c) o;

...

c.forName("Machine") object;

[1334 byte] By [radicjesa] at [2007-11-27 0:50:42]
# 1

what does "a class that is central to save all my persistable objects" mean? it's really unclear what you're asking for, here. are you saying you want a base class that all persistent classes extend? no point doing that with Hibernate, it goes to some considerable trouble to be transparent enough that you don't need such a class

you don't need to do any casting in order to get Hibernate to save your objects. it'll work it out for you, as long as you have an HBM for it

what are you actually asking? try and keep to the problem, not what you think might be a solution

georgemca at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 2

The problem is that I want a function, that can accept all different kinds of objects.

I want to give this object to hibernate so i don't have to write different save functions (and get, and delete, and update etc.) for every object that I want to use in hibernate.

The non-dynamic way:

....

saveUser( User user )

{

...

save(user)

...

}

...

...

saveMachine( Machine machine)

{

...

save(machine)

...

}

...

...

saveXXXX( XXXX xxxx)

{

...

save(xxxx);

...

}

...

What I want:

...

saveObject( Object object)

{

...

save(object);

...

}

...

I appreciate the help :)

radicjesa at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 3
hibernate already gives you such a method
georgemca at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 4

Shouldn't this work?

public void saveObject( Object object)

{

...get hibernate session

...session.save(object);

...close hibernate session

}

Peetzorea at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> Shouldn't this work?

> > public void saveObject( Object object)

> {

> ...get hibernate session

> ...session.save(object);

> ...close hibernate session

> }

>

>

precisely

georgemca at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 6
HmKind of stupid of me that i had not tested that :)Thanks for the help guys
radicjesa at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 7

> Hm

> Kind of stupid of me that i had not tested that :)

>

> Thanks for the help guys

or even followed a quick-start tutorial on hibernate, that would've shown you that. or thought "since the authors of hibernate don't know what sort of objects we're going to use with it, how did they allow for the saving of any kind of object?" :-)

georgemca at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...
# 8
It's a kind of magic! Check out the class org.hibernate.core.CrystallBall
Peetzorea at 2007-7-11 23:21:01 > top of Java-index,Java Essentials,Java Programming...