Я начал использовать Hibernate сегодня и протестировал простой пример, но получаю сообщение об ошибке: hibernate.cfg.xml не найден.
Я поместил файл hibernate.cfg.xml в папку src, и вот его содержание:
Код: Выделить всё
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
test
test
1
org.hibernate.dialect.MySQLDialect
thread
org.hibernate.cache.internal.NoCacheProvider
true
update
Код: Выделить всё
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory()
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure("hibernate.cgf.xml").buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
My class Test.java :
Код: Выделить всё
import ...
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ProductDao pd = new ProductDao();
try {
Product p = new Product("PC", 1000L);
pd.add(p);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void add(Product p) {
Код: Выделить всё
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.beginTransaction();
session.save(p);
tx.commit();
Thanks in advance
Источник: https://stackoverflow.com/questions/273 ... nd-eclipse