I'm trying to deal with the java web application.

I use Intellij IDEA

First, I wrote a small console application in which the database was accessed, the data was sampled and the result was output to the console. At first made through JDBC then remade through Hibernate

Then I decided to remake it into a web application, created a Maven-ovsky web project and threw the HiberUtil packages and the Main class into the java folder, and put the hiber config files into the xml files. Those. I just threw the files of the console application as they were in the previous project.

Connected Tomcat to the Idea, wrote servlets, jsp. The result was the following structure:

project structure

The problem is that not Hiber does not pick up configs in configure()

Tomcat message

Having thrown the hibernate.cfg.xml file into the webapp hyber starts up, but does not pick up the configs for the classes. Correspondingly, if you copy class configs, the classes themselves are not picked up.

By rewriting everything back through JDBC I get a working application.

Please explain how the files should be located correctly or where to read about the project structure.

Update

 "C:\Program Files\apache-tomcat-8.0.33\bin\catalina.bat" run [2016-04-29 11:39:48,307] Artifact test2LSwebapp:war exploded: Server is not connected. Deploy is not available. Using CATALINA_BASE: "C:\Users\emina\.IntelliJIdea15\system\tomcat\Unnamed_test2LSwebapp_2" Using CATALINA_HOME: "C:\Program Files\apache-tomcat-8.0.33" Using CATALINA_TMPDIR: "C:\Program Files\apache-tomcat-8.0.33\temp" Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_77\jre" Using CLASSPATH: "C:\Program Files\apache-tomcat-8.0.33\bin\bootstrap.jar;C:\Program Files\apache-tomcat-8.0.33\bin\tomcat-juli.jar" 29-Apr-2016 11:39:50.769 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.33 29-Apr-2016 11:39:50.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 18 2016 20:31:49 UTC 29-Apr-2016 11:39:50.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.33.0 29-Apr-2016 11:39:50.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 10 29-Apr-2016 11:39:50.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 10.0 29-Apr-2016 11:39:50.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 29-Apr-2016 11:39:50.772 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: C:\Program Files\Java\jdk1.8.0_77\jre 29-Apr-2016 11:39:50.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_77-b03 29-Apr-2016 11:39:50.775 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 29-Apr-2016 11:39:50.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\emina\.IntelliJIdea15\system\tomcat\Unnamed_test2LSwebapp_2 29-Apr-2016 11:39:50.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: C:\Program Files\apache-tomcat-8.0.33 29-Apr-2016 11:39:50.776 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote= 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\emina\.IntelliJIdea15\system\tomcat\Unnamed_test2LSwebapp_2\conf\logging.properties 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.endorsed.dirs=C:\Program Files\apache-tomcat-8.0.33\endorsed 29-Apr-2016 11:39:50.777 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\emina\.IntelliJIdea15\system\tomcat\Unnamed_test2LSwebapp_2 29-Apr-2016 11:39:50.778 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Program Files\apache-tomcat-8.0.33 29-Apr-2016 11:39:50.778 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Program Files\apache-tomcat-8.0.33\temp 29-Apr-2016 11:39:50.778 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.5 using APR version 1.5.1. 29-Apr-2016 11:39:50.779 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 29-Apr-2016 11:39:51.829 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.0.2g 1 Mar 2016) 29-Apr-2016 11:39:52.124 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 29-Apr-2016 11:39:52.177 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 29-Apr-2016 11:39:52.180 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 2049 ms 29-Apr-2016 11:39:52.228 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 29-Apr-2016 11:39:52.228 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.33 29-Apr-2016 11:39:52.241 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 29-Apr-2016 11:39:52.254 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 29-Apr-2016 11:39:52.257 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 76 ms Connected to server [2016-04-29 11:39:52,697] Artifact test2LSwebapp:war exploded: Artifact is being deployed, please wait... 29-Apr-2016 11:39:54.558 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. [2016-04-29 11:39:54,670] Artifact test2LSwebapp:war exploded: Artifact is deployed successfully [2016-04-29 11:39:54,670] Artifact test2LSwebapp:war exploded: Deploy took 1 973 milliseconds 29-Apr-2016 11:40:02.264 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Program Files\apache-tomcat-8.0.33\webapps\manager 29-Apr-2016 11:40:02.350 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Program Files\apache-tomcat-8.0.33\webapps\manager has finished in 85 ms 29-Apr-2016 11:40:13.911 INFO [http-apr-8080-exec-10] org.hibernate.Version.logVersion HHH000412: Hibernate Core {5.1.0.Final} 29-Apr-2016 11:40:13.915 INFO [http-apr-8080-exec-10] org.hibernate.cfg.Environment.<clinit> HHH000206: hibernate.properties not found 29-Apr-2016 11:40:13.918 INFO [http-apr-8080-exec-10] org.hibernate.cfg.Environment.buildBytecodeProvider HHH000021: Bytecode provider name : javassist 29-Apr-2016 11:40:14.727 INFO [http-apr-8080-exec-10] org.hibernate.annotations.common.reflection.java.JavaReflectionManager.<clinit> HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 29-Apr-2016 11:40:16.545 WARN [http-apr-8080-exec-10] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure HHH10001002: Using Hibernate built-in connection pool (not for production use!) 29-Apr-2016 11:40:16.560 INFO [http-apr-8080-exec-10] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:~/test] 29-Apr-2016 11:40:16.560 INFO [http-apr-8080-exec-10] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator HHH10001001: Connection properties: {user=sa, password=****} 29-Apr-2016 11:40:16.561 INFO [http-apr-8080-exec-10] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator HHH10001003: Autocommit mode: false 29-Apr-2016 11:40:16.569 INFO [http-apr-8080-exec-10] org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init> HHH000115: Hibernate connection pool size: 20 (min=1) 29-Apr-2016 11:40:17.578 INFO [http-apr-8080-exec-10] org.hibernate.dialect.Dialect.<init> HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 29-Apr-2016 11:40:19.848 INFO [http-apr-8080-exec-10] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.stop HHH10001008: Cleaning up connection pool [jdbc:h2:~/test] 

