Hibernate exception on 4.0 XSD - java

First time out of the box with Hibernate, and getting a bizarre error. I am using the 4.2.3 Final version, and would like to use the 4.0 XSD schema, but will use the 3.0 DTD if that's what it takes to get it to work.
My driver:
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("hibernate/cfg/hibernate.cfg.xml");
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration
.getProperties());
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
User user = new User("Dr.", "Sylvester", "McMonkey", "McBean", "IV");
session.save(user);
tx.commit();
session.close();
}
My hibernate.cfg.xml file, located in my project under src/main/config/hibernate/cfg:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://my-server:3306/my_db</property>
<property name="hibernate.connection.username">myuser</property>
<property name="hibernate.connection.password">password</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="hbm2ddl.auto">validate</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And the stacktrace Eclipse is giving me:
Jul 27, 2013 9:10:08 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jul 27, 2013 9:10:08 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.3.Final}
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate/cfg/hibernate.cfg.xml
Jul 27, 2013 9:10:08 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate/cfg/hibernate.cfg.xml
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2070)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at com.apponomical.adaptors.datasource.HibernateDriver.main(HibernateDriver.java:15)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 25; Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:387)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:321)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:226)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3080)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:899)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:625)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:819)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:748)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:525)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067)
... 2 more
Thoughts? Ideas? Suggestions? Thanks in advance!

The validator is not finding the schema. The xsi:schemaLocation attribute tells it to look for schema document hibernate-configuration-4.0.xsd in the same directory as the configuration document you're validating (.../src/main/config/hibernate/cfg). Is that where you have it?

With Hibernate 4.x you should use the same DTD as 3.x as you can see by this bug report, where the deprecation warning was removed.
Also the tutorials for 4.2 version use this, as you can see here.

Related

is-a relation in hibernate

