-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: programmatic mapping configuration
PostPosted: Tue Nov 04, 2003 2:29 pm 
Newbie

Joined: Tue Nov 04, 2003 2:19 pm
Posts: 5
Location: Emeryville, California
I see in the reference manual section 4.1 that mappings *may* be defined with XML. I would like to explore programmatically creating the mappings. I see in the API:

net.sf.hibernate.cfg.Configuration.createMappings().

The resultant Mappings object seems to suggest this kind of functionality. But the docs are silent on the use of the created Mappings object. Anyone have any ideas?[/b]


Top
 Profile  
 
 Post subject: Back up...
PostPosted: Tue Nov 04, 2003 2:53 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Back up and read Section 2.1 again.


Top
 Profile  
 
 Post subject: section 2.1
PostPosted: Tue Nov 04, 2003 4:13 pm 
Newbie

Joined: Tue Nov 04, 2003 2:19 pm
Posts: 5
Location: Emeryville, California
Hi David,

Thanks. Okay I read that section again. I'm not sure which part you're suggesting is relevant. It is entitled "Programmatic Configuration" but it seems only to discuss programmatically feeding the configuration from XML files. I'm wondering how to eliminate the need for the XML files altogether.

Are you suggesting I notice the sentence "These mappings are compiled from various XML mapping files." and read that to mean there is no way to do this without XML files?

Regards,

Barry


Top
 Profile  
 
 Post subject: Oh
PostPosted: Tue Nov 04, 2003 5:59 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Sorry, I misunderstood, I thought you just didn't want to have a hibernate.cfg.xml that listed all of the others.

I'm not sure of details how you could configure with no XML at all.

Try this thread:
http://forum.hibernate.org/viewtopic.php?t=925196


Top
 Profile  
 
 Post subject: thanks
PostPosted: Tue Nov 04, 2003 7:16 pm 
Newbie

Joined: Tue Nov 04, 2003 2:19 pm
Posts: 5
Location: Emeryville, California
Thanks David,

I see the thread discussing the use of a DOM tree to feed the configuration.

I might try first a more primitive direct configuration starting with Configuration.getMappings().addClass(PersistentClass) etc.

It seems we'll potentially be the first trying a non-XML approach. I'll report back if this works out.

regards,

Barry


Top
 Profile  
 
 Post subject: You would still need the XML files for each class...
PostPosted: Wed Nov 05, 2003 12:26 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Quote:
I might try first a more primitive direct configuration starting with Configuration.getMappings().addClass(PersistentClass) etc.


The addClass(Class) method assumes that there is an appropriately named .hbm.xml file in the same package as the Class passed as an argument to the method and tries to load it using getResourceAsStream().
This is documented in section 2.1 of the reference.

I think that DOM approach is the closest you are going to get to not having an actual XML file on your HD. You have to tell Hibernate somehow what table a particular class maps to and what properties of the class map to what columns etc. (and then you get into relationships between classes etc. etc.) so you need to give it all of this information. The provided interface for doing so is the xml file(s) (or manually creating the equivalent DOM in memory).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 7:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No no, the method he mentioned, Configuration.getMappings().addClass(PersistentClass), allows programmatic creation of mappings.


Top
 Profile  
 
 Post subject: Have I misunderstood?
PostPosted: Wed Nov 05, 2003 9:07 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
My mistake. I was going by this:
http://www.hibernate.org/hib_docs/reference/html/session-configuration.html#session-configuration-s1

And understood the following, particularly the last sentence, to apply to the .addClass(Class) method.

Quote:
An alternative (better?) way is to let Hibernate load a mapping file using getResourceAsStream().

Code:
Configuration cfg = new Configuration()
    .addClass(eg.Vertex.class)
    .addClass(eg.Edge.class);

Then Hibernate will look for mapping files named /eg/Vertex.hbm.xml, /eg/Edge.hbm.xml in the classpath. This approach eliminates any hardcoded filenames.


I've looked at the code in net.sf.hibernate.cfg.Configuration and it certainly appears to me that this is what it is doing...
Unless a .hbm.xml is being generated by something for the Class at runtime before calling .addClass?

I'm not sure what I've misunderstood here? I do notice that in his last post the call was actually Configuration.getMappings().addClass(PersistentClass) but I can not find such a method on the latest version of Configuration. I do see several get*Mappings() methods...

Is it actually a call to Mappings.addClass(PersistentClass) vs. Configuration.addClass(Class)?

