java - Exception in Hibernate config file while mapping my bean -
hello not in hibernate. when excecute class test. display in console config file xml of mapping (resource: com.live.hibernate.user.hbm.xm) not found. simple example :
user.java :
package com.live.beans; public class user { private int id; private string name; private string email; private string password; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { name = name; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public user(int id, string name, string email, string password) { super(); this.id = id; name = name; this.email = email; this.password = password; } @override public string tostring() { return "user [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + "]"; } }
user.hbm.xml :
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-configuration public "-//hibernate/hibernate mapping dtd//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class> <id name="id" type="int" column="id" length="15"/> <property name="name" type="string" column="name" length="45" /> <property name="email" type="string" column="email" length="45" /> <property name="password" type="string" column="password" length="45" /> </class> </hibernate-mapping>
i created simple class java test (hibernatetest.java) :
package com.live.presentation; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.configuration; import com.live.beans.user; public class hibernatetest { private session session; private void opensession(){ configuration conf = new configuration(); sessionfactory sessionfactory= conf.configure().buildsessionfactory(); session session = sessionfactory.opensession(); session.begintransaction(); } private void closesession(){ session.gettransaction().commit(); session.close(); } public hibernatetest() { opensession(); user p = new user(1, "ali", "ali@gmail.com", "password"); session.save(p); system.out.println("sauvegarde reussi"); closesession(); } public static void main(string[] args) { new hibernatetest(); } }
pom.xml :
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>p01</groupid> <artifactid>p01</artifactid> <version>0.0.1-snapshot</version> <packaging>war</packaging> <build> <sourcedirectory>src</sourcedirectory> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifactid> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <artifactid>maven-war-plugin</artifactid> <version>2.4</version> <configuration> <warsourcedirectory>webcontent</warsourcedirectory> <failonmissingwebxml>false</failonmissingwebxml> </configuration> </plugin> </plugins> </build> <name>hibernatetest</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupid>com.sun.faces</groupid> <artifactid>jsf-api</artifactid> <version>2.1.7</version> </dependency> <dependency> <groupid>com.sun.faces</groupid> <artifactid>jsf-impl</artifactid> <version>2.1.7</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.0.1.final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-validator</artifactid> <version>4.2.0.final</version> </dependency> <dependency> <groupid>org.hibernate.common</groupid> <artifactid>hibernate-commons-annotations</artifactid> <version>4.0.1.final</version> <classifier>tests</classifier> </dependency> <dependency> <groupid>org.hibernate.javax.persistence</groupid> <artifactid>hibernate-jpa-2.0-api</artifactid> <version>1.0.1.final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-entitymanager</artifactid> <version>4.0.1.final</version> </dependency> <dependency> <groupid>javax.validation</groupid> <artifactid>validation-api</artifactid> <version>1.0.0.ga</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>1.6.4</version> </dependency> <dependency> <groupid>org.jboss.logging</groupid> <artifactid>jboss-logging</artifactid> <version>3.1.0.cr2</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.6.4</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.10</version> </dependency> </dependencies> </project>
and console:
log4j:warn no appenders found logger (org.jboss.logging). log4j:warn please initialize log4j system properly. log4j:warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig more info. exception in thread "main" org.hibernate.mappingnotfoundexception: resource: com.live.hibernate.user.hbm.xml not found @ org.hibernate.cfg.configuration.addresource(configuration.java:724) @ org.hibernate.cfg.configuration.parsemappingelement(configuration.java:2102) @ org.hibernate.cfg.configuration.parsesessionfactory(configuration.java:2074) @ org.hibernate.cfg.configuration.doconfigure(configuration.java:2054) @ org.hibernate.cfg.configuration.doconfigure(configuration.java:2007) @ org.hibernate.cfg.configuration.configure(configuration.java:1922) @ org.hibernate.cfg.configuration.configure(configuration.java:1901) @ com.live.presentation.hibernatetest.opensession(hibernatetest.java:15) @ com.live.presentation.hibernatetest.<init>(hibernatetest.java:26) @ com.live.presentation.hibernatetest.main(hibernatetest.java:35)
edited
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url">jdbc:mysql://localhost/ment</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <property name="hibernate.show_sql">false</property> <mapping resource="com.live.hibernate.user.hbm.xml" /> </session-factory> </hibernate-configuration>
folder structure :
src |-- com |-- live | |-- hibernate | | `-- user.hbm.xml live |`--beans | `-- user.java live `--presentation `-- hibernatetest.java webcontent |-- meta-inf |-- web-inf | |-- index.xhtml pom.xml
something else remark when debug, have exception here :
sessionfactory sessionfactory= conf.configure().buildsessionfactory();
you know buildsessionfactory() method deprecated . idea ?
please me !
where hibernate.cfg.xml
?
make sure put hibernate.cfg.xml
, user.hbm.xml
@ same source folder , inside hibernate.cfg.xml
put this:
<mapping resource="user.hbm.xml" />
Comments
Post a Comment