Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / October 2005

Tip: Looking for answers? Try searching our database.

Can Hibernate persist class variables?

Thread view: 
bobalong@gmx.net - 26 Oct 2005 18:59 GMT
Hi there

I have a situation I'm completely stuck on. I'm creating some POJOs for
use in conjunction with Hibernate. Now some of my POJOs have class
(static) variables that contain shared configuartion information. For
example:

class Car
{
    // example picture of a car
    public static String pictureUrl;

    // model and age of this car
    private String model;
    private int age;

    public Car(String model, int age)
    {
        this.model = model;
        this.age = age;
    }

    // getters/setters etc...
}

Car.pictureUrl = "http://url/to/shared/car/picture.jpg";
Car c = new Car("Model", 5);

//... do a bunch of stuff

Car.pictureUrl = "http://url/to/new/shared/car/picture.jpg";

I want to persist my newly created Car, c, along with the shared class
variable Car.pictureUrl. My question is there any way to tell Hibernate
to persist Car.pictureUrl?

The reason I want to do this is that I want to be able to update class
variables *at runtime*, and somehow persist them so the new value gets
used when the application is restarted. Normally I would hard-code
class variables in my POJO, but I want to be able to update them at
runtime (having the change immediately reflected in all parts of my
app), and have the new values persisted over application restarts, all
without re-compiling the application.

Has anyone had the need to do something like this before? What strategy
should I use? Ideally I'd like to use a database to store the class
variable values and have them populated on application start.

Am I going about this the wrong way?

Any comments would be greatly appreciated!

Thanks
Rob
Adam Maass - 28 Oct 2005 05:27 GMT
> Hi there
>
[quoted text clipped - 47 lines]
>
> Any comments would be greatly appreciated!

There are theoretical problems with persisting class state in addition to
instance state; this has to do with the scope of class state. (If you update
the class variable, when do you expect to see the update in other concurrent
transactions? What do you do if the transaction fails? What happens if you
have more than one instance of your application running concurrently? How
does the second instance get the updated state of the class variable?)

These questions aren't impossible to answer, but Hibernate is not intended
to provide an answer for them that makes any kind of consistent sense. If
you insist on Hibernate, I recommend a singleton "default" object that
contains your defaults. This then gets around the theoretical difficulties I
alluded to above. It might also buy you some added flexibility that you
don't have by storing defaults as class variables.

By the way, Matisse, www.matisse.com, is the only object database or O/RM
solution I know of that solves the problem of persistence of class data
nicely. (And that's because it treats classes as first-class objects
themselves -- and treats schema modifications transactionally, unlike
virtually every other database product I've ever worked with.) It has other
problems, so I don't recommend it as a general-purpose solution, but it is
available if you're interested.

I hope this helps.

-- Adam Maass


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.