yeah i know what you think that i didn't try resolve, check other topics etc. I read other topics but anywhere I can't find same problem as my.
Console give me this stacktrace:
20-Dec-2016 20:59:19.482 INFO [http-nio-8080-exec-7]
org.hibernate.cfg.Configuration.configure HHH000042: Configuring from file:
hibernate.cfg.xml
Enitial SessionFactory creation failedorg.hibernate.HibernateException:
could not find file: spring-mvc-angularjs-
master\src\main\java\com\user\springmvcangularjs\hibernate.cfg.xml
Well in first line i got information about that springe configuring from file hibernate.cfg.xml, but when i try connect with database i have error "could not find file".
I try all solutions but any helped me.
My code:
HibernateUtill class
public class HibernateUtil {
private static final SessionFactory ourSessionFactory;
private static final ServiceRegistry serviceRegistry;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Enitial SessionFactory creation failed" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return ourSessionFactory;
}
}
Method in UserService class:
#Override
public void save(User user)
{
HibernateUtil hibernateUtil = new HibernateUtil();
SessionFactory sessFact = hibernateUtil.getSessionFactory();
Session session = sessFact.getCurrentSession();
org.hibernate.Transaction tr = session.beginTransaction();
System.out.println(tr.isActive());
session.save(user);
tr.commit();
sessFact.close();
}
PS I have hibernate.cfg.xml file in spring-mvc-angularjs-master/src/main/java
i saw a lot of example of using hibernate, in particular the session Factory, programmatically
I used this:
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration()
.configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
but in my current project i need some different implementation so i removed the FINAL for the sessionFactory constant and i implemented a constructor in order to inizialize this when it is null.
so i have an hibernateUtil abstrac class whit only the SYNCHRONIZED method buildSessionFactory and an implementation class whit this constructor
public myHibernateUtil(){
if (mySessionFactory == null){
mySessionFactory = buildSessionFactory();
}
}
Is it safe?? In your opinion by removing the FINAL for the session factory it is possible to have problem with the number of session and saturate the datasource??
I make this question because i'm getting a strange error for weblogic:
Pool myDatasource is Suspended, cannot allocate resources to applications..
Thanks to all!
EDIT:
Into the Server1-diagnostic.log there are those exception :
[2014-05-21T18:46:41.428+02:00] [Server_1] [NOTIFICATION] [DFW-40101] [oracle.dfw.incident] [tid: [ACTIVE].ExecuteThread: '12' for queue: 'weblogic.kernel.Default (self-tuning)'] [userId: ] [ecid: 356fce390f623850:-983a53a:1461f7d7cb4:-8000-0000000000000b1e,0] An incident has been signalled with the incident facts: [problemKey=BEA-337 [WebLogicServer] incidentSource=SYSTEM incidentTime=Wed May 21 18:46:41 CEST 2014 errorMessage=BEA-337 executionContextId=356fce390f623850:-983a53a:1461f7d7cb4:-8000-0000000000000995]
[2014-05-21T18:46:42.927+02:00] [Server_1] [NOTIFICATION] [DFW-40104] [oracle.dfw.incident] [tid: [ACTIVE].ExecuteThread: '12' for queue: 'weblogic.kernel.Default (self-tuning)'] [userId: ] [ecid: 356fce390f623850:-983a53a:1461f7d7cb4:-8000-0000000000000b1e,0] [errid: 84] [detailLoc: /app/oss/bea-domains/osssbdomain/servers/Server_1/adr/diag/ofm/osssbdomain/Server_1/incident/incdir_84] [probKey: BEA-337 [WebLogicServer]] incident 84 created with problem key "BEA-337 [WebLogicServer]"
Moreover i found a lot of error like this in the server1.log:
Tried all: '1' addresses, but could not connect over HTTP to server: '127.0.0.1', port: '7333'
in the communication beetween oracle service bus and my app.
Thanks!
I am creating a jar using hibernate. I have encountered a situation where I need to change a setting (url) often, so I would like to load the hibernate.cfg.xml like this
SessionFactory sessionFactory = new Configuration()
.configure("D:\\fax\\hibernate.cfg.xml")
.buildSessionFactory();
But then running the project I am getting this exception
org.hibernate.HibernateException: D:\fax\hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1287)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1309)
at hibernate.LabOrderHelper.getDatabaseSesssion(LabOrderHelper.java:55)
at hibernate.Test.main(Test.java:42)
How can I load hibernate.cfg.xml from a different location than the class path?
There is a method public Configuration configure(File configFile) in class Configuration
Try the following, it should work for sure :)
File f = new File("D:\\fax\\hibernate.cfg.xml");
SessionFactory sessionFactory = new Configuration().configure(f).buildSessionFactory();
The difference is you have used a method configure(String resource) which is expecting a resource in a classpath, but where as configure(File configFile) is expecting a File, so you can pass it.
I need to change the sql setting(url) often
I had the same requirement. For switching just the DB connection properties the approach suggested in the accepted answer, though it works, is a bit of a blunt instrument.
Loading a completely different configuration file just to change a few connection properties? Now all the other properties that are common in both are duplicated, and every time you make a change you need to make it in two places.
A better way is to put all the common properties that don't need to change between environments in the default hibernate.cfg.xml, build your Configuration from that as usual, and use the .addProperties() method to add the properties that are environment-specific on top, in this case the connection url. You can load these extra properties from anywhere you like.
public SessionFactory buildSessionFactory() {
return getConfiguration().buildSessionFactory();
}
private Configuration getConfiguration() {
Configuration config = new Configuration.configure(); // load the base config from the default hibernate.cfg.xml
return config.addProperties(getConnectionProperties()); // add your custom connection props for this environment on top
}
private Properties getConnectionProperties() {
Properties connectionProps = new Properties();
connectionProps.put("hibernate.connection.url", getConnectionUrl());
// possibly add other props like hibernate.connection.username, hibernate.connection.password
return connectionProps;
}
private String getConnectionUrl() {
// get your connection URL from wherever you like
}
Hibernate XML configuration file “hibernate.cfg.xml” is always put at the root of your project classpath, outside of any package. If you place this configuration file into a different directory, you may encounter the following error :
Initial SessionFactory creation failed.org.hibernate.HibernateException:
/hibernate.cfg.xml not found
To ask Hibernate look for your “hibernate.cfg.xml” file in other directory, you can modify the default Hibernate’s SessionFactory class by passing your “hibernate.cfg.xml” file path as an argument into the configure() method:
SessionFactory sessionFactory = new Configuration()
.configure("/com/example/persistence/hibernate.cfg.xml")
.buildSessionFactory();
return sessionFactory;
Full Example in HibernateUtil.java, to load “hibernate.cfg.xml” from directory “/com/example/persistence/“.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// load from different directory
SessionFactory sessionFactory = new Configuration().configure(
"/com/example/persistence/hibernate.cfg.xml")
.buildSessionFactory();
return sessionFactory;
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
supplementing the accepted answer,
You can load hibernate.cfg.xml from a different directory (not necessarily the classpath) using the configure(File configFile) method that takes the hibernateConfig File argument.
(note, am using hibernate 4.3.7)
The advantage is, if you have no access to the war file but can get access to the hibernate file in a different directory, say for maintenance.
Like this:
String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";
File hibernatePropsFile = new File(hibernatePropsFilePath);
Configuration configuration = new Configuration();
configuration.configure(hibernatePropsFile);
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
I' m developing a simple JSF application which uses Hibernate. I imported all required libraries to WEB-INF/lib folder and also point them in classpath. But when I tried to compile it I got error:
Here is code where I create SessionFactory and use it:
private static SessionFactory buildSessionFactory()
{
try
{
Configuration configuration = new Configuration();//This line
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry( );
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Exception e)
{
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory()
{
return buildSessionFactory();
}
And here I use it:
public static void saveUser( String title)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Client client = new Client();
......
So what am I doing wrong? How can I fix this?
The javax.transaction.SystemException is in the jta-x.y.jar (x.y is the required version for the version of Hibernate you use). It should be in your classpath.
Hibernate requires a lot of libraries. To manage the dependencies, you should use something like Maven or Ivy.
i am creating a HibernateUtil.java class to return SessionFactory Object. but it's giving Error "The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class". my code is as --
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
#SuppressWarnings("deprecation")
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
it seems you didn't include jre system liberary in build path.
Right click on project --> Properties --> select Java Build Path --> Libraries -->
Add Library --> JRE System Library --> Next --> Workspace default JRE --> Finish