使用mysql在hibernate中创建自动数据库

时间:2016-03-07 08:03:02

标签: java mysql hibernate spring-mvc

这是我的代码,我做错了。运行Test.java时收到“请求的资源不可用”之类的错误。我将所有这些文件保存在同一个包中

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

}

请帮助我,提前谢谢

1 个答案:

答案 0 :(得分:0)

将src文件夹中的student.hbm.xml和hibernate.cfg.xml保留在包中并再次运行。