-->
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: How to get rid of "hibernate.cfg.xml"?
PostPosted: Fri Apr 25, 2008 4:30 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi, I'm trying to do all configuration with in my java-files.

So I use addAnnotatedClass and addProperties of the
AnnotationConfiguration. Which works fine for the mappings, but it still want's to read the "hibernate.cfg.xml" via configure(). I haven't found a method yet, where I can avoid reading any file.

Cause of
Code:
HIBERNATE_PROPERTIES.setProperty("hibernate.mapping.precedence", "class");


a co existing is no problem, but I would like to avoid this. Especailly it would be easier and saver for Webserver deployment.

How can I do this?

Thanks a lot in advance.

Greetings Michael


Last edited by Urmech on Fri Apr 25, 2008 11:18 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 7:06 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
on which inplementation are you calling "configure();" ?
Probably not Ejb3Configuration ?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 8:09 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi Sanne, as mentioned above I'm calling configure on AnnotationConfiguration which extends org.hibernate.cfg.Configuration.

By the way:
Code:
private final static java.util.Properties HIBERNATE_PROPERTIES;
private final static Class<?>[] MAPPED_CLASSES;

private static void initMappedClasses(final AnnotationConfiguration aConfig) {
   for (final Class<?> toBeMapped : MAPPED_CLASSES) {
       aConfig.addAnnotatedClass(toBeMapped);
   }
    }

private static SessionFactory initFactory() {
   final AnnotationConfiguration aConfig = new AnnotationConfiguration();
   aConfig.addProperties(HIBERNATE_PROPERTIES);
   initMappedClasses(aConfig);
   return aConfig.configure().buildSessionFactory();
    }


So you mean I should use:

Ejb3Configuration
Code:
public Ejb3Configuration configure(javax.persistence.spi.PersistenceUnitInfo info,
                                   Map integration)

    Process configuration from a PersistenceUnitInfo object Typically called by the container
instead?

But how do i get a PersistenceUnitInfo ?
I have tried to figure that out by the API, but at
javax.persistence.Persistence I ran into a circle.

I want to get rid of any xml-file.

I believe I have found a solution in the meanwhile: All I have to do is to drop the configure-statement:
Code:
aConfig.configure().buildSessionFactory();

changes to:
Code:
aConfig.buildSessionFactory();


It seems to work. Is this correct?

Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 7:06 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
sorry I had understood you wanted to use persistence.xml, which doesn't require the hibernate.cfg.xml.
I don't know if it's possible (just guessing) to start it without any XML,
but why should you? It's just a single file and it's very useful to store
the configuration you most probably will need to change one day.

I am quite sure you'll need to call configure(), but never tried it.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 6:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Urmech wrote:
I believe I have found a solution in the meanwhile: All I have to do is to drop the configure-statement:
Code:
aConfig.configure().buildSessionFactory();

changes to:
Code:
aConfig.buildSessionFactory();


It seems to work. Is this correct?

Greetings Michael


Yes that's the way.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 9:37 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
emmanuel wrote:

Yes that's the way.


Hi Emmanuel,

thanks for the answer.

I got another question:
Can I control the logging of hibernate via the
addProperties method?

Where can I find a list of all allowed properties keys and values?

Thanks a lot in advance.

Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 10:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
logging depends on your logging framework
The list of properties are listed in the reference documentation. If not let us know

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 8:46 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi Emanuel,

in the reference manual most properties are explained in Chapter 3:
http://www.hibernate.org/hib_docs/v3/re ... ation.html

But I not sure if it is a complete list or not?

I thought it might be possible to change the debug-level without the need of a log4j.properties or programmatic configuration.

Thanks.

The example log4j.properties from the etc (not the src-dir) of the hibernate-distribution.

Code:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=warn, stdout

#log4j.logger.org.hibernate=info
log4j.logger.org.hibernate=debug

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace


so I converted this to:

Code:
/*
    * log4j for Hibernate. A ConsoleAppender and a RollingFileAppender (see
    * "Hibernate_error.log" are created.
    */
   final Logger hibernateRootLog = Logger.getLogger("org.hibernate");
   final PatternLayout hibernateLayout = new PatternLayout("%d{ABSOLUTE} %5p %c{1}:%L - %m%n");
   hibernateRootLog.addAppender(new ConsoleAppender(hibernateLayout));

   RollingFileAppender logForHibernate = null;
   try {
       logForHibernate = new RollingFileAppender(hibernateLayout, "Hibernate_error.log", true);
       logForHibernate.setMaxBackupIndex(1);
       logForHibernate.setMaxFileSize("128KB");
   }
   catch (final IOException e) {
       hibernateRootLog.warn("Can't write hibernate log file!");
   }
   if (logForHibernate != null) {
       hibernateRootLog.addAppender(logForHibernate);
   }

   hibernateRootLog.setLevel(Level.DEBUG);

   /*
    * log4j.logger.org.hibernate.type=info
    * #log4j.logger.org.hibernate.type=debug
    */
   Logger.getLogger("org.hibernate.type").setLevel(Level.INFO);

   /*
    * ### log schema export/update ###
    * log4j.logger.org.hibernate.tool.hbm2ddl=debug
    */
   Logger.getLogger("org.hibernate.tool.hbm2ddl").setLevel(Level.DEBUG);


I assume that hibernate cares to set these default properties, so this line should be enough, if you want to change only the debug level.

Code:
Logger.getLogger("org.hibernate").setLevel(Level.DEBUG);


Or for any other logger.

Greetings Michael


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.