-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to convert *.hbm.xml xml to Java code, avoiding all xml?
PostPosted: Wed Aug 26, 2009 7:54 pm 
Newbie

Joined: Thu Nov 13, 2008 1:46 am
Posts: 6
Within Hibernate 2, I am attempting to use the Configuration and Mappings object
in a Java class so that I don't use hibernate.cfg.xml and Object.hbm.xml
files. I have succeeded with hibernate.cfg.xml:

//***************************************************************************************

import org.hibernate.cfg.*;
import org.hibernate.util.*;

Configuration configuration = new Configuration();
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
properties.setProperty("hibernate.connection.url","jdbc:mysql://localhost/hibernatetutorial");
properties.setProperty("hibernate.connection.username","root");
properties.setProperty("hibernate.connection.password","password");
properties.setProperty("hibernate.connection.pool_size","10");
properties.setProperty("show_sql","true");
properties.setProperty("dialect","org.hibernate.dialect.MySQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto","update");
configuration.addProperties(properties);

Mappings mappings = configuration.createMappings();
HibernateObjectMapping object = new HibernateObjectMapping();
object.setMappingsObject();
Mappings = object.getMappingsObject();
object.buildMappings();

//....hbm.xml code.
SessionFactory factory = configuration.configure().buildSessionFactory();
Session session = factory.getCurrentSession();
Object object = (Object)session.load(Object.class,1);
//...

session.save(object);
factory.close();
//***************************************************************************************

but am having trouble with Object.hbm.xml, upon the Mappings Object.
I want the contents for these xml files in memory from Java code, not the XML files.
Ie. I DO want to hard configuration and mappings in Java code, as per my style above.
I know about what the tutorial says at

http://docs.jboss.org/hibernate/stable/ ... asses.html

and while the javadoc is some help
however, I still struggle to see through this, one xml tag/java code line at a time.
Could someone kindly advise/point me in the right direction
for programming of an Object.hbm.xml in java code?
In other terms, how do I successfully relate in what
one sees, in, say:

//*********************************************************88
- <hibernate-mapping>
- <class name="events.Person" table="PERSON">
- <id name="id" column="PERSON_ID">
<generator class="native" />
</id>
<property name="age" />
<property name="firstname" />
<property name="lastname" />
</class>
</hibernate-mapping>
//*********************************************************88

by programming with the Mappings class, and any other(s)?
[I do NOT want to know how to convert Object.hbm.xml to a Pojo Class,
rather I want to convert the mapping instructions themselves.]

:) ?


Top
 Profile  
 
 Post subject: Re: How to convert *.hbm.xml xml to Java code, avoiding all xml?
PostPosted: Wed Sep 16, 2009 5:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I don't understand what you are trying to do or what your problem is.

If you don't want to add hbm.xml files manually then use Annotations
where the mappings are in your classes.

And if that is not good enough for you the Hibernate entitymanager will
actually automatically load hbm.xml files for you (assuming they are named
the same as the java class, i.e. org/model/Person.java and org/model/Person.hbm.xml)

_________________
Max
Don't forget to rate


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