-->
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.  [ 4 posts ] 
Author Message
 Post subject: Generate schema from EJB3 persistence.xml?
PostPosted: Tue Jul 12, 2005 5:20 pm 
Newbie

Joined: Thu Feb 24, 2005 1:44 pm
Posts: 11
I want to capture the DDL during the build (rather than just auto update). I didn't see an ant task for this that was based on annotations and the persistence.xml. Did I miss something?

I ended up copying a lot of the code out of HibernatePersistence that creates and configures the AnnotationConfiguration, so that I could use that Configuration object in a call to SchemaExport.

Can someone clue me in on the long-term plans, if there are any?

Thanks!

- Jason


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 5:50 pm 
Beginner
Beginner

Joined: Tue Mar 15, 2005 2:36 am
Posts: 32
Location: Bonn, Germany
Hi Jason,

maybe those potentially long-term plans can realized in one go, when this issue is addressed:
http://forum.hibernate.org/viewtopic.php?t=944931
http://opensource.atlassian.com/projects/hibernate/browse/EJB-35

Possible solutions are:

1. (dirty) Bind the Configuration object to a well-known place like System.getProperties() or a JNDI context by default.

2. Implement an empty and protected (visitor-pattern?) "customize(AnnontationConfiguration cfg)" method in HibernatePersistence. Subclasses can override that method and do something like in 1. Those subclasses are referred to as the provider in the "persistence.xml".

3. (dirty too) Offer a pre-configured AnnoCfg to the HibernatePersistence.

or

4. If you just want to fire an event (call it "wipe-all-data-from-my-storage-and-create-fresh-tables") at runtime, you can re-read the entire "persistence.xml" and pass a local override setting for the hbm2ddl tool:
Code:
  Map map = new HashMap();
  map.put("hibernate.hbm2ddl.auto", "create");
  Persistence.createEntityManagerFactory("Manager1", map);

Ok - not too elegant, but no code duplication. :)

Cheers and voting for 2.,
Christian


Top
 Profile  
 
 Post subject: Re: Generate schema from EJB3 persistence.xml?
PostPosted: Fri Jul 15, 2005 2:18 am 
Beginner
Beginner

Joined: Tue Mar 15, 2005 2:36 am
Posts: 32
Location: Bonn, Germany
jsando wrote:
...
I ended up copying a lot of the code out of HibernatePersistence that creates and configures the AnnotationConfiguration, so that I could use that Configuration object in a call to SchemaExport. ...


This underhand usage of AnnotationConfiguration byHibernatePersistence hides another problem. Imagine, you set the auto-update property like this...
Code:
<property name="hibernate.hbm2ddl.auto" value="update" />

...and let javax Persistence create the factory without having launched the database. This leads to the following stack trace:
Code:
2005-07-15 07:54:03,720 ERROR [main] org.hibernate.tool.hbm2ddl.SchemaUpdate - could not get database metadata
java.sql.SQLException: socket creation error
   at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
   at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
   at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
   at org.hsqldb.jdbcDriver.connect(Unknown Source)
   at java.sql.DriverManager.getConnection(DriverManager.java:525)
   at java.sql.DriverManager.getConnection(DriverManager.java:140)
   at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
   at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:118)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:981)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:182)
   at org.hibernate.ejb.HibernatePersistence.createFactory(HibernatePersistence.java:77)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:93)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:42)

...

2005-07-15 07:54:03,720 ERROR [main] org.hibernate.tool.hbm2ddl.SchemaUpdate - could not complete schema update
java.sql.SQLException: socket creation error
   at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
   at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
   at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
   at org.hsqldb.jdbcDriver.connect(Unknown Source)
   at java.sql.DriverManager.getConnection(DriverManager.java:525)
   at java.sql.DriverManager.getConnection(DriverManager.java:140)
   at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
   at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:118)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:981)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:182)
   at org.hibernate.ejb.HibernatePersistence.createFactory(HibernatePersistence.java:77)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:93)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:42)


It's printed twice - but the excpetion(s) are collected by the SchemaUpdate tool only. You have no chance to react on those troubles in common way. Or am I missing something.

This is my test code snippet, where I expect the catch()-block to be executed if something fails while factory creation:
Code:
    try {
      EntityManagerFactory factory;
      factory = Persistence.createEntityManagerFactory("Man", null);
      // factory = new HibernatePersistence().createEntityManagerFactory(map);
      LOG.debug("Created instance of '" + factory.getClass() + "' as entity"
          + " manager factory.");
      return factory;
    }
    catch (Throwable cause) {
      LOG.fatal("Creating entity manager factory failed!", cause);
      throw new Error(cause);
    }


New JIRA issue?

Cheers,
Sormuras


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 2:40 am 
Beginner
Beginner

Joined: Tue Mar 15, 2005 2:36 am
Posts: 32
Location: Bonn, Germany
No JIRA issue yet, but started a thread in Tools http://forum.hibernate.org/viewtopic.php?t=945096 ... because it's more Tool-related.


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