我看过很多帖子,但没有人适合我的问题。 我把hibernate.cgf.xml放在" src"根据我在互联网上阅读一些例子所学到的东西来看日食的文件夹。
但是当我运行我的应用程序时,我仍然得到错误"could not find file: hibernate.cgf.xml"
这是我的网络应用程序
这是启动应用程序的类
public class TestHibernateInsert {
public static void main(String[] args)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//Add new Employee object
EmployeeEntity emp = new EmployeeEntity();
emp.setEmail("lokesh@mail.com");
emp.setNome("Nome");
emp.setCognome("Cognome");
emp.setPassword("Password");
//Save the employee in database
session.save(emp);
//Commit the transaction
session.getTransaction().commit();
HibernateUtil.shutdown();
}
}
这是SessionFactory类
public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory()
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure(new File("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();
}
}
这是持久对象类
Entity
@Table(name = "utente")
public class EmployeeEntity implements Serializable{
private static final long serialVersionUID = -1798070786993154676L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "email", unique = true, nullable = false, length = 100)
private String email;
@Column(name = "nome", unique = false, nullable = false, length = 100)
private String nome;
@Column(name = "cognome", unique = false, nullable = false, length = 100)
private String cognome;
@Column(name = "password", unique = false, nullable = false, length = 100)
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCognome() {
return cognome;
}
public void setCognome(String cognome) {
this.cognome = cognome;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//Getters and setters
}
答案 0 :(得分:0)
您似乎已经使用eclipse开发了一个Web应用程序。默认情况下,hibernate在类路径的根目录中查找hibernate.cfg.xml
文件。
所以最后该文件应放在WEB-INF/classes
目录中,该目录是Web应用程序的类路径的根目录。
答案 1 :(得分:0)
请将您的文件从hibernate.cgf.xml
重命名为hibernate.cfg.xml
并在HibernateUtil类中更改buildSessionFactory()
方法
private static SessionFactory buildSessionFactory()
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().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);
}
}
答案 2 :(得分:0)
将现有配置文件重命名为hibernate.cfg.xml
或使用以下代码创建会话
配置cfg = new Configuration(); cfg.configure( “hibernate.cgf.xml”);