Sorry to have confused things.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 9:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ah its actually called createMappings(). I knew what he was talking about, so I propagated his mistype ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 9:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
P.S. Someone (unfortunately me, probably) needs to write a simple tutorial on how to programmatically create a mapping. It looks something like this:

Code:
Table tab = new Table();
tab.setName("foo");
Column col = new Column(Hibernate.INTEGER, 0);
col.setName("bar");
tab.addColumn(tab);
Value value = new Value(tab);
value.setType(Hibernate.INTEGER);
value.addColumn(col);
Property prop = new Property(value);
prop.setName("bar");
RootClass clazz = new RootClass();
clazz.setPersistentClass(Foo.class);
clazz.setTable(tab);
clazz.addProperty(prop);
....
cfg.createMappings().addClass(clazz);


Of course, its a bit more complicated than that (we need an id property, primary key, associations, etc). But thats a start.

I could also spend a few hours cleaning up the API. sometimes its unclear why something is set in constructor instead of with a setter. Stuff like that.


Top
 Profile  
 
 Post subject: sorry about the confusion
PostPosted: Thu Nov 06, 2003 1:37 am 
Newbie

Joined: Tue Nov 04, 2003 2:19 pm
Posts: 5
Location: Emeryville, California
Yes, indeed, I meant "createMappings()" and not "getMappings()".

