I am learning hibernate and I can't figure out why is this error popping up. I tried searching but I couldn't find a solution that helped me. I'd like to learn why am I getting this error.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: from Destination
Here are some details:
main():
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
// Destination destination = new Destination();
// destination.setName("IDelhi");
// destination.setLatitude(1.0f);
// destination.setLongitude(123.0f);
// session.save(destination);
List result = session.createCriteria("from Destination").list();
session.getTransaction().commit();
session.close();
// for (Object dest : result) {
// Destination d = (Destination)dest;
// System.out.println(d.getId() + ": "+ d.getName());
// }
}
}
When I try to insert the destination (commented code), values are being inserted into db.
Configurations:
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 name="">
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.default_schema">wah_schema</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping class="org.wah.dao.Destination" resource="org/wah/dao/Destination.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Destination.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 25, 2012 3:31:00 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="org.wah.dao.Destination" table="DESTINATION">
<id name="id" type="int">
<column name="ID"/>
<generator class="identity"/>
</id>
<property generated="never" lazy="false" name="name" type="java.lang.String">
<column name="NAME"/>
</property>
<property generated="never" lazy="false" name="latitude" type="float">
<column name="LATITUDE"/>
</property>
<property generated="never" lazy="false" name="longitude" type="float">
<column name="LONGITUDE"/>
</property>
</class>
</hibernate-mapping>
Can someone please help me figure this out ?
use session.createCriteria(Destination.class); You are trying to use HQL - Hibernate Query Language for which you need to use other api like
Query query = session.createQuery("from Destination");
List list = query.list();
<mapping class="org.wah.dao.Destination"
resource="org/wah/dao/Destination.hbm.xml"/>
instead of this, please change it to
<mapping resource="org/wah/dao/Destination.hbm.xml">
Hibernate throws this error because when it expect some Annotation based class, you are referring the XML based Hibernate mapping. Stick to two different rule Annotations or XML based.
You are mixing HQL and Criteria query. You should either do
session.createCriteria(Destination.class).list();
or
session.createQuery("from Destination").list();
According to here, something like this should work:
List result = session.createCriteria(Destination.class).list();
I had similar problem for a simple Console application trying to use Hibernate. The solution I arrived to make the add the "packagesToScan" property explicitly for LocalSessionFactoryBean.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.mg.learning.spring.orm"/> <--- this SOLVED!
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
Related
I'm making this simple app using Hibernate to do some CRUD operations. I'm having this error in the query generated by hibernate
Config XML:
<hibernate-configuration>
<session-factory>
<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:3306/library</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_schema">library</property>
<property name="hbm2ddl.auto" value="create"/>
<!-- Mappings -->
<mapping resource="librarysystem/mappings/Role.hbm.xml"/>
<mapping resource="librarysystem/mappings/Librarian.hbm.xml"/>
<mapping resource="librarysystem/mappings/Task.hbm.xml"/>
<mapping resource="librarysystem/mappings/Library.hbm.xml"/>
</session-factory>
</hibernate-configuration>
POJO:
public class Library implements java.io.Serializable {
private int id;
private String name;
private Set librarians = new HashSet(0);
//Constructors, getters and setters...
}
Mapping:
<hibernate-mapping>
<class name="librarysystem.entities.Library" table="library" catalog="library" optimistic-lock="version">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="45" />
</property>
<set name="librarians" table="librarian" inverse="true" lazy="true" fetch="select">
<key>
<column name="libraryid" not-null="true" />
</key>
<one-to-many class="librarysystem.entities.Librarian" />
</set>
</class>
</hibernate-mapping>
Code to get the list:
List<Library> libraries = session.createQuery("FROM Library").list();
When I run a query to get the list of libraries, an exception occurs saying that the query syntax is wrong
the log output of the query is:
select
library0_.id as id1_2_,
library0_.name as name2_2_
from
library.library.library library0_
How did the library.library.library happened?
Any help please?
I've given the minimum details as I thought was necessary. If you need more code to find the error (like other POJOs and mappings), please inform me
How did the library.library.library happened? Any help please?
I'm no expert here but I think the tripple library here comes from the table being named library within a Database (catalog) named library.
<class name="librarysystem.entities.Library" table="library" catalog="library" optimistic-lock="version">
The table name and the catalog name are the same. So we are looking for a catalog called library library within which we want a table called library library.library. Although I'm not 100% on the third library, I think I'm in the right direction.
On a side note, I find it easier to work with hibernate annotation instead of xml mapping.
Difference between annotation and xml mapping in Hibernate
XML mapping example
Annotation example
I am getting this error:
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.hibernate.Session.close()Ljava/sql/Connection;
When calling this function:
private static SessionFactory factory;
public static void insert(Object model) {
factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
try {
session.save(model);
tx.commit();
} catch (HibernateException exc) {
if (tx != null)
tx.rollback();
exc.printStackTrace();
} finally {
session.close(); // here is when the error occurs
}
}
I believe I have imported all the necessary jars from /lib/required/ directory in the latest Hibernate 5.0.1.
The hibernate.cfx.xml file is also correctly configured:
<?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.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:3307/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property >
<mapping resource="customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And customer.hbm.xml mapping file:
<class name="com.test.app.model.Customer" table="customer">
<meta attribute="class-description">
Test.
</meta>
<id column="id" name="id" type="int">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="phoneNumbers" column="phone_numbers" type="string"/>
<property name="emailAddresses" column="email_addresses" type="string"/>
<property name="address" column="address" type="string"/>
<property name="note" column="note" type="string"/>
</class>
What could be the issue?
Looks like you have two hibernate-core-5.0.1.Final.jar in your classpath. For example, if you run a project in the IDE and add other project with hibernate-core-5.0.1.Final.jar to your classpath.
P.S. Please don't configure the session factory in every insert, and rollback on any exception, not only on HibernateException. You can see how properly working with transactions here, and you can find a simply example console project using Hibernate 5 and MySQL here.
I'm using hibernate framework for a desktop application (Swing) and I called the needed librairies for hibernte to work but I still get this exception when I create a SessionFactory:
private static SessionFactory factory = new Configuration(). configure(). buildSessionFactory();
And this is the list of used librairies:
My config file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="hibernate.connection.url">
jdbc:postgresql://localhost:5432/test_hibernate
</property>
<property name="hibernate.connection.username">
postgres
</property>
<property name="hibernate.connection.password">
root
</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My mapping file:
<?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="Employee" table="employee">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="salary" column="salary" type="double"/>
</class>
</hibernate-mapping>
Try removing encoding part from top level tag in both config and hbm file.
I am not sure if this will work for you, it worked for us.
I'm currently trying to learn to use Hibernate to store objects in a database. I've been working through a tutorial at https://netbeans.org/kb/docs/java/hibernate-java-se.html and I'm trying to create an XML file for a very simple class and table. However I'm just totally unable to get it to work and I'm now really getting frustrated. I just can't get it to work.
The database is Postgres 9.2, the class is one I wrote myself and the version of Hibernate is the one that ships with Netbeans 7.3 (3.2, I believe).
The table is as follows:
CREATE TABLE sellable.manufacturers
(
mfr_id serial NOT NULL, -- Manufacturer ID
mfr_name character varying(127) NOT NULL, -- Manufacturer name
CONSTRAINT manufacturers_pkey PRIMARY KEY (mfr_id),
CONSTRAINT manufacturers_mfr_name_key UNIQUE (mfr_name)
);
The class I'm trying to map it to is as follows:
package bikeshop.sellable;
import java.io.Serializable;
public class Manufacturer implements Serializable {
private Integer mfrId = null;
private String mfrName = null;
public Integer getMfrId () {
return this.mfrId;
}
public String getMfrName () {
return this.mfrName;
}
public void setMfrName (String MfrName) {
this.mfrName = MfrName;
}
}
The XML for the Hibernate 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 Aug 21, 2013 7:20:20 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="bikeshop.sellable.Manufacturer" table="manufacturers" schema="sellable">
<comment>Product manufacturers</comment>
<id name="mfrId" type="int">
<column name="mfr_id" />
<generator class="assigned" />
</id>
<property name="mfrName" type="string">
<column name="mfr_name" length="127" not-null="true" unique="true">
<comment>Manufacturer name</comment>
</column>
</property>
</class>
</hibernate-mapping>
And the Hibernate project config 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>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/bikeshop</property>
<property name="hibernate.connection.username">bikeshop</property>
<property name="hibernate.connection.password">bikeshop</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.format_sql">true</property>
<mapping class="bikeshop.sellable.Manufacturer" file="" jar="" package="" resource="bikeshop/mappings/Manufacturers.hbm.xml"/>
<mapping resource="bikeshop/mappings/Sellables.hbm.xml"/>
</session-factory>
</hibernate-configuration>
When I try using the HQL query editor, all I get is the query "select from" displayed, which obviously can't be executed. Attempting to execute it results in an exception.
org.hibernate.exception.SQLGrammarException: could not execute query
Why is this failing to generate a valid SQL query? What am I missing?
EDIT: I've been trying to get this to work, now I'm getting a different error. The HQL query window now displays the message "Unable to retrieve data from the database." and the exception has changed to "org.hibernate.PropertyNotFoundException: Could not find a setter for property mfrId in class bikeshop.sellable.Manufacturer"
The config file has changed to:
<?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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/bikeshop</property>
<property name="hibernate.connection.username">bikeshop</property>
<property name="hibernate.connection.password">bikeshop</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.format_sql">true</property>
<mapping class="bikeshop.sellable.Manufacturer" file="" jar="" package="bikeshop.sellable" resource="bikeshop/mappings/manufacturer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
and the mapping file has changed to:
<?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="bikeshop.sellable.Manufacturer" table="manufacturers" schema="sellable">
<comment>Product manufacturers</comment>
<id name="mfrId" type="int">
<column name="mfr_id" />
<generator class="native" />
</id>
<property name="mfrName" type="string">
<column name="mfr_name" length="127" not-null="true" unique="true">
<comment>Manufacturer name</comment>
</column>
</property>
</class>
</hibernate-mapping>
EDIT 2: As a last ditch attempt I made all the Manufacturer members public and changed the access type to field in the XML.
Now the HQL query works and returns results. But I obviously don't want to take that approach, public fields are a horrible idea!
It seems that the problem is that the HQL editor doesn't get updated classes or metadata all the time when you make changes to the class or its metadata, it also seems that this can happen regardless of whether you use an external XML file or inline annotations.
The only workaround I've found so far is to quit Netbeans and restart it. This makes the HQL query editor update, but is obviously quite annoying.
I'm trying to use the Hibernate Code Generation feature in Hibernate Tools Eclipse Add-On. It is giving me the following error:
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Alert.hbm.xml
Could not parse mapping document from resource Alert.hbm.xml
org.hibernate.MappingException: class Alert not found while looking for property: alertId
class Alert not found while looking for property: alertId
org.hibernate.MappingException: class Alert not found while looking for property: alertId
class Alert not found while looking for property: alertId
java.lang.ClassNotFoundException: Alert
Alert
It is not finding the class Alert.java but I thought the Code Generator (hence the name...) was supposed to generate all the hibernate classes for me.
Using eclipse Indigo with Hibernate Tools 3.4.x.
Here's my hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<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/findata?tcpKeepAlive=true
</property>
<property name="connection.username">root</property>
<property name="connection.password">madmax1.</property>
<property name="connection.pool_size">2</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.mysqldialect
</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<mapping resource="Alert.hbm.xml" />
<mapping resource="Entity.hbm.xml" />
<mapping resource="FactData.hbm.xml" />
<mapping resource="TimeEvent.hbm.xml" />
<mapping resource="User.hbm.xml" />
<mapping resource="AlertTarget.hbm.xml" />
<mapping resource="LogAlert.hbm.xml" />
</session-factory>
</hibernate-configuration>
Here's Alert.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="Alert" table="alerts">
<id name="alertId" column="id">
<generator class="assigned"/>
</id>
<property name="limitValue" column="limit_value" type="decimal" />
<!-- The unique=true property makes the many-to-one a one-to-one relationship -->
<many-to-one name="alertEntity"
class="Entity" column="entity_id"
not-null="true" cascade="all" unique="true"/>
<set name="alert_targets" table="alerts_alert_targets" cascade="all">
<key column="alert_id" />
<many-to-many column="alert_target_id"
class="AlertTarget" />
</set>
</class>
</hibernate-mapping>
weird it is looking for the to-be-generated class.
I will check the hibernate-reverse.xml file and check if it does not have additional attributes that may result in this.
Alternatively, during generation, try to setup the hibernate-revenge.xml and hibernate.cfg.xml rather than using the existing ones.
The cause turned out to be the fact that the "type" was not specified for the "id" property in Alert.hbm.xml.