I'm having trouble getting Hibernate to correctly reverse engineer a MySQL database into a Java Class.
I have 3 tables:
departments (pk = dept_no)
employees (pk = emp_no)
dept_emp (composite pks dept_no and emp_no)
I am trying to modify the Hibernate Reverse Engineering file to force the DeptEmp Java class that is generated to link a single department to a set of employees within that department, but I keep getting the same error, even though I have declared a name attribute for the set:
org.hibernate.MappingException: Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
org.hibernate.MappingException: Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
Could not configure overrides from file: C:\lib\workspace\Hibernate Analysis 2\resources\hibernate.reveng.xml
org.hibernate.MappingException: invalid override definition
invalid override definition
org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 88; Attribute "name" must be declared for element type "set".
Attribute "name" must be declared for element type "set".
My Reverse Engineering File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-catalog="employees" match-name="departments" />
<table-filter match-catalog="employees" match-name="employees" />
<table-filter match-catalog="employees" match-name="dept_emp" />
<table name="dept_emp" class="com.DeptEmp">
<set name="employeesSet" table="employees" inverse="true" lazy="true" fetch="select">
<key>
<column name="dept_no" ></column>
</key>
<one-to-many class="com.Employees"></one-to-many>
</set>
</table>
</hibernate-reverse-engineering>
My Hibernate 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/employees</property>
<property name="hibernate.connection.username">****</property>
<property name="hibernate.connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.default_catalog">employees</property>
<property name="connection.pool_size">20</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="format_sql">true</property>
<property name="use_sql_comments">true</property>
</session-factory>
</hibernate-configuration>
Related
I have a maven project that I am using with hibernate-core 4.2.3.Final. I have followed the advice provided here:
Where to place hibernate config?
Hibernate Tutorial - Where to put Mapping File?
and the config file in src/main/resources looks like this:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/justHibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">12345</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/mycompany/app/model/Movie.hbm.xml" />
</session-factory>
</hibernate-configuration>
while the mapping file is within the package com.mycompany.app.model alongside the Movie class:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mycompany.app.model.Movie" table="Movies">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="title" column="title"/>
<property name="director" column="director"/>
<property name="synopsis" column="synopsis"/>
</class>
</hibernate-mapping>
Exception
The configuration cannot find the mapping file.
INFO: HHH000221: Reading mappings from resource: com/mycompany/app/model/Movie.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com/mycompany/app/model/Movie.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:738)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2167)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2139)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2119)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2072)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1966)
at com.mycompany.app.MovieManager.initSessionFactory(MovieManager.java:22)
at com.mycompany.app.MovieManager.<init>(MovieManager.java:17)
at com.mycompany.app.App.main(App.java:27)
Place Movie.hbm.xml under src/main/resources and change path in your config file as follows,
<mapping resource="Movie.hbm.xml" />
Hope this helps.
Good Morning to everyone!
I´m trying to use Hibernate to read an MSAccess database in Java using Eclipse, but it gives me a MappingException
here is my hibernateaccess.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<session-factory>
<property name='connection.driver_class'>net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name='connection.username'></property>
<property name='connection.password'></property>
<!-- JDBC connection pool (use the built-in) -->
<property name='connection.pool_size'>1000</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">600000</property>
<!-- SQL dialect -->
<property name='dialect'>dialect.MSAccessDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name='show_sql'>true</property>
<!-- Mapping files -->
<mapping class="TransporteAccess.hbm.xml" />
</session-factory>
</hibernate-mapping>
and the TransporteAccess.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Transporte" table="Transportes">
<property name="transporte" column="TRANSPORTES" type="string"></property>
</class>
</hibernate-mapping>
Which I´m doing wrong???
Thanks a lot!
It is mandatory to have Primary Key in Hibernate, refer
Add Primary key as well,
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
apart from this the hibernateaccess.xml contains some irrelevant tags
Update top lines in hibernateaccess.xml with
<?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>
& change
<mapping class="TransporteAccess.hbm.xml" />
to
<mapping resource="TransporteAccess.hbm.xml" />
I am posting my hbm file with the cfg file along with my error log. I found my questions based on similar problems are tried out their solution too but still my error is not fading away.
log
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at net.viralpatel.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
at net.viralpatel.hibernate.Main.main(Main.java:13)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource net/viralpatel/hibernate/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
... 2 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 9 more
Caused by: org.dom4j.DocumentException: Error on line 9 of document : The element type "generator" must be terminated by the matching end-tag "</generator>". Nested exception: The element type "generator" must be terminated by the matching end-tag "</generator>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 10 more
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="EMPLOYEE" >
<id name="employeeId" column="EMPLOYEE_ID">
<generator class="native">
</id>
<one-to-one name="employeeDetail" class="net.viralpatel.hibernate.EmployeeDetail"
cascade="save-update"></one-to-one>
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="birthDate" type="date" column="birth_date" />
<property name="cellphone" column="cell_phone" />
</class>
hibernate.cfg.xml
<!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.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">tcs#1234</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</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">update</property>
<mapping resource="net/viralpatel/hibernate/EmployeeDetail.hbm.xml"/>
<mapping resource="net/viralpatel/hibernate/Employee.hbm.xml"/>
</session-factory>
In your Employee.hbm.xml you're not closing the generator tag.
Replace
<generator class="native">
by this
<generator class="native"/>
By looking at stack trace, you have not end the generator tag properly.
Please change your employee.hbm.xml file to end the <generator> element properly.
For Ex: It should be <generator class="native"/>
I am trying to query an access db in java 8 and i use maven. I am using net.ucanaccess.jdbc.UcanaccessDriver for my connection and below is my hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"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">
net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name="hibernate.connection.url">jdbc:ucanaccess://D:\\db\\QIDB-Access\\db.mdb</property>
<property name="hibernate.connection.username"> </property>
<property name="hibernate.connection.password"> </property>
<mapping resource="IndikatorProcessor.hbm.xml" />
</session-factory>
</hibernate-configuration>
My class with annotations looks like this:
#Entity
#Table(name = "Indikator")
public class IndikatorProcessor {
#Id
#Column(name = "QI_ID")
private int qiId;
public int getQiId() {
return qiId;
}
public void setQiId(int qiId) {
this.qiId = qiId;
}
}
In another class i create the session and write a simple query:
....
public void listQIIndikator() {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List<?> indikators = session.createQuery("From IndikatorProcessor").list();
for (Iterator<?> iterator = indikators.iterator(); iterator.hasNext();) {
IndikatorProcessor indiaktor = (IndikatorProcessor) iterator.next();
System.out.println("Indikator ID = " + indiaktor.getQiId());
}
tx.commit();
I get this error:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: IndikatorProcessor is not mapped
I am not sure what causes the error since it is mapped! Is it the query or using the ucanaccess driver for the connection?
Now i added a IndikatorProcessor.hbm.xml as folow and changed the hibernate file to use it.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="IndikatorProcessor" table="Indikator">
<meta attribute="class-description">
This class contains the Indikator detail.
</meta>
<id name="qiId" type="int" column="QI_ID"> <!-- <generator class="native"/> --> </id>
<!-- <property name="qiId" column="QI_ID" type="int"/> -->
<property name="fkQig" column= "FK_QIG"></property>
Now i get these errors:
Caused by: org.hibernate.MappingException: class IndikatorProcessor not found while looking for property: fkQig
and
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [IndikatorProcessor]
Exception says your IndikatorProcessor is not mapped in SessionFactory, so do a mapping as follows:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class"> net.ucanaccess.jdbc.UcanaccessDriver</property>
<property name="hibernate.connection.url">jdbc:ucanaccess://D:\\db\\QIDB-Access\\db.mdb</property>
<property name="hibernate.connection.username"> </property>
<property name="hibernate.connection.password"> </property>
<mapping class="your.package.IndikatorProcessor"/>
</session-factory>
</hibernate-configuration>
hibernate could not find the class without giving the package name. I added the package name in IndikatorProcessor.hbm.xml and it worked.
<hibernate-mapping>
<class name="model.IndikatorProcessor" table="Indikator">
<meta attribute="class-description">
This class contains the Indikator detail.
</meta>
<id name="qiId" type="int" column="QI_ID"> </id>
<property name="fkQig" type ="int" column= "FK_QIG"></property>
If it is duplicate, my apology. I could not find an answer for my question.
I just start Hibernate. I have an issue is every time the program automatically delete the old shceme and creates new when it is running. For example, If I want to add records into database, I could not do it because the scheme will be recreate, so the histories will be deleted.
Here is my hbm.xml mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-Oct-2013 1:39:31 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="net.ys.hibernate.Equip" table="EQUIP">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="dis" type="java.lang.String" column="dis" />
<property name="ref" type="java.lang.String" column="ref" />
<property name="type" type="java.lang.String" column="type" />
</class>
</hibernate-mapping>
hibernate.cfg.xml configuration file.
<?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>
<!-- hibernate dialect -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">pw</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/explorer_DB?useUnicode=true&characterEncoding=GBK</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.default_schema">explorer_DB</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Automatic schema creation(begin) -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Simple memory-only cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- mapping files with external dependencies -->
<mapping resource="net/ys/hibernate/Equip.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And addEquip method:
public Integer addEquip(String dis, String ref, String type){
Session session = sessionFactory.openSession();
currentSession = sessionFactory.getCurrentSession();
Transaction tx = null;
Integer equipID = null;
try {
tx = currentSession.beginTransaction(); //start a transaction
Equip equip = new Equip(dis,ref,type);
equipID = (Integer)currentSession.save(equip);
tx.commit();
} catch (HibernateException e) {
if(tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return equipID;
}
}
could someone help me to solve this issue? I just don't understand how to use getCurrentSession(), probably I am wrong in this point. Could you explain how hibernate works when we call getCurrentSession() for me? I really appreciate it.
Thank you so much
Change this property in your hibernate.cfg.xml file
current configuration
<property name="hibernate.hbm2ddl.auto">create</property>
to
<property name="hibernate.hbm2ddl.auto">none</property>
if no change required in database use.
<property name="hibernate.hbm2ddl.auto">validate</property>
More on this please refer this link
Hibernate hbm2ddl.auto possible values and what they do?