I need to create a session factory for hibernate, but by default it looks for "hibernate.cfg.xml" file. I did not create this file. All hibernate config is in "applicationContext.xml" file of Spring MVC config. All I need to do is provide "/WEB-INF/applicationContext" file path to the "configure" method of Configuration class, but I don't know how find the relative path to this file in JAVA.
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure(HERE I NEED TO GET "/WEB-INF/applicationContext.xml FILE PATH");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory; }
Hibernate loads /WEB-INF/applicationContext.xml using ClassLoader.getResourceAsStream("/WEB-INF/applicationContext.xml")
ClassLoader tries to get access to the files in the root of the classpath — for the web-applications it is war/WEB-INF/classes/. You should put applicationContext.xml to the war/WEB-INF/classes/applicationContext.xml and load it using
sessionFactory = new Configuration().configure("applicationContext.xml")
.buildSessionFactory();
You can't specify /WEB-INF/applicationContext.xml, because of /WEB-INF/ is not in the class path.
How Hibernate loads resources
If you really want to get a configuration from /WEB-INF/applicationContext.xml you should get URL of applicationContext.xml using ServletContext: File path to resource in our war/WEB-INF folder?
And pass that URL to the Configuration or StandardServiceRegistryBuilder, it depends of Hibernate version.
An important notice
I think, above is senseless, because of applicationContext.xml should has a structure like hibernate.cfg.xml and it, obviously, doesn't.
The file should be in your classpath, so you should be able to reference it using the classpath variable: classpath:/WEB-INF/applicationContext.xml
If this won't work then you load the file using class loader and then "ask" for the full path ( this should not be necessary though... ):
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource( "WEB-INF/applicationContext.xml" );
String fullPath = resource.getPath()
Related
In my Spring proejct, I created a config.conf to store some variables.
And in a class I need to read these variables.
I am using the config library, https://github.com/lightbend/config.
And as the document says, I tried to used
private static final Config config = ConfigFactory.load(basename here goes here);
to load that config.conf file.
And by the document, https://lightbend.github.io/config/latest/api/com/typesafe/config/ConfigFactory.html#load-java.lang.String-, this method receives a string as classpath resource basename.
I am wondering how to get this "classpath resource basename" of the config.conf file?
My project's structure:
Thanks!
you can access file like this.
URL url = EmailService.class.getClassLoader().getResource("config.conf");
to load config, you can access path like below
config.load(url.getPath());
I created an spring xml config file with
<util:properties
id="com.abc.xyz.handler.abchandler"
location="classpath:/properties/myhandler_${spring.profile.active:e3}.properties" />
now from my java program when I am trying to get the property file like below:
Properties props = ((Properties).getBean(getClass().getName()));
it is saying NO bean with the name com.abc.xyz.handler.abchandler has been defined.
Please help !!
From your supplied code you are using getBean() but you have not passed in the properties file. Moreover, you have not declared the properties file as a bean in the spring-xml config file.
You could try accessing the properties file in this way:
Resource resource = new ClassPathResource("nameOfYourPropertiesFile.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
I have to configure two database on same project by configuring two CFG file,
I tried but it is always use the first configuration file,
May i know how can i use two database on same project
In your code, what you need to do is to open two different session factory for different databases.
For example:
Configuration configA=new Configuration();//use the default hibernate.cgf.xml file
Congiruration configB=new Configuration.configure('/hibernate_db2.cfg.xml') // use hibernate_db2.cfg.xml under root folder.
SessionFactory sfa=configA.buildSessionFactory();
SessionFactory sfb=configB.buildSessionFactory();
Now, you open different session using different db.
You need to have two configuration files.
hibernate-mysql.cfg.xml
hibernate-oracle.cfg.xml
And code should be like this.
mysql configuration
private static SessionFactory sessionAnnotationFactory;
sessionAnnotationFactory = new Configuration().configure("hibernate-mysql.cfg.xml").buildSessionFactory();
Session session = sessionAnnotationFactory.openSession();
oracle sql configuration
sessionAnnotationFactory = new Configuration().configure("hibernate-oracle.cfg.xml").buildSessionFactory();
Session session = sessionAnnotationFactory.openSession()
I'm trying to change cfg properties at runtime.
For example:
cfg.setProperty("hibernate.connection.url")
The problem is that it works only when this property is not defined in the cfg file itself,
meaning, it doesn't override.
Can it be done in some way?
when you run
Configuration cfg = new Configuration().configure();
the .configure() reads the data from the XML, and it has a higher priority over the programmatic configuration.
However, if you remove the .configure, all the configuration will be "read" from the settings that you will pass. For example:
Configuration configuration = new Configuration()
.setProperty( "hibernate.connection.driver_class", "org.postgresql.Driver" )
.setProperty( "hibernate.dialect","org.hibernate.dialect.PostgreSQLDialect")
[...snip...]
.addAnnotatedClass( com.myPackage.MyClass.class )
[...] ;
will set all the properties at runtime.
I have a properties file which I have put in the classpath and I am trying to load it from a JSP:
InputStream stream = application.getResourceAsStream("/alert.properties");
Properties props = new Properties();
props.load(stream);
But I am getting a FileNotFoundException.
The ServletContext#getResourceAsStream() returns resources from the webcontent, not from the classpath. You need ClassLoader#getResourceAsStream() instead.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
properties.load(classLoader.getResourceAsStream("filename.properties"));
// ...
That said, it is considered bad practice to write raw Java code like that in a JSP file. You should be doing that (in)directly inside a HttpServlet or maybe a ServletContextListener class.