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>
Related
I'm trying to make a query to the database and I get this:
java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: generalsetup is
not mapped [FROM generalsetup]
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)
org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
org.apache.jsp.Environments_jsp._jspService(Environments_jsp.java:188)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
IMPORTANT: I can write to the database but I can't read.
This is my code that tries to get a list from (I have a error mark under generalsetup:
SessionFactory factory = HibernateUtil.GetSessionFactory();
Session hibernateSession = factory.openSession();
Transaction tx = null;
try {
tx = hibernateSession.beginTransaction();
List environments = hibernateSession.createQuery("FROM generalsetup").list();
for (Iterator iterator = environments.iterator(); iterator.hasNext();) {
Environment environment = (Environment) iterator.next();
System.out.println(environment.getName());
}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
hibernateSession.close();
}
My GeneralSetup.hbm.xml
<?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.atp.servlets.Model.GeneralSetup" table="generalsetup">
<meta attribute="class-description">
This class contains the general_setup detail.
</meta>
<id name="id" type="int" column="idGeneralSetup">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="hasProfile7" column="hasProfile7" type="boolean"/>
<property name="hasWebAdmin" column="hasWebAdmin" type="boolean"/>
<property name="hasWebClient" column="hasWebClient" type="boolean"/>
<property name="hasWebCSR" column="hasWebCSR" type="boolean"/>
<property name="profile7Branch" column="profile7Branch" type="string"/>
<property name="webAdminBranch" column="webAdminBranch" type="string"/>
<property name="webClientBranch" column="webClientBranch" type="string"/>
<property name="webCSRBranch" column="webCSRBranch" type="string"/>
<property name="initialDayEnd" column="initialSystemDate" type="string"/>
<property name="finalDayEnd" column="finalSystemDate" type="string"/>
<property name="amountOfDayEnds" column="amountOfDayEnds" type="int"/>
<property name="user" column="user" type="string"/>
</class>
</hibernate-mapping>
my config:
<?xml version="1.0" encoding="utf-8"?>
<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/mydb
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
qetwr123
</property>
<!-- List of XML mapping files -->
<mapping resource="com/atp/servlets/Model/GeneralSetup.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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 got this problem and don't know how to handle it.
Here is my Member.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 12.07.2013 13:28:27 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="MyApp.WebApp.model.Member" table="MEMBER">
<id name="id" type="java.lang.Long" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="surname" type="java.lang.String">
<column name="SURNAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="username" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="result" type="java.lang.String" access="field">
<column name="RESULT" />
</property>
</class>
</hibernate-mapping>
My hbm.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</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="MyApp/WebApp/model/Member.hbm.xml"/>
</session-factory>
the exception:
13:31:48,073 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC00001: Failed to start service jboss.persistenceunit."WebApp.war#primary": org.jboss.msc.service.StartException in service jboss.persistenceunit."WebApp.war#primary": Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [classes.jar:1.6.0_51]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [classes.jar:1.6.0_51]
at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_51]
Caused by: org.hibernate.MappingException: Error while parsing file: Member.hbm.xml
at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1255)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1047)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:692)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:72)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
... 3 more
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:106)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:470)
at org.hibernate.cfg.Configuration.add(Configuration.java:643)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:681)
at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1247)
... 10 more
Caused by: org.dom4j.DocumentException: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory Nested exception: org.dom4j.DocumentFactory cannot be cast to org.dom4j.DocumentFactory
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:76)
... 15 more
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.