org.hibernate.internal.util.config.ConfigurationException: 找不到 cfg.xml 资源 [hibernate.cfg.xml]

org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

我正在尝试完成以下项目,使用休眠和 java 注释更新具有一对多和多对一关系的数据库,不带 hbm 文件。一个用户可以拥有多台笔记本电脑。我的项目在 NetBeans、eclipse 上运行良好。如何在网络中抛出以上错误 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 + "]";
    }
}

我的测试class-:

@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> 我的项目在 IDE 中运行良好,例如 NetBeans 和 eclipse 在网络中无法运行 ide

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

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

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

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

有时有效:)

2> 尝试使用 java 配置检查数据库是否准备就绪,但没有 hibernate.cfg.xml 文件。你可以尝试如下..(如果使用任何其他数据库,下面基于 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();

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

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