I have a problem with java jdk1.6.0_17 and hibernate .
I must open a hibernate session factory but mapping file (mapper-test.jar) is load by a personaly classLoader (URLClassLoader)
File map = new File("/opt/sigeco/infowebrepository/mapper/mapper-test.jar");
File map1 = new File("/opt/sigeco/infowebrepository/mapper/hibernate3.jar");
URL[] urls = new URL[] { map.toURI().toURL(),map1.toURI().toURL()};
URLClassLoader loader = new URLClassLoader(urls);
//test load is ok
loader.loadClass("it.sigeco.infoweb.dao.app.IcFabb");
Configuration configuration=new Configuration();
configuration.configure( loader.getResource("hibernate.cfg.xml"));
configuration.buildSessionFactory();
the problem is:
org.hibernate.MappingException: Resource: it/sigeco/infoweb/dao/app/IcFabb.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1325)
at testLoad.main(testLoad.java:32)
the hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.jdbc.use_scrollable_resultset">true</property>
<mapping resource="it/sigeco/infoweb/dao/app/IcFabb.hbm.xml"/>
</session-factory>
</hibernate-configuration>
if load the mapper-test with build path is success, so the mapper-test is ok.
Seems that the Configuration search the file it/sigeco/infoweb/dao/app/IcFabb.hbm.xml in the system classloader and not in the URLClassLoader, but how do I connect the Configuration with the Loader?
Thank you
I've found one solution. I create a personaly ClassLoader, MapperClassLoader, that extend UrlClassLoader where in costructor:
MapperClassLoader(URL[] urls)
{
super( urls,MapperClassLoader.class.getClassLoader());
}
where urls contains the jars url. So set the parent loader whit the system class loader.
After create MapperClassLoader, set the system ClassLoader whit MapperClassLoader
Thread.currentThread().setContextClassLoader( MapperClassLoader.getInstance());
So the mapper-test.jar is usabile.
Related
I am working with Maven, on Netbeans, on Windows
I have my full configured hibernate.cfg.xml file on
"/src/main/resources/hibernate.cfg.xml"
I have triple checked this is a fact.
This is my folder structure on Netbeans (picture)
When running I get this:
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException:
Could not locate cfg.xml resource [/resources/hibernate.cfg.xml]
This is my hibernate cfg.xml file:
<!-- This file should be referenced in the configure() method!!! -->
<?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>
<! -- url -->
<property name="hibernate.connection.url">jdbc:oracle:thin:#toshiba-pc:1521:SYSTEM</property>
<! -- username -->
<property name="hibernate.connection.username">system</property>
<!-- password -->
<property name="hibernate.connection.password">27111995</property>
<!-- database driver -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
</session-factory>
</hibernate-configuration>
And on my main class the relevant lines are these:
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("/resources/hibernate.cfg.xml");
...more code...
So basically I have been trying to get around this with no success at all. Any help would be extremely appreciated.
Finally solved it!
My solution was to create a new File object using the System.property("user.dir") method and pass it to the Configuration constructor:
String roothFolder = System.getProperty("user.dir");
File hibernateConfigFile = new File
(roothFolder + "/src/main/resources/hibernate.cfg.xml");
Configuration cfg = new Configuration();
cfg.configure(hibernateConfigFile);
My XML file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.sourceforge.net/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#192.168.252.128:1521:orcl</property>
<property name="connection.username">system</property>
<property name="connection.passowrd">manager</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.nttdata.domain.Employee"/>
</session-factory>
</hibernate-configuration>
Console after execution ::
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1222)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1147)
at com.nttdata.util.HibernateUtil.getSessionFactory(HibernateUtil.java:11)
at com.nttdata.dao.EmployeeDao.saveEmployee(EmployeeDao.java:13)
at com.nttdata.client.Driver.main(Driver.java:10)
Caused by: org.dom4j.DocumentException: www.hibernate.sourceforge.net Nested exception: www.hibernate.sourceforge.net
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
... 5 more
What can be the error any help??
Try changing the DOCTYPE to this:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
This means that hibernate will load the DTD from classpath - it is usually included in hibernate jar in org/hibernate directory.
However, we use hibernate 3.5.6 - I don't hnow if this approach still works in the newer version - give it a try. The benefit of this is that you are completely independent on internet connection, proxies and so on.
Hibernate Configuration File Location
The first solution was to provide the DTD file location in the system using classpath. So the DocType that worked offline would be;
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
Use SourceForge DTD URL with SYSTEM
Another solution I found working is when I change the DTD URL to SourceForge and changed the declaration from PUBLIC to SYSTEM.
So below will also work if your system is offline.
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
I am new to Hibernate, so I have few dumb questions hope someone would be able to assist me. I have a query in regards to configuring hibernate file i.e. hibernate.cfg.xml file. Normally we configure it as below :
<?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="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Where the considers relevant mapping files. Lets say I have another mapping file, call it company.hbm.xml file and have a one-to-many relationship with employee.hbm.xml file. So, as and when the number of mapping files increases we include them into tag.
Query : How is it different to include multiple mapping files with individual tag from having just a single mapping file with all the relevant mappings.
Appreciate your response
Because such separation is cleaner. Single file with whole configuration tends to grow up and to become a yet another huge, unmaintainable and unreadable XML file.
Please also note, that using annotations right inside your model classes seems to be the right way for you (just as #Shiju Babu stated).
I'm trying to run HQL queries in IntelliJ's Hibernate Console. I've added the datasource to the Data Sources view and added my hibernate.cfg.xml to the Hibernate facet such that in appears in the Persistence view. The content of hibernate.cfg.xml is:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost/mmanager</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="dialect">com.puca.core.util.db.MySQLInnoDBDialect</property>
<!-- mapping files -->
<mapping resource="com/puca/messagemanager/api/model/XmlApiDlrMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The .hbm.xml is shown in green, whereas if I change it to a file that doesn't exist it is shown in red, so it seems that IntelliJ can find it. However, if I run a simple query in the console like:
from XmlApiDlrMapping xmlApiDlrMapping
where xmlApiDlrMapping.retries = 5
I get an error:
java.lang.RuntimeException: org.hibernate.MappingNotFoundException: resource: com/puca/messagemanager/api/model/XmlApiDlrMapping.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:563)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
I'm using IntelliJ 10.5.4.
It's exactly what it says in the error message. The mapping XML file is missing:
com/puca/messagemanager/api/model/XmlApiDlrMapping.hbm.xml
These files are used to map table columns to your entity class fields.
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/tutorial.html#tutorial-firstapp-mapping
I am using hibernate in spring app. but due to some problem i can't use spring injection so i manually have to declare the session factory like below
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure("com/vaannila/service/hibernate.cfg.xml")
.buildSessionFactory();
Session session = sessionFactory.openSession();
Registration person = (Registration) session.get(Registration.class, 1);
As i am using annotation in entity class i get the following error
org.hibernate.MappingException: Unknown entity: com.vaannila.domain.Registration
<?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>
<!-- We're using MySQL database so the dialect needs to MySQL as well-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable this to see the SQL statements in the logs-->
<property name="show_sql">true</property>
<!-- This will drop our existing database and re-create a new one.
Existing data will be deleted! -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.vaannila.domain.Country" />
</session-factory>
</hibernate-configuration>
This could be because of the wrong FQCN entry for the class Registration or, you might be having
import javax.persistence.Entity;
instead of,
import org.hibernate.annotations.Entity;
Make sure your Registration class is annotated correctly with "javax.persistence.Entity".
I am just making sure, I have had same trouble when I missed that.
import javax.persistence.Entity;
#Entity
public class Registration{
}
Another reason could be that you class is not listed in hibernate.cfg.xml:
<mapping class="your.package.Registration"/>
Personally I haven't found the way to force standalone Hibernate scanning entity annotations, so I have pretty big list of mappings inside.