Cannot modify hibernate settings - java

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

Related

hibernate can't configure POJO by *.hbm.xml

I'm new to hibernate and I wrote a simply bean: User. Although I can configure it directly using configuration.addResource("User.hbm.xml") in my java code, but when I configure it through hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- JDBC connection pool(use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">none</property>
<!-- List of XML mappings -->
<mapping resource="User.hbm.xml"/>
</session-factory>
The application does not configure User entity and it runs with the error:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.philip.fin.test.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.philip.fin.test.TestHibernate.main(TestHibernate.java:37)
the User.hbm.xml file pasted below:
<hibernate-mapping>
<class name="com.philip.fin.test.User" table="User_table" catalog="test">
<id name="userId" column="User_Id">
<generator class="increment"/>
</id>
<property name="userName" column="User_Name" type="string"/>
<property name="userMessage" column="User_Message type="string"/>
</class>
</hibernate-mapping>

Criteria.list() returns an empty collection, no query executed

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

OGM and ORM named queries in xml file

My application contains both ORM and OGM. For ORM I was wrote some named queries in xyz.hbm.xml. But while using OGM those queries caused for exception. I am using OGM 4.1.3.Final version. Please help someone.
example.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 Jan 1, 2015 1:53:57 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="mkcl.os.apps.edumiss.model.student.Admission" table="ADMISSION">
<id name="id" type="java.lang.String">
<column name="ADMISSION_ID" />
<generator class="uuid" />
</id>
<property name="studentId">
<column name="STUDENTID" />
</property>
<property name="schoolId">
<column name="SCHOOL_ID" />
</property>
<property name="academicYearId" type="int">
<column name="ACADEMICYEARID" />
</property>
<property name="sectionRollNumber" type="int">
<column name="SECTIONROLLNUMBER" />
</property>
<property name="admissionDate" type="java.util.Date">
<column name="ADMISSIONDATE" />
</property>
<property name="schoolLeavingDate" type="java.util.Date">
<column name="SCHOOL_LEAVING_DATE" />
</property>
<property name="standardId" type="short">
<column name="STANDARDID" />
</property>
<property name="standardName" type="java.lang.String">
<column name="STANDARD_NAME" />
</property>
<property name="sectionId">
<column name="SECTION_ID" />
</property>
<property name="sectionName">
<column name="SECTION_NAME" />
</property>
<property name="reasonForLeaving">
<column name="REASON_FOR_LEAVING" />
</property>
<property name="streamId" type="short">
<column name="STREAM_ID" />
</property>
<property name="streamName" type="java.lang.String">
<column name="STREAM_NAME" />
</property>
<property name="admissionType" column="ADMISSION_TYPE">
<type name="org.hibernate.type.EnumType">
<param name="useNamed"></param>
<param name="enumClass">mkcl.os.apps.edumiss.model.student.AdmissionType</param>
</type>
</property>
<property name="createdBy" type="java.lang.String">
<column name="CREATED_BY" />
</property>
<property name="createOn" type="java.util.Date">
<column name="CREATED_ON" />
</property>
<property name="modifiedBy" type="java.lang.String">
<column name="MODIFIED_BY" />
</property>
<property name="lastModified" type="java.util.Date">
<column name="LAST_MODIFIED" />
</property>
<property name="patternId" type="integer">
<column name="PATTERN_ID"></column>
</property>
</class>
<sql-query name="getAdmissionForAcademicYear">
<return alias="admission" class="mkcl.os.apps.edumiss.model.student.Admission"></return>
<![CDATA[
SELECT
{admission.*}
FROM
ADMISSION admission
INNER JOIN STUDENT s
ON
s.CURRENT_ADMISSION_ID = admission.ADMISSION_ID
WHERE
admission.STUDENTID = :mkclIdentificationNumber
AND
admission.ACADEMICYEARID = :academicYearId
]]>
</sql-query>
</hibernate-mapping>
this throws
java.lang.IncompatibleClassChangeError: class org.objectweb.asm.tree.ClassNode has interface org.objectweb.asm.ClassVisitor as super class
if I am removing above query from hbm file then it worked fine. But I have to keep those queries in .hbm.xml file as it is.
That query is for a relational database, so you don't need the OGM for that.
Why do you want to run an SQL query against OGM, which is for NoSQL?
The default hibernate.archive.autodetection property value is class,hbm, so make sure you set it to class in the persistence.xml file that's associated to the OGM EntityManagerFactory. You do have two separate persistence.xml configurations (one for ORM and one for OGM), right?
according to current version of OGM(5.0) feature document:
Model can not possible to share between OGM and ORM together and this will projected to resolve in next version of OGM.
ORM classes can try to parse the .hbm.xml file and find the named queries which having the syntax of mongo query. so while reading the file OGM unable to parse the syntax of SQL named query and throws an error so I have only way to shift the all the queries into another hbm whose wntry will not be present in nosql_hibernate.xml file so OGM will not parse and my code will run fast else till this feature not support shift to another framework.

Java hbm.xml multiple index of one column

For example,
<?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 package="com.sql.index">
<class name="User">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="firstName" type="string" index="IDX_FIRST_NAME" />
<property name="lastName" type="string" />
<property name="address" type="string" />
<property name="field_1" type="long" />
<property name="field_2" type="long" />
</class>
</hibernate-mapping>
If I want the field_1 and field_2 has 2 indexes description. Can I do the following thing? Or How to achieve it ?
<property name="field_1" type="long" index="idx_1,idx_2"/>
<property name="field_2" type="long" index="idx_1,idx_3"/>
The field_1 and field_2 will has 2 index for their self.
I refer the hibernate 3.6, 5.1.4.2 Property mapping with hbm.xml, it seems like the index field can be assigned by only one column.
PS,
The project is some kind old, and is maintained by many people, so I cannot use annotation syntax to add index.
I found the post and give it a try.
<?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 package="com.sql.index">
<class name="User">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="firstName" type="string" index="IDX_FIRST_NAME" />
<property name="lastName" type="string" />
<property name="address" type="string" />
<property name="field_1" type="long" index="idx_2"/>
<property name="field_2" type="long" index="idx_3"/>
</class>
<database-object>
<create>
CREATE INDEX idx_1 ON User (field_1, field_2)
</create>
<drop></drop>
</database-object>
</hibernate-mapping>
This problem can be solved by <database-object>, by writing native sql syntax to create index.
UPDATE, 2018/11
For unique constraint with multiple properties, someone has answered it.
Use properties tag
<properties name="uk1" unique="true">
<property name="username" .../>
<many-to-one name="client" .../>
</properties>
<properties name="uk2" unique="true">
<property name="email" .../>
<many-to-one name="client" update="false" insert="false" .../>
</properties>

java.lang.ExceptionInInitializerError in the creation of hibernate configuration

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.

Categories