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>
Related
I am trying to connect to in memory h2 database from java using hibernate/jpa but execution is hanging after while and not creating any tables in DB or terminating either. this is my persistence.xml file and execution log
<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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"
version="2.0">
<persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL">
<provider> org.hibernate.ejb.HibernatePersistence </provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://127.0.0.1:8082/~/test"></property>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"></property>
<property name="javax.persistence.jdbc.user" value="chaitu"></property>
<property name="javax.persistence.jdbc.password" value=""></property>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
<property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
</properties>
</persistence-unit>
</persistence>
These are the execution logs
Oct 22, 2021 11:36:33 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [name: myApp]
Oct 22, 2021 11:36:33 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 5.4.29.Final
Oct 22, 2021 11:36:33 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
Oct 22, 2021 11:36:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Oct 22, 2021 11:36:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:tcp://127.0.0.1:8082/~/test]
Oct 22, 2021 11:36:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=chaitu, password=****}
Oct 22, 2021 11:36:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 22, 2021 11:36:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Change your database URL to jdbc:h2:mem:test (test is your database name) because you are using an in-memory h2 database and mem represents the in-memory database.
I'm getting this error in my Hibernate desktop application:
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
It's not creating SessionFactory and giving this exception:
Executing E:\Vishal\summer\EmpMan\dist\run1432002073\EmpMan.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_131\jre/bin/java
Jul 04, 2017 2:24:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
Jul 04, 2017 2:24:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 04, 2017 2:24:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:~/EmpMan]
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
That's it. I'm getting only this much information.
My hibernate.cfg.xml file configuration:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:~/EmpMan</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="models.AdminDetails"/>
<mapping class="models.EmployeeDetails" />
<mapping class="models.DepartmentDetails" />
<mapping class="models.DailyDataDetails"/>
</session-factory>
</hibernate-configuration>
Notice:
=> I'm using h2 database and I want to configure embedded database
=> I'm using netbeans and Java8 with hibernate version 5.2.10
=> I'm new to h2 database
=> Where to locate my EmpMan database in my project?
Problem Solved : I have already exist same named database so i changed name and now it's working :) Thank you guys
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.
> 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.
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.