Hello all. I am using
Hibernate version: 3.1 with
Eclipse 3.2
This is my package:
This is my project:
$project/src/no/detnye/moteguiden/hibernate.cfg.xml
$project/src/no/detnye/moteguiden/hibernate.properties
$project/src/no/detnye/moteguiden/hibernate/HibernateUtil.java
(The same from the tutorial)
$project/src/no/detnye/moteguiden/pojos/FargeData.java
$project/src/no/detnye/moteguiden/pojos/FargeData.hbm.xml
$project/bin/log4j.properties
$project/bin/hibernate.properties
These are the same as in my $project/src/no/detnye/moteguiden/ files, because I didn't know exactly where the configuration files should be
$project/bin/*.class (ALL java class files)
Mapping documents: FargeData.hbm.xml
Code:
<?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="no.detnye.moteguiden.model.FargeData"
table="farge"
lazy="false"
>
<cache usage="read-write" />
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment" />
</id>
<property
name="fargenavn"
type="java.lang.String"
update="true"
insert="true"
column="fargenavn"
unique="true"
not-null="true"
/>
<set
name="produktfarger"
lazy="false"
cascade="none"
sort="unsorted"
>
<key column="fargeid">
</key>
<one-to-many
class="no.detnye.moteguiden.model.ProduktFargeData"
/>
</set>
</class>
</hibernate-mapping>
My Test class is as follows: Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
assertNotNull(session);
Criteria criteria = session.createCriteria(ForhandlerData.class);
try {
List forhandlerListe = criteria.list();
for (int i = 0; i < forhandlerListe.size(); i++) {
System.out.println("List contains: " + forhandlerListe.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
When I run this example I get the following exception:
Full stack trace of any exception that occurs: 13:15:44,668 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(18) Element type "id" must be followed by either attribute specifications, ">" or "/>".
Could not initialize SessionFactory org.hibernate.MappingException: Could not read mappings from resource: no/detnye/moteguiden/pojos/ForhandlerData.hbm.xml SessionFactory is null
Name and version of the database you are using:
I am using MySQL
The generated SQL (show_sql=true): 13:15:43,943 INFO Environment:479 - Hibernate 3.1.3
13:15:43,975 INFO Environment:494 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.MySQLMyISAMDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.format_sql=true, hibernate.query.substitutions=yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=root, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:mysql:///moteguiden, hibernate.show_sql=true, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
13:15:43,977 INFO Environment:524 - using java.io streams to persist binary types
13:15:43,978 INFO Environment:525 - using CGLIB reflection optimizer
13:15:43,982 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
13:15:44,132 INFO Configuration:1308 - configuring from resource: /hibernate.cfg.xml
13:15:44,132 INFO Configuration:1285 - Configuration resource: /hibernate.cfg.xml
13:15:44,357 DEBUG DTDEntityResolver:22 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
13:15:44,360 DEBUG DTDEntityResolver:24 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
13:15:44,361 DEBUG DTDEntityResolver:34 - located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
13:15:44,429 DEBUG Configuration:1269 - show_sql=false
13:15:44,430 DEBUG Configuration:1269 - connection.datasource=java:comp/env/jdbc/moteguiden
13:15:44,432 DEBUG Configuration:1269 - dialect=org.hibernate.dialect.MySQLDialect
13:15:44,432 DEBUG Configuration:1269 - hibernate.connection.driver_class=org.gjt.mm.mysql.Driver
13:15:44,432 DEBUG Configuration:1269 - hibernate.connection.url=jdbc:mysql://localhost/moteguiden?user=root&password=
13:15:44,434 DEBUG Configuration:1269 - hibernate.connection.username=root
13:15:44,435 DEBUG Configuration:1269 - hibernate.connection.password=
13:15:44,435 DEBUG Configuration:1269 - hibernate.dialect=org.hibernate.dialect.MySQLDialect
13:15:44,436 DEBUG Configuration:1269 - hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
13:15:44,436 DEBUG Configuration:1269 - hibernate.generate_statistics=true
13:15:44,437 DEBUG Configuration:1269 - hibernate.cache.use_structured_entries=true
13:15:44,437 DEBUG Configuration:1464 - null<-org.dom4j.tree.DefaultAttribute@1506dc4 [Attribute: name resource value "no/detnye/moteguiden/pojos/FargeData.hbm.xml"]
13:15:44,438 INFO Configuration:469 - Reading mappings from resource: no/detnye/moteguiden/pojos/FargeData.hbm.xml
13:15:44,440 DEBUG DTDEntityResolver:22 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
13:15:44,441 DEBUG DTDEntityResolver:24 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
13:15:44,442 DEBUG DTDEntityResolver:34 - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
13:15:44,593 INFO HbmBinder:309 - Mapping class: no.detnye.moteguiden.model.FargeData -> farge
13:15:44,603 DEBUG HbmBinder:1261 - Mapped property: id -> id
13:15:44,615 DEBUG HbmBinder:1261 - Mapped property: fargenavn -> fargenavn
13:15:44,620 DEBUG HbmBinder:1261 - Mapped property: produktfarger
13:15:44,621 DEBUG Configuration:1464 - null<-org.dom4j.tree.DefaultAttribute@b76fa [Attribute: name resource value "no/detnye/moteguiden/pojos/ForhandlerData.hbm.xml"]
13:15:44,623 INFO Configuration:469 - Reading mappings from resource: no/detnye/moteguiden/pojos/ForhandlerData.hbm.xml
13:15:44,624 DEBUG DTDEntityResolver:22 - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
13:15:44,625 DEBUG DTDEntityResolver:24 - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
13:15:44,626 DEBUG DTDEntityResolver:34 - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
This is my Hibernate.cfg.xml file:Code:
<?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>
<!-- By default Moteguiden uses a JNDI DataSource and the MySQLDialect -->
<property name="show_sql">false</property>
<property name="connection.datasource">java:comp/env/jdbc/moteguiden</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/moteguiden?user=root&password=</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Hibernate L2 Caching -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<!-- Map Moteguiden pojo classes -->
<mapping resource="no/detnye/moteguiden/pojos/FargeData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/ForhandlerData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/ForhandlerProduktData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/ForhandlerStedData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/KlestypeData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/MerkeData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/ProduktData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/ProduktFargeData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/ProduktKlestypeData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/PublikasjonData.hbm.xml" />
<mapping resource="no/detnye/moteguiden/pojos/StedData.hbm.xml" />
</session-factory>
</hibernate-configuration>
My hibernate.propertiesCode:
hibernate.query.substitutions yes 'Y', no 'N'
## MySQL
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///moteguiden
hibernate.connection.username root
hibernate.connection.password
#################################
### Hibernate Connection Pool ###
#################################
hibernate.connection.pool_size 1
hibernate.proxool.pool_alias pool1
## print all generated SQL to the console
hibernate.show_sql true
## format SQL in log and console
hibernate.format_sql true
hibernate.max_fetch_depth 1
hibernate.jdbc.batch_versioned_data true
## enable use of JDBC 2 scrollable ResultSets (specifying a Dialect will cause Hibernate to use a sensible default)
#hibernate.jdbc.use_scrollable_resultset true
## use streams when writing binary types to / from JDBC
hibernate.jdbc.use_streams_for_binary true
hibernate.cache.region_prefix hibernate.test
hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
If someone could please help me on how to set up hibernate correctly I would appreciate it and give alot of points :)
I can also see that I have chosen to output sql but the hibernate.properties file doesnt seem to be read. So maybe the problem is that hibernate.properties and hibernate.cfg.xml is not read! How can I set these to be read? In the tutorial it said I should have them in the classpath (I didnt know where to put them, so I put them in the package)
The exception says:
13:15:44,668 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(18) Element type "id" must be followed by either attribute specifications, ">" or "/>".
But I have checked my FargeData.hbm.xml file and this is well formed.
So I am quite confused :(