Hi i am trying simple hibernate program but not able to fix this issue can anyone help
UserDetails.class
package org.pavan.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {
@Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
HibernateTest.java
package org.pavan.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.pavan.dto.UserDetails;
public class HibernateTest {
public static void main(String[] args) {
UserDetails user =new UserDetails();
user.setUserId(1);
user.setUserName("First User");
SessionFactory sessionFactory = new Configuration().configure("C:\\Users\\pavan\\workspace\\FirstHibernateProject\\src\\hibernate.cfg.xml").buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. -->
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="org.pavan.dto.UserDetails"/>
</session-factory>
Error:
INFO: Configuration resource: C:\Users\pavan\workspace\FirstHibernateProject\src\hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateException: C:\Users\pavan\workspace\FirstHibernateProject\src\hibernate.cfg.xml not found at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405) at org.hibernate.cfg.Configuration.configure(Configuration.java:1427) at org.pavan.hibernate.HibernateTest.main(HibernateTest.java:14)
Can anyone help please
thanks in advance
C:\Users\pavan\workspace\FirstHibernateProject\src\hibernate.cfg.xml
Does this file exist? The error says this file is not found. Where is yourhibernate.cfg.xml
file located? – user3778137C:\Users\pavan\workspace\FirstHibernateProject\src
? Really weird that it is not able to find it. Just make sure there are no typos in the filename. – user3778137