ran install on Maven, got the war file, entered it: war file

updated his project and corrected the names of configs update

HiberUtil.java

package HibernateUtils;

 import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HiberUtil { private static SessionFactory sessionFactory = null; static { sessionFactory = new Configuration().configure().buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sessionFactory; } } 
  • one
    It is worth smoking manuals gibernata. - Sergey

1 answer 1

the problem is that not heber does not pick up configs in configure ()

The error that is in the screenshot is not related to configuration, as this error occurs during the execution of the database query. Please, next time bring the full spectrum, not a screenshot.

Regarding the location of files. When you do new Configuration().configure() Hibernate tries to load hibernate.cfg.xml through ClassLoader.getResourceAsStream(resourcePath) resourcePath is the path to the resource.

If it is simplistic (without considering the full classpath), then in the web application the classifier tries to access the file from war/WEB-INF/classes . For example:

configure("hibernate.cfg.xml") — war/WEB-INF/classes/hibernate.cfg.xml configure("xxx/hibernate.cfg.xml") — war/WEB-INF/classes/xxx/hibernate.cfg.xml

Those files referenced by hibernate.cfg.xml (your configs for classes) are also loaded by the classifier, according to the same rules (it’s customary to give them the postfix hbm.xml )

 <mapping resource="hibernate/fileStatistic.hbm.xml"></mapping> 

corresponds to war/WEB-INF/classes/hibernate/fileStatistic.hbm.xml

Interestingly, for the classifier, the path to the file should not begin with / , but Hibernate takes this into account and tries to delete the leading one / if the boot is unsuccessful, therefore the path /hibernate/fileStatistic.hbm.xml is also correct.

If you use the Maven build, for the console application, Maven places the contents of the resources in the same place as the compiled src contents — to the root of the bin or build folder, or to the jar root. For a web application, Maven places the contents of resources in war/WEB-INF/classes . If everything is properly configured. Therefore, you need to see in war what you have in war/WEB-INF/classes .

Here is a method that loads resources in Hibernate

 public InputStream locateResourceStream(String name) { // first we try name as a URL try { return new URL( name ).openStream(); } catch (Exception ignore) { } try { final InputStream stream = getAggregatedClassLoader().getResourceAsStream( name ); if ( stream != null ) { return stream; } } catch (Exception ignore) { } final String stripped = name.startsWith( "/" ) ? name.substring( 1 ) : null; if ( stripped != null ) { try { return new URL( stripped ).openStream(); } catch (Exception ignore) { } try { final InputStream stream = getAggregatedClassLoader().getResourceAsStream( stripped ); if ( stream != null ) { return stream; } } catch (Exception ignore) { } } return null; } 

Update

In war there should not be a resources folder, or you will need to specify it in the paths to the configuration files.

You can check if the mapping file is available (or hibernate.cfg.xml ) by running the following code before the error occurs

 URL url = Thread.currentThread().getContextClassLoader() .getResource( "hibernate/fileStatistic.cfg.xml"); System.out.println(url); 

Update 2

Apparently, Hibernate finds configuration files. The error in the screenshot is due to sessionFactory.close() this line must be removed. You need to add logging to understand what is happening. Add to pom.xml

 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> 

Turn on SQL logging in Hibernate, and place the log4j.properties file next to hibernate.cfg.xml . Turn on schema creation to see how the tables are cleaned.

 <property name = "hbm2ddl.auto">create</property> 

I’ve added something like this, it will work only when re-creating a scheme

 session.beginTransaction(); FileStatistic st = new FileStatistic(); st.setIdFile(1); session.save(st); session.flush(); Criteria criteriaFile = session.createCriteria(FileStatistic.class); fileStatistic = criteriaFile.list(); session.getTransaction().commit(); 
  • I tried to build Maven, gave an error of incompatibility of the compiler with try-with-resources. Corrected the pom-file, so that it was going to be in version 1.8, everything went without errors. I checked the war-file, the files lay in folders as in the source. Xml-files for classes are in the same folder as the classes themselves - Prahvessor
  • @Prahvessor Please add examples of paths to war and a full stack trace error in the question. - v.ladynev
  • I updated the question - Prahvessor
  • @Prahvessor In hibernate.cfg.xml paths to hbm.xml should be StatisticBeans/fileStatistic.hbm.xml . A where is hibernate.cfg.xml turned out to be? - v.ladynev
  • it is one level higher, it is written <mapping resource="StatisticBeans/fileStatistic.hbm.xml"/> <mapping resource="StatisticBeans/lineStatistic.hbm.xml"/> - Prahvessor