I am trying basic example for Is-A relation in hibernate , but getting error.
I have created 3 child class of Employee Class
SEmployee
Hemployee
AEmployee
and used (joined-subclass ) tag to establish relation.
Before this i tried same example using (sub-class) tag and it worked perfectly fine but now using (joined-subclass) error is showing.
i have not change anything but the xml tag.
don't know what i am doing wrong here.
mysql.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedata</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="resources/employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Employee.hbm.xml
<joined-subclass name="beans.SEmployee" table="semployee" extends="beans.Employee">
<key column="id"></key>
<property name="tool"></property>
</joined-subclass>
<joined-subclass name="beans.HEmployee" table="hemployee" extends="beans.Employee">
<key column="id"></key>
<property name="wh"></property>
</joined-subclass>
<joined-subclass name="beans.AEmployee" table="aemployee" extends="beans.Employee">
<key column="id"></key>
<property name="server"></property>
</joined-subclass>
</class>
</hibernate-mapping>```
Test.java
public static void main(String[] args)
{
Configuration cfg=new Configuration();
cfg.configure("resources/mysql.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction t=session.beginTransaction();
SEmployee se=new SEmployee(100,"abc","abc#gmail.com",2000,"hibernate");
HEmployee he=new HEmployee(200,"abc","abc#gmail.com",2000,20);
AEmployee ae=new AEmployee(300,"abc","abc#gmail.com",2000,"oracle");
session.save(se);
session.save(he);
session.save(ae);
t.commit();
session.close();
sf.close();
}
Exception Log:
Sep 11, 2019 10:25:58 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.4.Final}
Sep 11, 2019 10:25:59 AM
org.hibernate.annotations.common.reflection.java.JavaReflectionManage
<clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for
production use!)
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL
[jdbc:mysql://localhost:3306/hibernatedata]
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
Sep 11, 2019 10:26:01 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Sep 11, 2019 10:26:03 AM org.hibernate.AssertionFailure <init>
ERROR: HHH000099: an assertion failure occurred (this may indicate a
bug in Hibernate, but is more likely due to unsafe use of the
session): org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
Sep 11, 2019 10:26:03 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool
[jdbc:mysql://localhost:3306/hibernatedata]
Exception in thread "main" org.hibernate.MappingException: Could not
instantiate persister
org.hibernate.persister.entity.JoinedSubclassEntityPersister
at
org.hibernate.persister.internal.PersisterFactoryImpl.
createEntityPersister(PersisterFactoryImpl.java:112)
at
org.hibernate.persister.internal.PersisterFactoryImpl.
createEntityPersister(PersisterFactoryImpl.java:77)
at
org.hibernate.metamodel.internal.MetamodelImpl.
initialize(MetamodelImpl.java:181)
at org.hibernate.internal.SessionFactoryImpl.<init>
(SessionFactoryImpl.java:300)
at
org.hibernate.boot.internal.
SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
at
org.hibernate.cfg.Configuration.
buildSessionFactory(Configuration.java:708)
at
org.hibernate.cfg.Configuration.
buildSessionFactory(Configuration.java:724)
at test.Client_Relations.main(Client_Relations.java:17)
Caused by: org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
at
org.hibernate.persister.entity.AbstractEntityPersister.
getTableId(AbstractEntityPersister.java:5541)
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>
(JoinedSubclassEntityPersister.java:450)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl.java:62)
at
sun.reflect.DelegatingConstructorAccessorImpl.
newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.hibernate.persister.internal.PersisterFactoryImpl.
createEntityPersister(PersisterFactoryImpl.java:96)
... 7 more
This error shows that the table Employee does not exist in the database.
Caused by: org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
You need to replace
<property name="hbm2ddl.auto">update</property>
with
<property name="hbm2ddl.auto">create</property>

Hibernate Configuration: Jar files?

im noobie here but i have been finding the fix of my problem here i dont see it... Im new with hibernate and im using my free time to learn this.
package negocio;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import integracion.HibernateUtil;
import integracion.dao.BodegaDAOImpl;
import integracion.dao.VinoDAO;
import integracion.dao.VinoDAOImpl;
import integracion.entidades.Bodega;
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
//The problem is here
**sessionFactory = configuration.buildSessionFactory(serviceRegistry);**
Session session = sessionFactory.openSession();
BodegaDAOImpl bodegaDao = new BodegaDAOImpl<>();
bodegaDao.get(1);
}
}
This is the list of the jar added1
And this is the mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/vinoteca</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="integracion.entidades.Vino"/>
<mapping class="integracion.entidades.Bodega"/>
<mapping class="integracion.entidades.Denominacion"/>
</session-factory>
</hibernate-configuration>
dic 15, 2016 12:37:51 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.9.Final}
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
dic 15, 2016 12:37:51 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/vinoteca]
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Thu Dec 15 00:37:51 CET 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
dic 15, 2016 12:37:51 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery
at org.hibernate.cfg.AnnotationBinder.bindDefaults(AnnotationBinder.java:259)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.prepare(AnnotationMetadataSourceProcessorImpl.java:184)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.prepare(MetadataBuildingProcess.java:156)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:253)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at negocio.Main.main(Main.java:31)
Caused by: java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
So.. this is the problem. I was seeing that the problem when eclipse says java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
was because the jar (javax.persistence.jar or hibernate-jpa-2.1-api-1.0.0...jar) was not added, but i added them and i dont resolve this.
anyone could say me why is working bad? Thanks!
PD:Sorry for my english, im not Native english speaker =)
Use a build system like maven or gradle to build your project, and add a dependency on hibernate. That will almost certainly solve all of your basic dependency-related classpath problems.
Add the required jar files into the classpath.

Java hibernate throws Exception at boot

I'm working on a project with Hibernate now but I get an error when running my project.
On server.java (my entry point) I got this:
private static HibernateUtil hibernateUtil;
And in my entry point:
hibernateUtil = new HibernateUtil();
This is my HibernateUtil class:
public class HibernateUtil {
private SessionFactory sessionFactory;
public HibernateUtil() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
StandardServiceRegistryBuilder.destroy( registry );
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Now my Hibernate.cf.xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dynamicdb</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">false</property>
<property name="connection.pool_size">1</property>
</session-factory>
And now this is the error I get:
Jun 18, 2016 1:17:17 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 18, 2016 1:17:17 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 18, 2016 1:17:17 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 18, 2016 1:17:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/dynamicdb]
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/dynamicdb]
Jun 18, 2016 1:17:18 PM org.hibernate.service.internal.AbstractServiceRegistryImpl stopService
INFO: HHH000369: Error stopping service [class org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] : java.lang.NullPointerException
I'm running Eclipse mars 2 on Mac OS X 10.10 with MAMP. MySQL is running and the database user and database exists and the password is correct. What's the problem?
I have the same problem like you. It seems that we both copy the code from Hibernate Official code samples. I think maybe it's bug of mysql connecotrj 6.0.After I add throw statement to the catch statement:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure()
.build();
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
throw new RuntimeException(e);
}
And I found the origin exception:
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
This case doesn't appear in mysql connectorj 5.1
So, there are two solutions:switch mysql jdbc drivers to 5.1 or add timezone argument to jdbc url string like this. In hibernate.cfg.xml file don't forget to change & to &
I got the same issue when my mapping file contained one column twice
<property column="STATE" name="state" type="java.lang.String" not-null="true"/>
<property column="BRANCH" name="branch" type="java.lang.String" not-null="true"/>
<property column="COUNTRY" name="country" type="java.lang.String" not-null="true"/>
<property column="STATE" name="state" type="java.lang.String" not-null="true"/>
So once I changed one state to appropriate property e.g.
<property column="SITE" name="site" type="java.lang.String" not-null="true"/>
The issue was solved.
So be careful with your mappings
In my case, I was using Netbeans and had my entities generated using Netbeans only.
after fiddling with mysql and hibernate-core jar versions, after commenting #NamedQueries fixed it for me.
For others who may still encounter this issue, the database provided in the connection URL needs to exist, in this case, we need to ensure the database dynamicdb exists. I had the same issue and this fixed it.

Hibernate cant test my session, it's null

Hi im using eclipse this hibernate update.
i have this config:
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>
i have this class that has the basics to start hibernate:
package com.simpleprogramer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch(HibernateException exeption)
{
System.out.println("Problem creating session factory!");
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
and main that test hibernate session, and i don't know were is the null:
package com.simpleprogramer;
import org.hibernate.Session;
public class program {
public static void main(String[] args) {
System.out.println("Hola mundo");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.close();
}
}
getting this error from console:
Hola mundo
oct 01, 2013 2:12:31 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
oct 01, 2013 2:12:31 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
oct 01, 2013 2:12:31 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
oct 01, 2013 2:12:31 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Problem creating session factory!
Exception in thread "main" java.lang.NullPointerException
at com.simpleprogramer.program.main(program.java:9)
Hi there, thank you for your advice exeption.printStacktrace(System.out);. I figure out that my jdbc driver was not loaded, and then realized that it wasn't loaded at all on the proyect (i thought that the config autoload no matter what or were the driver is). So i downloaded the corresponding jar and it works. Thanks for your advice!

Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML

> I am using eclipse .I tried to do java hibernate program for the first time but got the above error.please help me to solve it.
downloaded hibernate release 4.2.5 final.zip and added jar files to user library
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
hibernate-entitymanager-4.2.5.Final.jar
hibernate-envers-4.2.5.Final.jar
hibernate-osgi-4.2.5.Final.jar
org.osgi.core-4.3.1.jar
is these jar files enough for hibernate program.
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>
<!-- Related to the connection START -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:#192.168.1.2:1521:xe</property>
<property name="connection.user">system</property>
<property name="connection.password">siddhu</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->
<!-- Related to mapping START -->
<mapping resource="Details.hbm.xml"/>
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
Details.hbm.xml
- <hibernate-mapping>
- <class name="Details" table="stu">
- <id name="stNo" column="SNo">
<generator class="assigned" />
</id>
<property name="stName" column="SName" />
<property name="stAddress" />
</class>
</hibernate-mapping>
mainclass.java
import org.hibernate.*;
import org.hibernate.cfg.*;
public class mainclass {
public static void main(String[] args)
{
Configuration c = new Configuration();
c.configure("hibernate.cfg.xml");
SessionFactory factory = c.buildSessionFactory();
Session session = factory.openSession();
Details p=new Details();
p.setStno(101);
p.setStName ("iPhone");
p.setStAddress("dfdsgdf");
Transaction tx = session.beginTransaction();
session.save(p);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
Details.java
public class Details {
private int stNo;
private String stName;
private String stAddress;
public void setStno(int stNo)
{
this.stNo=stNo;
}
public int getStNo()
{
return stNo;
}
public void setStName(String stName)
{
this.stName=stName;
}
public String getStName()
{
return stName;
}
public void setStAddress(String stAddress)
{
this.stAddress=stAddress;
}
public String getStAddress()
{
return stAddress;
}
}
Console Error
===========================================
Sep 20, 2013 6:49:30 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 20, 2013 6:49:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Sep 20, 2013 6:49:31 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Sep 20, 2013 6:49:31 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Details.hbm.xml
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:488)
at org.hibernate.cfg.Configuration.add(Configuration.java:484)
at org.hibernate.cfg.Configuration.add(Configuration.java:657)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
at mainclass.main(mainclass.java:9)
Caused by: org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 10 more
Your XML file, for whatever reason, might contain a BOM as described in this answer and its linked article.
I suggest you create a new file and write (copy) over the configuration from your mapping file.

Categories

Resources