2016-03-07 106 views
1

这是我的代码,我犯了错误。当运行Test.java时出现错误,如“请求的资源不可用”。我一直在同一个包使用mysql在休眠状态下创建自动数据库

的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

用Student.java的包更新你的hibernate映射类,像这样 Filip

回答

0

保持student.hbm.xml和hibernate.cfg.xml在src文件夹不包,然后再次运行。

+0

谢谢,得到了解决方案,但是在动态web项目中并没有创建它。如果我们从jsp提供输入,那么它将创建数据并将其存储到数据库中。我如何创建自动表,而不从jsp提供输入?我应该在web.xml中写任何东西?我写的代码与上面相同 – NRV