Sorry my fumble fingers caused confusion. :(

Gavin, thanks for the example. That, together with your Hibernate code itself, is probably all I'll need for now.

regards (and apologetically),

Barry


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 1:07 am 
Newbie

Joined: Tue Aug 16, 2005 2:18 pm
Posts: 5
gavin wrote:
P.S. Someone (unfortunately me, probably) needs to write a simple tutorial on how to programmatically create a mapping. It looks something like this:

Code:
Table tab = new Table();
tab.setName("foo");
Column col = new Column(Hibernate.INTEGER, 0);
col.setName("bar");
tab.addColumn(tab);
Value value = new Value(tab);
value.setType(Hibernate.INTEGER);
value.addColumn(col);
Property prop = new Property(value);
prop.setName("bar");
RootClass clazz = new RootClass();
clazz.setPersistentClass(Foo.class);
clazz.setTable(tab);
clazz.addProperty(prop);
....
cfg.createMappings().addClass(clazz);


Of course, its a bit more complicated than that (we need an id property, primary key, associations, etc). But thats a start.

I could also spend a few hours cleaning up the API. sometimes its unclear why something is set in constructor instead of with a setter. Stuff like that.


Gavin,
Could you point me to some documentation on createMapping and Table, Values etc...


Top
 Profile  
 
 Post subject: Creating a sample program -Creating programmatic mappings
PostPosted: Mon Jan 07, 2008 3:35 am 
Newbie

Joined: Wed Jan 02, 2008 5:05 am
Posts: 2
Hi
I am trying this example out,as per sample code provided by Gavin:
For the following hbm.xml I am creating a program:
+++++++++++++++++++++++++++++++++++
//Objective.hbm.xml
<hibernate-mapping package="bidirectionaltest2">
<class name="Objective" >
<id name="id" type="long">
<generator class="assigned" />
</id>
<property name="name" />
<set name="childObjectives" table="OBJECTIVE_MAPPING" lazy="true" cascade="all-delete-orphan">
<key column="objectiveId" />
<many-to-many class="Objective" column="id" />
</set>
</class>
</hibernate-mapping>
+++++++++++++++++++++++++++++++++++++
public void create(){
Dialect dialect = Dialect.getDialect();

//create table object
Table table = new Table();
table.setName("Objectives");
Column column_id = new Column("id");
Column column_name = new Column("name");

//create columns in the table
column_id.setSqlType(dialect.getTypeName(java.sql.Types.INTEGER));
column_name.setSqlType(dialect.getTypeName(java.sql.Types.VARCHAR));

//create values for the columns
SimpleValue value_int = new SimpleValue(table);
SimpleValue value_char = new SimpleValue(table);

//set the types to values
value_int.setTypeName(dialect.getTypeName(java.sql.Types.INTEGER));
value_char.setTypeName(dialect.getTypeName(java.sql.Types.VARCHAR));

//mapping values to columns
value_int.addColumn(column_id);
value_char.addColumn(column_name);

//Creating new Property "id" and set value type
Property property1 = new Property();
property1.setName("id");
property1.setValue(value_int);

//Creating new Property "name" and set value type
Property property2 = new Property();
property2.setName("Name");
property2.setValue(value_char);


//Creat Root class and set the class of interest which needs to be persisted
RootClass pClass = new RootClass();
pClass.setEntityPersisterClass(Objective.class);
//set table to the root class
pClass.setTable(table);
//Add properteis to the class
pClass.addProperty(property1);
pClass.addProperty(property2);

Table mappingTable = new Table();
mappingTable.setName("OBJECTIVE_MAPPING");


Set childObjectives = new Set(pClass);
childObjectives.setTypeName("childObjectives");
childObjectives.setCollectionTable(mappingTable);
childObjectives.setLazy(true);
childObjectives.setOrphanDelete(true);
// //....
// //cfg.createMappings().addClass(clazz);
}

I am stuck at defining a many-to-many association of entity with itself(refer to Objective.hbm.xml).Can somebody help me here ?

Mandar


Top
 Profile  
 
 Post subject: Re: programmatic mapping configuration
PostPosted: Thu Nov 08, 2012 5:47 am 
Newbie

Joined: Thu Nov 08, 2012 5:38 am
Posts: 3
Hi all...

Even though this thread is more than 4 years old, I'm wondering whether somebody was able to get this programmatic mapping configuration working. I tried to reproduce what kulkarnm had attempted, however, I had to change a few things since I'm using Hibernate 4.1. Unfortunately, even though the code is not bombing, it doesn't create anything at all in my database.

Code:
private void setup (Configuration p_Configuration, String p_TableName, String p_EntityName)
   {
   Dialect      l_Dialect;
   Column      lIdentifierColumn;
   Property      l_IdentifierProperty;
   SimpleValue   l_IdentifierValue;
   boolean      l_AlreadyMapped = false;
   
   Iterator <PersistentClass>   l_ClassMappingsIterator;
   PersistentClass         l_ClassMapping;
   
   m_Configuration = p_Configuration;
   l_Dialect = Dialect.getDialect (m_Configuration.getProperties());
   
   l_ClassMappingsIterator = m_Configuration.getClassMappings();
   while (l_ClassMappingsIterator.hasNext() && !l_AlreadyMapped)
      {
      l_ClassMapping  = l_ClassMappingsIterator.next();
      l_AlreadyMapped = l_ClassMapping.getMappedClass().equals(this.getClass());
      }
   
   if (!l_AlreadyMapped)
      {
      m_Mappings = m_Configuration.createMappings();
      m_Table    = new Table (p_TableName);
      
      l_IdentifierColumn = new Column (c_Identifier);
      l_IdentifierColumn.setSqlType(l_Dialect.getTypeName(java.sql.Types.INTEGER));
         
      l_IdentifierValue = new SimpleValue (m_Mappings,m_Table);
      l_IdentifierValue.setTypeName ("int");
      l_IdentifierValue.addColumn (l_IdentifierColumn);      
      
      l_IdentifierProperty = new Property();
      l_IdentifierProperty.setName (c_Identifier);
      l_IdentifierProperty.setValue (l_IdentifierValue);
      l_IdentifierProperty.setGeneration (PropertyGeneration.INSERT);
   
      m_Entity = new RootClass ();
      m_Entity.setEntityPersisterClass (DynamicEntityBean.class);
      m_Entity.setEntityName (p_EntityName);
      m_Entity.setJpaEntityName (p_EntityName);
      m_Entity.setTable(m_Table);   
      m_Entity.setIdentifier(l_IdentifierValue);
      m_Entity.setIdentifierProperty(l_IdentifierProperty);
      
      m_Mappings.addClass(m_Entity);      
      }


My hibernate.hbm2ddl.auto property in the hibernate.cfg.xml file is set to update.

Any idea what I'm missing ?

Many thanks in advance...


Top
 Profile  
 
 Post subject: Re: programmatic mapping configuration
PostPosted: Thu Nov 08, 2012 1:37 pm 
Newbie

Joined: Thu Nov 08, 2012 5:38 am
Posts: 3
Hi again...

Ok, I'm step further. I finally got Hibernate to recognize my mapping. In fact, I have to perform everything defined in my setup method before actually opening the session. Next, my dynamic bean had to extend from SingleTableEntityPersister, and last but not least, my table required a primary key. With hibernate in Debug mode, I see that Hibernate is validating my mapping, and it appears to pass. However, what still isn't working is the actual update of the database schema. As I already mentioned, my hibernate.hbm2ddl.auto property is set to update (also tried create to no avail). Any idea on this one ?

Regards...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.