Hibernate sessionfactory nullpointerexception - java

I'm practicing on hibernate. I'm trying to connect local mysql server and get some information but have some error messages on my console. I debugged my code and found the initial part when I'm trying to create a session factory
Configuration configuration = new Configuration();
this part of my code to cause an java.lang.NullPointerException error. I'm pretty sure I have import the all hibernate jars.
hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
'-//Hibernate/Hibernate Configuration DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>
<hibernate-configuration>
<session-factory>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name='dialect'>org.hibernate.dialect.MySQLDialect</property>
<property name='connection.driver_class'>com.mysql.jdbc.Driver</property>
<property name='connection.url'>jdbc:mysql://localhost:3306/hotel</property>
<property name='connection.username'>root</property>
<property name='connection.password'>root123</property>
<property name='cache.provider_class'>org.hibernate.cache.HashtableCacheProvider</property>
<property name='transaction.factory_class'>org.hibernate.transaction.JDBCTransactionFactory</property>
<property name='current_session_context_class'>thread</property>
<property name='hibernate.show_sql'>false</property>
<!-- List of XML mapping files -->
<mapping resource='Hotel.hbm.xml'/>
<mapping resource='Car_Rental.hbm.xml'/>
<mapping resource='Hotel_Reservation.xml'/>
<mapping resource='Ticket_Reservation.hbm.xml'/>
<mapping resource='Tour_Reservation.hbm.xml'/>
<mapping resource='Touristic_Place.hbm.xml'/>
<mapping resource='User.hbm.xml'/>
</session-factory>
</hibernate-configuration>
this is where I'm trying to build sessionfactory:
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
//Configuration configuration = new Configuration().configure(new File("C:\\Users\\user\\workspace\\Hotel\\src\\hibernate.cfg.xml"));
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
Console:
javax/transaction/SystemException
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at screens.Screen_User.save(Screen_User.java:211)
at screens.Screen_User$1.actionPerformed(Screen_User.java:182)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
I'm working on this for 2 day but I could not figure that out. Any help would be great. Thank you.

Related

Getting exception while reading element for XML

I am able to successfully load the XML based property file using below code:
Properties props = new Properties();
InputStream is = SampleConfig.class.getResourceAsStream("/test.properties");
System.out.println(is);
props.loadFromXML(is);
String appId = props.getProperty("favoriteSeason");
System.out.println(appId);
My Property File Code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="favoriteSeason">test</entry>
<entry key="favoriteFruit">test1</entry>
<entry key="favoriteDay">test2</entry>
</properties>
But however, when I run this I get below exception:
Caused by: org.xml.sax.SAXParseException: The content of element type "properties" must match "(comment?,entry*)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at java.util.XMLUtils.getLoadingDoc(XMLUtils.java:113)
at java.util.XMLUtils.load(XMLUtils.java:85)
You are trying to use loadFromXML() method to load from a .properties file. I think that is the issue as the stacktrace shows that its not getting the required format of comments-entry type.

java.lang.AbstractMethodError: com.hxtt.sql.by.bu()Lcom/hxtt/global/a0;

I am trying to work with MS ACCESS and Hibernate. And I caught such exception:
Exception in Application init method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$49/128893786.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.AbstractMethodError: com.hxtt.sql.by.bu()Lcom/hxtt/global/a0;
at com.hxtt.sql.a.getTypeInfo(Unknown Source)
at org.hibernate.engine.jdbc.spi.TypeInfo.extractTypeInfo(TypeInfo.java:101)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:163)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at service.Menu.init(Menu.java:59)
... 4 more
Exception running application service.Menu
My configuration file is shown bellow:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.hxtt.sql.access.AccessDriver</property>
<property name="connection.url">jdbc:access:///d:/hw71.accdb</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">com.hxtt.support.hibernate.HxttAccessDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="domain.Notebook"/>
<mapping class="domain.CPU"/>
<mapping class="domain.Memory"/>
<mapping class="domain.Vendor"/>
<mapping class="domain.Store"/>
<mapping class="domain.Sales"/>
</session-factory>
Exception appears when I am trying to build a SessionFactory. Please help me, or advise something.

netbeans platform no persistence provider for entitymanager named

