These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Form Generation for Persistable Objects
PostPosted: Sun Nov 14, 2004 3:11 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
Hibernate version: 2.1.4

My boss has asked me to build a Generic Form class in Java (1.4.2) which takes parameters (Class, InstanceID, Datasource) and displays (generates) an HTML form for performing CRUD operations.

the parameters have the following meanings:
-----------------------------------------------------------------------------------
Class - the fully qualified name of the java class we will be persisting

InstanceId - if not null, represents the instance of the object we will be READING, UPDATING or DELETING. if null, we will be INSERTING.

Datasource - which datasource to use for persisting this object.
-----------------------------------------------------------------------------------

OK, that's the scenario. I had originally thought about using java reflection to build the list of properties the class has, and consequently build the text fields, radio buttons, checkboxes, etc according to naming conventions on the field level. But then I started getting into problems when I wanted to include too much information in the naming convention. FOR EXAMPLE: private String description_255_not-null

where description is the actual name of the property, 255 is the max length, and not-null indicates that its a required field. This is when I remembered that the *.HBM.XML document already contained this level of information on the properties and that it wouldn't be necessary to build this into a crazy-long naming convention.

(now for the question)
Is there a class within Hibernate that parses these *.HBM.XML and retrieves these properties? I have look around the API but to no avail... I tried this bit of code to see what it got me, but it only brought my the classes in the sessionFactory from Spring (which was useful, but not enough):

Code:
SessionFactory sessionFactory=(SessionFactory)ctx.getBean(sessionFactoryRef);
Map metaData=sessionFactory.getAllClassMetadata();


Also, I looked into these methods, but I could have gotten this information through java reflection:

Code:
EntityPersister.getPropertyNames();
EntityPersister.getPropertyTypes();
EntityPersister.getPropertyValues();


Again, I would need to be able to not only retrieve the fields themselves (i.e. description) but also the properties of the fields (i.e. length=255, not-null, etc) from the HBM.XML.

Thanks in advance for any help!
karokain
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 4:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Some of the stuff is available in the classmetdata.

All of the stuff is available via the Configuration and the classes in the mapping package.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 5:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You might have the drop to JDBC Meta data as i'm not sure you can get the Configuration object from the MBean. I know there have been changes in this area recently so it may have changed but I had to do it this way in the past.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 11:24 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
thanks, I'll look into these options and get back to you...

-karokain


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 5:59 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
thanks guys.. the Configuration object seems to be what I was looking for. I didn't have an actual hibernate.cfg.xml before (I guess Spring was generating one for me based on their applicationContext.xml), so I have resorted to using XDoclet's <hibernatecfg> task in my ant build. It was very easy to implement and now gives me direct access to the Configuration object in Hibernate. The only thing I haven't found out how to do in the <hibernatecfg> task yet is to distinguish which classes go to which datasources (via their independent Session Factories)... instead, the task seems to be lumping everything into one datasource. But, I'm sure there is a way to do it, just haven't found it yet.

My next step is to take the information furnished by the net.sf.hibernate.cfg.Configuration Object and to build the field labels and input tags (being of type text, textarea, radio, checkbox, select, etc) with appropiate lengths (i.e. text field for name will have length 50 let's say, and since the length of description is 4000, we'll use a textarea with x columns and y rows...). Also, I know when to use select fields because they will be necessary for registering the relationships (objects that have other objects as properties).

One stumbling block I know I want to include, but haven't given much thought to yet, is the inclusion of validation and business rules... perhaps by adding more attributes to the hbm.xml... but then again this must be illegal because of the DTD... do you have any suggestions on this aspect of the form generation which may be useful? perhaps use of the struts-validation.xml... or even xml schema or schematron... just haven't looked hard enough yet because I want to see the form being generated first :) later I will definitly want to add dynamic business rules and validation (in a template manner similar to what struts has)...

Also, any general comments about our effort to try to create a generic form for persistable objects is welcome, for surely the idea was not ours alone, OR perhaps it is not a feasible solution??

thanks again Max and David for your help so far! it is much appreciated :)
-karokain


Top
 Profile  
 
 Post subject: Validation rules
PostPosted: Sun Dec 05, 2004 2:31 am 
Beginner
Beginner

Joined: Thu Jun 17, 2004 11:25 pm
Posts: 21
Location: Los Angeles
If you can come up with a good solution to the validation rules problem you should win a prize. It's been around as long as computers have taken input from humans and I've yet to see a good solution.

The problem is just not simplifiable. The difficulty is in formulating and expressing the actual validation rules, not in applying them. The best you can do with as generic an application as yours is make sure the data is valid according to the constraints of the database schema.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 05, 2004 11:14 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
for validation rules check spring-rcp project - it is swing example, but I think that it can use in any application

regards


Top
 Profile  
 
 Post subject: validation
PostPosted: Mon Dec 13, 2004 11:25 am 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
thanks for the continued interest. I'll take a look into the spring sollution.

~karokain


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.