简体   繁体   中英

auto database creation in hibernate with mysql

this is my code, where i done mistake. while running Test.java am getting error like "The requested resource is not available". i kept all this files in same package

hibernate.cfg.xml

<hibernate-configuration>
   <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/studentdb </property>
    <property name="hibernate.connection.username">root</property>
   <property name="hibernate.connection.password">root</property>
 <property name="connection.pool_size"> 1</property> 

   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hbm2ddl.auto">create</property>
  <property name="show_sql">true</property>


   <!-- List of XML mapping files -->
   <mapping resource="student.hbm.xml"/>

   </session-factory>
</hibernate-configuration>

Test.java

public class Test {

    public static void main(String[] args) {
        Configuration cfg=new Configuration();
        cfg.configure("hibernate.cfg.xml");
        cfg.buildSessionFactory();
    }

}

student.hbm.xml

<hibernate-mapping>
<class name="com.javathub.Student" table="stu_details">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="name"></property>
<property name="branch"></property>
<property name="fee"></property>
</class>


</hibernate-mapping>

Student.java

public class Student {
    private int id;
    private String name;
    private String branch;
    private double fee;
    public Student(){

    }
    public Student(int id, String name, String branch, double fee) {
        super();
        this.id = id;
        this.name = name;
        this.branch = branch;
        this.fee = fee;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBranch() {
        return branch;
    }
    public void setBranch(String branch) {
        this.branch = branch;
    }
    public double getFee() {
        return fee;
    }
    public void setFee(double fee) {
        this.fee = fee;
    }

}

please help me, thanks in advance

将src文件夹中的student.hbm.xml和hibernate.cfg.xml不在软件包中,然后再次运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM