gpt4 book ai didi

java - 如何解决这个hibernate运行时异常?

转载 作者:行者123 更新时间:2023-12-01 05:01:34 25 4
gpt4 key购买 nike

我正在尝试在 eclipse 中运行以下代码

package com.trial;


import java.sql.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class AddStudent {

private static SessionFactory sessionFactory;

public static void main(String args[]) throws Exception {

DataInputStream d = new DataInputStream(System.in);
System.out.println("ENTER YOUR NAME");
String name = d.readLine();
System.out.println("ENTER YOUR DEGREE");
String degree = d.readLine();
System.out.println("ENTER YOUR PHONE");
int phone = Integer.parseInt(d.readLine());
System.out.println("Name: " + name);
System.out.println("Degree: " + degree);
System.out.println("Phone: " + phone);
if ((name.equals("") || degree.equals(""))) {
System.out.println("Information Required");
}
else {

try {
sessionFactory = new Configuration().configure("com//xml//hibernate.cfg.xml").buildSessionFactory();
}
catch (Exception e) {
System.out.println(e.getMessage());
}

Session s = sessionFactory.openSession();
Student stu = new Student();
stu.setName(name);
stu.setDegree(degree);
stu.setPhone(phone);
s.save(stu);
System.out.println("Added to Database");
if (s != null)
s.close();
}
}
}

但是在创建 session 工厂对象期间出现运行时异常,无法读取 XML。

我正在使用以下 xml 文件

  1. hibernate.cfg.xml

     <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>

    <session-factory name="studentFactory">

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ganeshdb</property>
    <property name="connection.username">****</property>
    <property name="connection.password">****</property>
    <property name="connection.pool_size">10</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping resource="com//xml//student.hbm.xml" />

    </session-factory>

    </hibernate-configuration>
  2. 映射文件

      <?xml version="1.0"? encoding='UTF-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/hibernate-configuration-3.0.dtd">

    <hibernate-mapping>

    <class name="com.trial.Student" table="studentdemo">
    <id name="id" type="int" column="ID">
    <generator class="increment" />
    </id>
    <property name="name" column="name" />
    <property name="degree" column="degree" />
    <property name="phone" column="phone" />
    </class>

    </hibernate-mapping>

请帮忙。

最佳答案

<mapping resource="student.hbm.xml" />

并确保它与 hibernate.cfg.xml 位于同一目录

关于java - 如何解决这个hibernate运行时异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com