【问题标题】:org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]org.hibernate.internal.util.config.ConfigurationException:找不到 cfg.xml 资源 [hibernate.cfg.xml]
【发布时间】:2021-03-22 12:34:31
【问题描述】:

我正在尝试使用 hibernate 和 java 注释而不使用 hbm 文件来完成以下项目,以使用一对多和多对一的关系更新数据库。一个用户可以拥有多台笔记本电脑。我的项目在 NetBeans、eclipse 上运行良好。如何在 Web IDE 中抛出上述错误。我在代码中遗漏了什么吗?

用户实体-:

@Entity
public class User {  
    
    @Id
    private int id;       
    private String first_name;       
    private String last_name;        
    private String email;
       
    @OneToMany(targetEntity = Laptop.class, mappedBy = "User", cascade = CascadeType.ALL, fetch = FetchType.LAZY)      
    private List<Laptop> laptop = new ArrayList<Laptop>();
    public User(int id, String first_name, String last_name, String email, List<Laptop> laptop) {
        super();
        this.id = id;
        this.first_name = first_name;
        this.last_name = last_name;
        this.email = email;
        this.laptop = laptop;
    }
    public List<Laptop> getLaptop() {
        return laptop;
    }
    public void setLaptop(List<Laptop> laptop) {
        this.laptop = laptop;
    }
    public User(int id, String first_name, String last_name, String email) {
        super();
        this.id = id;
        this.first_name = first_name;
        this.last_name = last_name;
        this.email = email;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", first_name=" + first_name + ", last_name=" + last_name + ", email=" + email
                + ", laptop=" + laptop + "]";
    }
    public User(String first_name, String last_name, String email) {
        super();
        this.first_name = first_name;
        this.last_name = last_name;
        this.email = email;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirst_name() {
        return first_name;
    }
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

笔记本电脑实体-:

@Entity
public class Laptop {
    
    @Id
    private int laptop_id;
    private String laptop_name;
    @ManyToOne        
        @JoinColumn(name = "user_id", referencedColumnName = "id") //, nullable = false
    private User User;
    public User getUser() {
        return User;
    }
    public Laptop() {
        super();
        
    }
    public void setUser(User user) {
        this.User = user;
    }
    public int getId() {
        return laptop_id;
    }
    public void setId(int id) {
        this.laptop_id = id;
    }
    public String getLaptop_name() {
        return laptop_name;
    }
    public void setLaptop_name(String laptop_name) {
        this.laptop_name = laptop_name;
    }
    public Laptop(int id, String laptop_name) {
        super();
        this.laptop_id = id;
        this.laptop_name = laptop_name;
    }
    @Override
    public String toString() {
        return "Laptop [id=" + laptop_id + ", laptop_name=" + laptop_name + ", user=" + User + "]";
    }
}

我的测试课-:

@Transactional
public class user {

    private Session session= null;
    @Before
    public void before() {
        Configuration config = new Configuration().configure().addAnnotatedClass(User.class)
                .addAnnotatedClass(Laptop.class);
        SessionFactory factory = config.buildSessionFactory();
        session = factory.openSession();
    }
    
    @org.junit.Test
    public void test1() throws Exception{
        //LaptopRepository lr; 
        User user = new User(100,"Escofe","Labro","Escofe@Labro");
        
        Laptop laptop = new Laptop(2,"MACbookPro");
        laptop.setUser(user);
        user.getLaptop().add(laptop);
        Transaction tx = session.beginTransaction();
        session.save(user);
        session.save(laptop);
        tx.commit();
        assertEquals("MACbookPro", laptop.getLaptop_name());
    }

    @org.junit.Test
    public void test2() throws Exception{
        //LaptopRepository lr; 
        User user = new User(100,"Anushka","Shetty","anushka@gmali.in");
        
        Laptop laptop = new Laptop(1,"DELLXPS");
        laptop.setUser(user);
        user.getLaptop().add(laptop);
        Transaction tx = session.beginTransaction();
        session.save(user);
        session.save(laptop);
        tx.commit();
        assertEquals("Anushka", user.getFirst_name());
        assertEquals("DELLXPS", laptop.getLaptop_name());
        
        
    }
}

在给出任何输入之前请注意几件事 - :
1> 我的 hibernate.cfg.xml 在 src 文件夹中
2> 我确实尝试过配置(“hibernate.cfg.xml”)甚至完整路径
3> 我的项目在 NetBeans 等 IDE 中运行良好,而 eclipse 在 web ide 中无法运行

如果我遗漏了什么,请帮助我

【问题讨论】:

  • 你在使用 Spring Boot 为什么要在你的测试中创建 SessionFactory?
  • 据我所知,我做到了。我将不胜感激任何建议更好的方法来做到这一点。但是我的问题不是这样。测试也工作正常是它在 Web IDE 中给出错误(org.hibernate.internal.util.config.ConfigurationException:无法找到 cfg.xml 资源 [hibernate.cfg.xml])

标签: java spring-boot hibernate one-to-many many-to-one


【解决方案1】:

因为您已经为您的问题尝试了很多解决方案,但这些都不适用于您的项目。因此,我正在分享您可以做的事情。请检查这是否有帮助...

1> 将您的配置文件移动到您的主项目文件夹,然后提供配置文件的路径(“hibernate.cfg.xml”)如果 throw 的错误检查错误消息中的默认路径(错误消息将显示您路径以及错误)。如果不采用默认路径,请尝试提供完整路径。也可以尝试下面的参考路径为...

File f = new File(""hibernate.cfg.xml"");
    
        Configuration config = new Configuration().configure(f).addAnnotatedClass(User.class)
                .addAnnotatedClass(Laptop.class);

有时它会起作用:)

2> 尝试在没有 hibernate.cfg.xml 文件的情况下使用 java 配置检查数据库准备情况。您可以尝试如下..(如果使用任何其他数据库,以下基于 mysql chnage 详细信息)

 Configuration cfg = new Configuration()
      .addAnnotatedClass(your clas)
      .addAnnotatedClass(your clas)

      .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect")
      .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
      .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/yourdb")
      .setProperty("hibernate.connection.username", "your username")
      .setProperty("hibernate.connection.password", "your password")
      .setProperty("hibernate.hbm2ddl", "create")
      .setProperty("hibernate.show_sql", "true");

      cfg.configure();

      SessionFactory sf = cfg.buildSessionFactory();

      Session session = sf.openSession();

      Transaction tx = session.beginTransaction();

至少在这里你会了解你的问题。

希望这将有助于解决您的问题。

【讨论】:

  • 谢谢伙计.. 终于找到了根本原因并成功了。尝试了 cfg.xml。
猜你喜欢
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2021-07-18
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
相关资源
最近更新 更多