java - Exception in thread "main" org.hibernate.MappingException -
test:
import java.util.gregoriancalendar; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import it.s.monitoringdoor.model.door_open; import it.s.monitoringdoor.model.hibernateutilities; public class testhibernate { public static void main(string[] args){ sessionfactory factory = hibernateutilities.getsessionfactory(); door_open d = new door_open(); d.setid(1); d.setid_door(1);; d.settimestamp(gettime()); session s = factory.opensession(); transaction t = s.gettransaction(); t.begin(); s.save(d); t.commit(); } private static date gettime(){ gregoriancalendar gc = new gregoriancalendar(); return gc.gettime(); } }
hibernateutilities:
package it.s.monitoringdoor.model; import java.util.properties; import org.hibernate.sessionfactory; import org.hibernate.boot.registry.standardserviceregistrybuilder; import org.hibernate.cfg.configuration; import org.hibernate.service.serviceregistry; public class hibernateutilities { //xml based configuration private static sessionfactory sessionfactory; private static sessionfactory buildsessionfactory() { try { // create sessionfactory hibernate.cfg.xml configuration configuration = new configuration(); configuration.configure("hibernate.cfg.xml"); system.out.println("hibernate configuration loaded"); serviceregistry serviceregistry = new standardserviceregistrybuilder().applysettings(configuration.getproperties()).build(); system.out.println("hibernate serviceregistry created"); sessionfactory sessionfactory = configuration.buildsessionfactory(serviceregistry); return sessionfactory; } catch (throwable ex) { // make sure log exception, might swallowed system.err.println("initial sessionfactory creation failed." + ex); throw new exceptionininitializererror(ex); } } public static sessionfactory getsessionfactory() { if(sessionfactory == null) sessionfactory = buildsessionfactory(); return sessionfactory; } }
the pojo class door_open.java
package it.selco.monitoringdoor.model; import java.util.date; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.table; @entity @table(name = "door_open") public class door_open { //---------------- instance variables ---------------- @id @generatedvalue @column(name = "id") private int id; @column(name = "id_door", nullable = false) private int id_door; @column(name = "id_door", nullable = false) private date timestamp; //-------------------- constructor -------------------- public door_open (){ ; } public door_open(int id_door, date timestamp) { setid_door(id_door); settimestamp(timestamp); } //--------------------- & set --------------------- public int getid() { return id; } public void setid(int id) { this.id = id; } @suppresswarnings("unused") private int getid_door() { return id_door; } public void setid_door(int id_door) { this.id_door = id_door; } @suppresswarnings("unused") private date gettimestamp() { return timestamp; } public void settimestamp(date timestamp) { this.timestamp = timestamp; } //--------------------- methods ---------------------- //todo auto-generated methods variables block }
hibernate.cfg.xml
<?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 name=""> <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/monitor_door</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"/> <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> <!-- ############################################ # mapping files external dependencies # ############################################ --> <mapping resource="it/s/monitoringdoor/model/door_open.hbm.xml"/> </session-factory> </hibernate-configuration>
door_open.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="door_open" table="door_open" schema="monitor_door"> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="id_door" column="id_door" type="int" not-null="true"/> <property name="timestamp" column="timestamp" type="timestamp" not-null="true"/> </class> </hibernate-mapping>
when run test returned me following error.
exception in thread "main" org.hibernate.mappingexception: unknown entity: it.s.monitoringdoor.model.door_open [...]
some advice?
look @ package of door_open
class, is: it.selco.monitoringdoor.model
while in hibernate.cfg.xml
door_open.hbm.xml
file at: <mapping resource="it/s/monitoringdoor/model/door_open.hbm.xml"/>
. package , mapping resource path not match, should same e.g. it/selco/monitoringdoor/model/door_open.hbm.xml
, of course move door_open.hbm.xml
in same path class.
hope helps.
ps, why using both annotations (e.g. @entity) , hbm mapping class? think should choose 1 not both.
Comments
Post a Comment