I'm trying to store an object Ficheiro inside my in-memory database. This object contains:
private int version;
private Workbook content;
This is my code I'm using to store in the database:
HSQLServerUtil.getInstance().start("DBMemoria");
Ficheiro fich = new Ficheiro();
fich.setVersion(1);
fich.setContent(workbook);
byte[] by = serialize(fich);
try {
Blob blob = new SerialBlob(by);
Session ss = HibernateUtils.newSessionFactory("csheets\\ext\\Hibernate\\hibernate.cfg.xml").openSession();
ss.beginTransaction();
ss.getTransaction();
ss.save(workbook);
ss.getTransaction().commit();
ss.close();
} catch (SQLException ex) {
System.out.println("ERROR");
}
This is my hibernate.cfg.xml file:
<?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">org.hsqldb.jdbcDriver</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.url">jdbc:hsqldb:file:C:\Users\Adam\Documents\NetBeansProjects\lapr4_2dd_g1_s2\lib\Version</property>
<property name="hibernate.connection.username"/>
<property name="hibernate.connection.password"/>
<mapping resource="csheets/ext/Hibernate/File.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And finally this is my File.hbm.xml file:
<?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 dynamic-insert="false" dynamic-update="false" mutable="true" name="csheets.ext.Hibernate.Ficheiro" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="File">
<id column="version" name="version" type="int" unsaved-value="0">
<generator class="identity">
</generator>
</id>
<property column="content" name="content" type="blob"/>
<!--<property column="name" name="name" type="string"/>-->
</class>
</hibernate-mapping>
I'm getting the following error:
Exception in thread "AWT-EventQueue-0" org.hibernate.MappingException: Unknown entity: csheets.core.Workbook
I've already put an #Entity in the Workbook class. Anybody know whats wrong?
And if possible, can you tell me if i'm doing this right?
Thanks
Update your hibernate config to the following to make it work (for development machine):
<property name="hibernate.hbm2ddl.auto">update</property>
Related
I've i run into an exeption while trying to use Hibernate 4.3.x with java on netbeans, the exption is:
Exception in thread "main" java.lang.ExceptionInInitializerError
at teste.ConectaJavaDB.main(ConectaJavaDB.java:17)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource model/Equipe.hbm.xml
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3764)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3753)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3741)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at conexao.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
at conexao.HibernateUtil.<clinit>(HibernateUtil.java:14)
... 1 more
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping model.Equipe
at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2837)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:178)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3761)
... 7 more
(the second error says about duplicate mapping, but i dindt mapped twice)
by reading the exeption, it's possible to see that the problem is in the mapping xml of the class Equipe, the XML contains:
<?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="model">
<class name="model.Equipe" table="equipe">
<id name="id" type="int" column="ID" >
<generator class="increment"/>
</id>
<property name="nome"/>
<property name="dataNascimento"/>
<property name="email"/>
<property name="enderecoWeb"/>
<property name="fone"/>
</class>
</hibernate-mapping>
And the class:
package model;
import java.util.Date;
public class Equipe {
private int id;
private String nome;
private Date dataNascimento;
private String email;
private String enderecoWeb;
private String fone;
[..]getters and setters[..]
Hibernate.cgf
<?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:3306/mysql?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="model.Jogador"/>
<mapping resource="model/Equipe.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I can't seem to find the error, the hole code is posted on Bitbucket above, thanks!
Hole code: https://bitbucket.org/angelorodem/progapl_ii_272278_2015/src
You invoked configuration.configure() twice. Remove the second invocation, or just copy paste the code below:
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="model.Jogador"/> (remove this)
<mapping resource="model/Equipe.hbm.xml"/>
try this:
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="model/Equipe.hbm.xml"/>
I'm using hibernate for desktop application and I'm getting this exception:
Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Unable to read XML
Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 20; Element type "hibernate-mapping" must be declared.
This is my configuration XML file:
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test_hibernate</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And this is the mapping XML 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="model.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>
What did I do wrong?
From the hibernate-mapping-3.0.dtd I've in hibernate-core-4.3.7.Final.jar, your DOCTYPE declaration in Employee.hbm.xml should be like below
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Instead of
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Notice there's a 3.0 between DTD and //EN
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.
Im working in a maven project. Atm I only have entities , mappings and a configuration files. Im trying to test my hibernate mappings with junit.
I cant find the problem. I can validate the xml files in netbeans, but when i run my test I get the following error.
Validation in netbeans:
XML validation started.
Checking file:/C:/Users/Err/Documents/NetBeansProjects/PinkSocks/src/main/resources/hibernate.cfg.xml...
Referenced entity at "http://localhost:8080/files/hibernate-configuration-4.0.xsd".
XML validation finished.
Error:
Initial SessionFactory creation failed.org.hibernate.MappingException: invalid configuration
at bleh.mihihi.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
at bleh.mihihi.utils.HibernateUtil.<clinit>(HibernateUtil.java:16)
at bleh.mihihi.utils.HibernateUtilTest.setUp(HibernateUtilTest.java:20)
Caused by: org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1931)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1910)
at bleh.mihihi.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 25; Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration http://localhost:8080/files/hibernate-configuration-4.0.xsd">
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/pinksocks</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.pool_size">1</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.internal.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping class="bleh.mihihi.Beverage" file="Beverage.hbm.xml" />
</session-factory>
</hibernate-configuration>
Beverage.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping
xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://localhost:8080/files/hibernate-mapping-4.0.xsd"
package="bleh.mihihi.entities">
<class name="Beverage" table="beverages">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" />
<property name="alcohol" column="alcohol"/>
</class>
</hibernate-mapping>
Thanks in advance
Err
Edit1:
Things I tried:
retyping
using dtd
placing xsd to other places
With Hibernate 4.x you should use the same DTD as 3.x:
<?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>
as you can see by this bug report, where the deprecation warning was removed.
Also the tutorials for 4.2 version use this, as you can see here.
You forgot to specify DTD.
Try keeping below code in both the xml files and let me know
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
your forgot DTD in hibernate.cfg.xml file