-->
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: Calling hbm2ddl from an applicationi
PostPosted: Fri Apr 15, 2005 3:44 pm 
Newbie

Joined: Thu Apr 14, 2005 2:13 pm
Posts: 4
I am trying to call hbm2ddl from an appllication as per p. 355 in "Hibernate in Action". But no tables get generated. Can someone help. TIA

Configuration File- (hibernate.cfb.xml):
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.connection.url">jdbc:hsqldb:xcvrdb</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping resource="org/barenakedobjects/framework/model/Project.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Hibernate version: hibernate-3.0

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.barenakedobjects.framework.model.Project" table="`PROJECTS`" lazy="false">
<id name="ID" type="string">
<column name="project_id" sql-type="char(32"/>
<generator class="uuid.hex"/>
</id>
<property name="name" type="string">
<column name="NAME"
not-null="true"
length="255"/>
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
SessionFactory sessions = new Configuration()
.configure()
.buildSessionFactory();
Session session = sessions.openSession();
Project project = new Project("MyFirstProject");
Transaction tx = session.beginTransaction();
session.save(project);
tx.commit();
session.close();


Full stack trace of any exception that occurs: Hibernate: insert into "PROJECTS" (NAME, project_id) values (?, ?)
14:33:53,212 WARN JDBCExceptionReporter:57 - SQL Error: -22, SQLState: S0002
14:33:53,222 ERROR JDBCExceptionReporter:58 - Table not found: PROJECTS in statement [insert into "PROJECTS" (NAME, project_id) values ('MyFirstProject', '2c9081a103475a550103475a58e00001')]
14:33:53,227 ERROR AbstractFlushingEventListener:277 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: could not insert: [org.barenakedobjects.framework.model.Project]
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1882)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2214)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.barenakedobjects.framework.db.TestMyDB.<init>(TestMyDB.java:25)
at org.barenakedobjects.framework.db.TestMyDB.main(TestMyDB.java:33)


Name and version of the database you are using:HSQLDB 1.7.3


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 4:23 am 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
A) Enter
Code:
<property name="hbm2ddl">create</property>


in the hibernate configuration.

or,

B) when you constuct the session factory:

Code:
Configuration c = new Configuration();
c.configure();
(new SchemaUpdate(c)).execute(false, true));
SessionFactory sessions = c.buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 6:20 am 
Newbie

Joined: Thu Apr 14, 2005 2:13 pm
Posts: 4
The Addition of the line
Code:
<property name="hbm2ddl">create</property>


causes this error:
05:13:02,725 ERROR XMLHelper:59 - Error parsing XML: XML InputStream(18) The content of element type "property" must match "(meta*,(column|formula)*,type?)".
Exception in thread "main" org.hibernate.MappingException: Error reading resource: org/barenakedobjects/framework/model/Project.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:399)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at org.barenakedobjects.framework.db.TestMyDB.<init>(TestMyDB.java:19)
at org.barenakedobjects.framework.db.TestMyDB.main(TestMyDB.java:39)
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:346)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:396)
... 8 more
Caused by: org.xml.sax.SAXParseException: The content of element type "property" must match "(meta*,(column|formula)*,type?)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:334)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:345)
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 8:00 am 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
Add it to the hibernate configuration file, not the entity mapping file.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 8:04 am 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
Also see The documentation for details of this switch, table 3.7 row 3. (just above the start of section 3.4.1)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 12:18 pm 
Newbie

Joined: Thu Apr 14, 2005 2:13 pm
Posts: 4
I appreciate your help, but it seems as though the table is still not being created in the db by SchemaExport.

I added the line
<property name="hbm2ddl">create</property>
to the hibernate.cfg.xml file, and get this error when I try to persist my object:

Hibernate: insert into "projects" (NAME, project_id) values (?, ?)
11:15:53,989 WARN JDBCExceptionReporter:57 - SQL Error: -22, SQLState: S0002
11:15:53,999 ERROR JDBCExceptionReporter:58 - Table not found: projects in statement [insert into "projects" (NAME, project_id) values ('MyFirstProject', '2c9081a103561827010356182a3c0001')]
11:15:54,005 ERROR AbstractFlushingEventListener:277 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: could not insert: [org.barenakedobjects.framework.model.Project]
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1882)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2214)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.barenakedobjects.framework.db.TestMyDB.<init>(TestMyDB.java:30)
at org.barenakedobjects.framework.db.TestMyDB.main(TestMyDB.java:38)
Caused by: java.sql.SQLException: Table not found: projects in statement [insert into "projects" (NAME, project_id) values ('MyFirstProject', '2c9081a103561827010356182a3c0001')]
at org.hsqldb.Trace.getError(Unknown Source)
at org.hsqldb.jdbcResultSet.<init>(Unknown Source)
at org.hsqldb.jdbcConnection.executeStandalone(Unknown Source)
at org.hsqldb.jdbcConnection.execute(Unknown Source)
at org.hsqldb.jdbcStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source)
at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1866)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 12:36 pm 
Regular
Regular

Joined: Wed Feb 02, 2005 6:33 am
Posts: 70
hmm, guess I should read the documentation as well :)

the property name is actually 'hbm2ddl.auto', not 'hbm2ddl'. Hope that resolves it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 18, 2005 12:53 pm 
Newbie

Joined: Thu Apr 14, 2005 2:13 pm
Posts: 4
Success! thanks muchly.

<property name="hbm2ddl.auto">create</property>
did the trick.

I also had a missing ")" in my *.hbm.xml file.

I'm assuming that I can use
<property name="hbm2ddl.auto">create-drop</property>
if I want to drop the table and then recreate it.

thanks again.


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.