-->
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.  [ 5 posts ] 
Author Message
 Post subject: programmatic mapping generation example/tutorial needed
PostPosted: Tue Jan 13, 2009 11:25 am 
Newbie

Joined: Tue Jan 13, 2009 11:08 am
Posts: 3
hi folks,

since days i am searching references, tutorials, anything that can help me make a working programmatic mapping configuration for Hibernate 3.

the only thing i can find are a short pseudo-example (declared as absolutely not complete) and some non-working code on this forum, thats all... as i see the API seems to be there to create mappings programmatically, but i can find absolutely nothing how to use it....

for the beginning i only want to create a trivial mapping to a table consisting of an auto-increment integer and a varchar name.

any help is highly appreciated.

thanks in advance,

---loki


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 11:53 am 
Newbie

Joined: Tue Jan 13, 2009 11:49 am
Posts: 11
you could take a look at the Envers project. They add some mappings to the hibernate Configuration. Starting point to browse is org.jboss.envers.event.VersionEventListener.initialize(Configuration conf)

I hope that helps,

Dirk


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 7:05 am 
Newbie

Joined: Tue Jan 13, 2009 11:08 am
Posts: 3
thanks, that might be an idea... i just hoped the usage of this API was documented somehwere...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 8:27 am 
Newbie

Joined: Tue Jan 13, 2009 11:49 am
Posts: 11
I'm currently struggling with the lack of documentation too.

There are 2 different approaches I use:

- adding xml elements to the Configuration (like Envers). I used that for additional class mappings. You can take the hibernate xml configuration documentation as documentation. It basically looks something like this:

DOMWriter writer = new DOMWriter();
Document document = DocumentHelper.createDocument();
Element hibernate_mapping = document.addElement("hibernate-mapping");
hibernate_mapping.addAttribute("auto-import", "false");

Element class_mapping = hibernate_mapping.addElement("class");

class_mapping.addAttribute("entity-name", versionName);
class_mapping.addAttribute("table", clazz.getTable().getName() + "_version");

Element id_mapping = class_mapping.addElement("id");
id_mapping.addAttribute("name","id");
id_mapping.addAttribute("type","long");
id_mapping.addAttribute("column","id");
Element id_generator = id_mapping.addElement("generator");
id_generator.addAttribute("class","native");

Element main_mapping = class_mapping.addElement("many-to-one");
main_mapping.addAttribute("name", "main");
main_mapping.addAttribute("column", "main_id");
// main_mapping.addAttribute("class", clazz.getEntityName());
main_mapping.addAttribute("lazy", "false");
main_mapping.addAttribute("entity-name", clazz.getEntityName());

conf.addDocument(writer.write(document));
conf.buildMappings();



- adding stuff to PersistentClass and adding Collection mappings. I use that for adding a Relation to an already existing Class Mapping. Since I could not find any documentation or examples for this, I just made a sample entity with a similar relation and copied the structure I saw when debugging. The code I have (just testing it myself) looks something like this:

PersistentClass versionClass = conf.getClassMapping(versionName);

Property versionsRel = new Property();
Set set = new Set(clazz);
OneToMany one2Many = new OneToMany(clazz);
one2Many.setReferencedEntityName(versionName);
one2Many.setAssociatedClass(versionClass);
set.setCollectionTable(versionClass.getTable());
SimpleValue mainPropertyValue = new SimpleValue(clazz.getTable());
mainPropertyValue.addColumn(new Column("main_id"));
mainPropertyValue.setTypeName("long");
DependantValue dependantValue = new DependantValue(versionClass.getTable(),mainPropertyValue);
dependantValue.addColumn(new Column("main_id"));
Properties idGenProperties = new Properties();
idGenProperties.setProperty("target table", clazz.getTable().getName());
idGenProperties.setProperty("target column","id");
dependantValue.setIdentifierGeneratorProperties(idGenProperties);
set.setKey(dependantValue);
set.setRole(clazz.getEntityName() + ".versions");
set.setFetchMode(FetchMode.SELECT);
set.setLazy(false);

set.setElement(one2Many);
set.setInverse(true);
set.setNodeName("versions");
set.setOwner(clazz);
versionsRel.setValue(set);

versionsRel.setInsertable(false);
versionsRel.setName("versions");
versionsRel.setNodeName("versions");
versionsRel.setPropertyAccessorName("my special thing");

versionsRel.setLazy(false);
clazz.addProperty(versionsRel);

conf.createMappings().addCollection(set);

maybe this is a little inspiration to you.

Dirk


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 11:30 am 
Newbie

Joined: Tue Jan 13, 2009 11:08 am
Posts: 3
thanks, i will study this when i find some time.

i also thought that maybe analysing the GORM part of Grails might be in idea here...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.