【问题标题】:auto database creation in hibernate with mysql使用mysql在hibernate中自动创建数据库
【发布时间】:2016-03-07 08:03:02
【问题描述】:

这是我的代码,我做错了。运行 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;
    }

}

请帮帮我,提前谢谢

【问题讨论】:

  • 用 Student.java 的包更新你的休眠映射类,就像这样

标签: java mysql hibernate spring-mvc


【解决方案1】:

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

【讨论】:

  • 谢谢,得到了解决方案,但在动态 Web 项目中它没有创建。如果我们从 jsp 提供输入,那么它正在创建数据并将其存储到数据库中。如何在不从 jsp 提供输入的情况下创建自动表?我应该在 web.xml 中写任何东西吗?我写的代码和上面一样
猜你喜欢
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 2011-05-29
  • 2011-04-05
  • 1970-01-01
  • 2014-08-08
相关资源
最近更新 更多