When I run my application in Netbeans, I have no problem, but when I run it using java -jar filename.jar, then I get the following:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\ksk>java -jar C:\Users\ksk\Documents\Testing\StateMachine.jar
Oct 16, 2013 10:06:01 AM dtc.fileops.properties.MEISDirectoryManager createDirectories
INFO: Found MEIS Directory: C:\Users\ksk\MEIS
Trying config file in C:\Users\ksk\MEIS\StationApplication\config.txt
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at com.factoryserviceslibrary.db.EMGR.getInstance(EMGR.java:24)
at statemachine.Context.startEMGR(Context.java:412)
at statemachine.Main$3.run(Main.java:277)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named PU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.factoryserviceslibrary.db.EMGR.startEMGR(EMGR.java:41)
at com.factoryserviceslibrary.db.EMGR.<init>(EMGR.java:32)
at com.factoryserviceslibrary.db.EMGR.<init>(EMGR.java:16)
at com.factoryserviceslibrary.db.EMGR$SingletonHolder.<clinit>(EMGR.java:64)
... 17 more
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EngFoxProPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.....BOM</class>
<class>com.....INV</class> (Other classes)
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:dbf:/R:/DTCI/"/>
<property name="javax.persistence.jdbc.driver" value="com.hxtt.sql.dbf.DBFDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.platform.class.name" value="com.hxtt.support.eclipselink.DbfPlatform"/>
</properties>
</persistence-unit>
</persistence>
Notice that my database is linked to a network drive and it's just reading the data from files, not an actual database server.
I have verified that the classpath includes the eclipselink.jar and javax.persistence.jar.
I hope someone can give me something new to try. Thanks so much.

Oracle hibernate cfg

I am getting this error when trying to start up my hibernate. I am a hibernate newbie so was hoping someone could point out the mistake I made with my cfg. I included the error, hope it is an easy fix, thanks.
<?xml version='1.0' encoding='utf-8'?>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:orcl"</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2241)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:230)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:71)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:212)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:71)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:206)
at HibernateContext.setConfiguration(HibernateContext.java:20)
at HibernateContext.addClass(HibernateContext.java:205)
at HibernateContext.addClasses(HibernateContext.java:216)
at main.main(main.java:8)
Caused by: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 89; Attribute "value" must be declared for element type "property".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
... 11 more
Try using
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
As a newbie, you can read entire hibernate documentation at
http://www.hibernate.org/docs
change : connection.driver_class
to official :
hibernate.connection.driver_class
also read this article

XML error validating appengine-web.xml

I get this error when I try to deploy to Google AppEngine:
com.google.apphosting.utils.config.AppEngineConfigException: XML error validating /Users/matt/Documents/workspace/myapp222/war/WEB-INF/appengine-web.xml against /Users/matt/Downloads/eclipse 7/plugins/com.google.appengine.eclipse.sdkbundle.1.3.1_1.3.1.v201002101412/appengine-java-sdk-1.3.1/docs/appengine-web.xsd
at com.google.appengine.tools.admin.Application.validateXml(Application.java:322)
at com.google.appengine.tools.admin.Application.<init>(Application.java:87)
at com.google.appengine.tools.admin.Application.readApplication(Application.java:121)
at com.google.appengine.eclipse.core.proxy.AppEngineBridgeImpl.createAppAdmin(AppEngineBridgeImpl.java:204)
at com.google.appengine.eclipse.core.proxy.AppEngineBridgeImpl.deploy(AppEngineBridgeImpl.java:265)
at com.google.appengine.eclipse.core.deploy.DeployProjectJob.runInWorkspace(DeployProjectJob.java:145)
at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: org.xml.sax.SAXParseException: cvc-pattern-valid: Value '0s' is not facet-valid with respect to pattern '\s*(([1-9][0-9]*)([DdHhMm]|[sS]?))(\s+([1-9][0-9]*)([DdHhMm]|[sS]?))*\s*' for type 'expirationType'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.processOneAttribute(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.processAttributes(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Validator.java:127)
at com.google.appengine.tools.admin.Application.validateXml(Application.java:319)
... 7 more
My appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>myapp222</application>
<version>1</version>
<!-- Configure serving/caching of GWT files -->
<static-files>
<include path="**" />
<!-- The following line requires App Engine 1.3.2 SDK -->
<include path="**.nocache.*" expiration="0s" />
<include path="**.cache.*" expiration="365d" />
<exclude path="**.gwt.rpc" />
</static-files>
<!-- Configure java.util.logging -->
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>
What's wrong?
Caused by: org.xml.sax.SAXParseException: cvc-pattern-valid:
Value '0s' is not facet-valid with respect to pattern
'\s*(([1-9][0-9]*)([DdHhMm]|[sS]?))(\s+([1-9][0-9]*)([DdHhMm]|[sS]?))*\s*'
for type 'expirationType'.
<include path="**.nocache.*" expiration="0s" />
0s is not valid. You must specify at least 1s.

Categories

Resources