Search code examples
javaspring-boothibernateone-to-manymany-to-one

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


I am trying to complete below project to update database with one to many and many to one relationship using hibernate and java annotation with out hbm file. where one user can have multiple laptop. my project is working fine with NetBeans, eclipse. how ever throwing above error in web IDE. am I missing something in code.

User entity-:

@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;
    }
}

Laptop entity-:

@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 + "]";
    }
}

my test 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","[email protected]");
        
        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());
        
        
    }
}

Please note few things before giving any inputs -:
1> my hibernate.cfg.xml is in the src folder
2> I did tried with configure ("hibernate.cfg.xml") even full path
3> my project is working fine in IDE like NetBeans and eclipse not working in web ide

Please help me out if I am missing something


Solution

  • As you already tried so many solution for your issue, but these didn't worked for your project. Hence I am sharing what you can do a bit. Please check if this can help...

    1> move your configuration file to your main project folder and then provide path to the configure("hibernate.cfg.xml") if throw's error check in error message what path it is taking as default (error message will show you the path along with error). try giving full path if it is not taking default path. also try below with reference path as ...

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

    some times it works :)

    2> Try to check database readiness with java configuration with out hibernate.cfg.xml file. you can try as below.. (below based on mysql chnage details if using any other db)

     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();
    

    here at least you will get fare idea of your issue.

    hope this will contribute to solve your prob.