I'm trying to set up Hibernate 3.6 in my existing Spring 3 MVC + Spring 3 security project. Nevertheless I keep getting java.lang.ClassNotFoundException in my HibernateUtil class - line with return new Configuration().configure().buildSessionFactory();
My project structure looks like this:
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>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3049/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="./DBUser.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
DBUser.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="../src/main/java/user/DBUser" table="DBUSER">
<id name="userId" type="int">
<column name="USER_ID" precision="5" scale="0" />
<generator class="assigned" />
</id>
<property name="username" type="string">
<column name="USERNAME" length="20" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="20" not-null="true" />
</property>
<property name="createdDate" type="date">
<column name="CREATED_DATE" length="7" not-null="true" />
</property>
</class>
</hibernate-mapping>
And a relevant part of the pom.xml file:
<!-- Hibernate + MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
EDIT: I've pasted here the stack trace. It was too long to put it right here, because it would make this post confused.
I'd be really glad, if someone could help me with setting up this thing, because I've got no idea, why it can't find that class.
It looks like you are missing the javaassist library. Download the javaassist jar and add it to your classpath.
Your class name should be the package path to the Hibernate POJO in DBUser.hbm.xml, e.g:
user/DBUser
And your mapping resource does not need ./ in the path.
Related
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 want to get a collection of Event.class objects, but criteria.list() returns an empty collection. I also have show_sql = true property, but there is no sql's in console
My hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- hibernate dialect -->
<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/inspector_database</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">********</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</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 class="mainobjects.Driver"/> -->
<property name="hibernate.show_sql">true</property>
<mapping class="inspectorserver.entities.Driver" resource="inspectorserver/entities/Driver.hbm.xml"/>
<mapping class="inspectorserver.entities.Event" resource="inspectorserver/entities/Event.hbm.xml"/>
<mapping class="inspectorserver.entities.Violation" resource="inspectorserver/entities/Violation.hbm.xml"/>
<mapping class="inspectorserver.entities.Car" resource="inspectorserver/entities/Car.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Event.hbm.xml
<?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 18.03.2016 21:32:29 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="inspectorserver.entities.Event" table="EVENT" lazy="false">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="eventDate" type="java.lang.String">
<column name="EVENTDATE" />
</property>
<property name="feeSum" type="java.lang.String">
<column name="FEESUM" />
</property>
<property name="driverLicenseNumber" type="java.lang.String">
<column name="DRIVERLICENSENUMBER" />
</property>
<property name="carVINCode" type="java.lang.String">
<column name="CARVINCODE" />
</property>
<property name="syncStatus" type="int">
<column name="SYNCSTATUS" />
</property>
</class>
</hibernate-mapping>
This is how I get the collection:
Session session = MyHibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Event.class);
criteria.list(); //empty
Solved this by removing class="inspectorserver.entities.Driver(Event, etc.)" from tag
I created hibernate.cfg.xml and UserDataFromDb.hbm.xml and tried to put it in a correct place. First time there was exception like "Cannot find hibernate.cfg.xml", but after several replacement correct place was found. New exception was (with except of lots of other trace):
Caused by: org.hibernate.HibernateException: Enum class not found
Caused by: java.lang.ClassNotFoundException: net.codejava.hibernate.Gender
It is ok, because I've forgotten to change tutorial sample code and print my class name. I've fixed this problem, so, now there are no mentions about class Gender in UserDataFromDb.hbm.xml. Problem is certainly the same.
I've replaced both UserDataFromDb.hbm.xml and hibernate.cfg.xml to desktop and even renamed them. So, no there are no either UserDataFromDb.hbm.xml or hibernate.cfg.xml files on my computer (instead of them -- UserData11FromDb.hbm.xml and hiber111nate.cfg.xml on desktop). Exception are still the same:
Caused by: org.hibernate.HibernateException: Enum class not found
Caused by: java.lang.ClassNotFoundException: net.codejava.hibernate.Gender
though neither Gender class nor even configuration and mapping files exist on computer.
Rebooting computer makes no effect.
OS Windows 7, Hibernate 4.3.6
Config file:
`
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/abusefinder
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
</property>
<!-- List of XML mapping files -->
<mapping resource="UserDataFromDb.hbm.xml" />
</session-factory>
</hibernate-configuration>
mapping:
<?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="UserDataFromDb" table="user_history">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<list name="operations" cascade="all">
<key column="user_id"/>
<list-index column="idx"/>
<one-to-many class="Operation"/>
</list>
<property name="maxOpersPerWeek" column="max_opers_per_week" type="int"/>
<property name="currentOpersPerWeek" column="current_opers_per_week" type="int"/>
<property name="lastWeekFirstOperationTime" column="last_week_first_operation_time" type="long"/>
<property name="addTotal" column="add_total" type="long"/>
<property name="getTotal" column="get_total" type="long"/>
<property name="addOpers" column="add_opers" type="int"/>
<property name="getOpers" column="get_opers" type="int"/>
</class>
<class name="Operation" table="operations">
<id name="operId" type="int" column="oper_id">
<generator class="native"/>
</id>
<property name="userId" column="user_id" type="int"/>
<property name="sum" column="sum" type="long"/>
<property name="time" column="time" type="long"/>
<property name="type" column="type">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">databaseaccess.Type</param>
<param name="useNamed">true</param>
</type>
</property>
</class>
</hibernate-mapping>
Project -> clean
project -> build
I'm working on a Hibernate project and I configured everything.
So, I generated the beans and hbm files.
Then, I wrote a test class to test the project(I used the Client class)
When I executed the code, the following exception was thrown:
java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at org.slf4j.LoggerFactory.singleImplementationSanityCheck(LoggerFactory.java:192)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:113)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.aness.test.HiberM.<clinit>(HiberM.java:12)
Exception in thread "main"
the code is :
import org.aness.beans.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HiberM {
final static Logger logger = LoggerFactory.getLogger(Client.class);
public static void main(String[]arg){
Configuration cfg = new Configuration().configure();
SessionFactory sf =cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx =s.beginTransaction();
Client c =new Client();
c.setRaisonSociale("peugeot algerie");
c.setNumeroRc("3215468897");
c.setIdentificationFiscale("888777999");
c.setAdresseActivite("blida zone atlas");
c.setTelephone("00213(0)25436996");
c.setFax("00213(0)25436996");
s.save(c);
tx.commit();
}
}
that's the whole problem.
the hibernate cfg file is : *
<?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 name="apurement">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/apurement</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
the client mapping is :
<?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 27 d?c. 2012 11:47:54 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="org.aness.beans.Client" table="client" catalog="apurement">
<id name="clientId" type="int">
<column name="client_id" />
<generator class="assigned" />
</id>
<property name="raisonSociale" type="string">
<column name="raison_sociale" length="150" not-null="true" />
</property>
<property name="numeroRc" type="string">
<column name="numero_rc" length="45" not-null="true" />
</property>
<property name="identificationFiscale" type="string">
<column name="identification_fiscale" length="45" not-null="true" />
</property>
<property name="adresseActivite" type="string">
<column name="adresse_activite" length="250" not-null="true" />
</property>
<property name="adressePersonelle" type="string">
<column name="adresse_personelle" length="250" />
</property>
<property name="telephone" type="string">
<column name="telephone" length="45" not-null="true" />
</property>
<property name="fax" type="string">
<column name="fax" length="45" not-null="true" />
</property>
<set name="domiciliations" table="domiciliation" inverse="true" lazy="true" fetch="select">
<key>
<column name="client_id" not-null="true" />
</key>
<one-to-many class="org.aness.beans.Domiciliation" />
</set>
</class>
</hibernate-mapping>
Two possibilities :
Your Hibernate.cfg.xml is not on classpath. What folder is it in?
Else you may try updating the version of slf4j jar
I think you've hit this issue.
It seems you're using SLF4J version 1.5.8 (or thereabouts), as the source code of org.slf4j.LoggerFactory with tag 'SLF4J_1.5.8' has line numbers that match those in your stacktrace.
I would recommend updating to later version of SLF4J.
I'm trying to implement one-to-one association mapping but it gives MappingException
Student.hbm.xml 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 16 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Student" table="STUDENT">
<id name="studentId" type="java.lang.Long">
<column name="STUDENT_ID" />
<generator class="native" />
</id>
<property name="studentName" type="string" length="20" column="STUDENT_NAME"/>
<many-to-one name="studentAddress"
column="STUDENT_ADDRESS" class="com.sst.student.Address" cascade="all"
unique="true"/>
</class>
</hibernate-mapping>
my Address.hbm.xml 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 16 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Address" table="ADDRESS">
<id name="addressId" type="java.lang.Long">
<column name="ADDRESS_ID" />
<generator class="native" />
</id>
<property name="street" type="string" column="STREET" length="20"/>
<property name="city" type="string" column="CITY" length="20"/>
<property name="state" type="string" column="STATE" length="20"/>
<property name="country" type="string" column="COUNTRY" length="20"/>
</class>
</hibernate-mapping>
at the time of code generation I'm getting the following exception:
org.hibernate.MappingException: An association from the table STUDENT refers to an
unmapped class: com.sst.student.Address
my cfg file "one2one.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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root$12</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306
/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/sst/student/Student.hbm.xml"/>
<mapping resource="com/sst/student/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
You need a one-to-many mapping in Address hbm.xml.