使用MyEclipse在hibernate代码中有多个异常

时间:2018-01-09 17:59:07

标签: java xml hibernate exception

我正在做一个hibernate映射多对一项目。其中学生类映射Saddress类。我正在使用MyEclipse进行项目,但是hibernate项目几乎没有例外。在这里,我尝试使用StackOverflow中的建议来纠正代码。但没有用;还因为我使用的代码已经定义了,hibernate.cfg.xml文件或Students.hbm.xml文件或Saddress.hbm.xml文件,这些文件都是自动生成的,所以没有错误的问题。我无法识别错误。

hibernate.cfg.xml -

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

    <session-factory>
    <mapping resource="studentonetomany/Students.hbm.xml" />
    <mapping resource="studentonetomany/Saddress.hbm.xml" />

    </session-factory>

    </hibernate-configuration>

Students.java -

 package studentonetomany;

 import java.util.HashSet;
 import java.util.Set;

 /**
  * Students entity. @author MyEclipse Persistence Tools
  */

  public class Students implements java.io.Serializable {

    // Fields

private Integer sid;
private String sname;
private Integer age;
private Set saddresses = new HashSet(0);

// Constructors

/** default constructor */
public Students() {
}

/** minimal constructor */
public Students(Integer sid) {
    this.sid = sid;
}

/** full constructor */
public Students(Integer sid, String sname, Integer age) {
    this.sid = sid;
    this.sname = sname;
    this.age = age;

}

// Property accessors

public Integer getSid() {
    return this.sid;
}

public void setSid(Integer sid) {
    this.sid = sid;
}

public String getSname() {
    return this.sname;
}

public void setSname(String sname) {
    this.sname = sname;
}

public Integer getAge() {
    return this.age;
}

   public void setAge(Integer age) {
    this.age = age;
    }

    public Set getSaddresses() {
        return this.saddresses;
    }

    public void setSaddresses(Set saddresses) {
        this.saddresses = saddresses;
  }

} 

Saddress.java

  package studentonetomany;

 /**
  * Saddress entity. @author MyEclipse Persistence Tools
  */

 public class Saddress implements java.io.Serializable {

// Fields

private Integer aid;
private Students students;
private String street;
private String city;
private String state;

// Constructors

/** default constructor */
public Saddress() {
}

/** minimal constructor */
public Saddress(Integer aid) {
    this.aid = aid;
}

/** full constructor */
public Saddress(Integer aid, Students students, String street, String city, 
 String state) {
    this.aid = aid;
    this.students = students;
    this.street = street;
    this.city = city;
    this.state = state;
}

// Property accessors

public Integer getAid() {
    return this.aid;
}

public void setAid(Integer aid) {
    this.aid = aid;
}

public Students getStudents() {
    return this.students;
}

public void setStudents(Students students) {
    this.students = students;
}

public String getStreet() {
    return this.street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return this.city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return this.state;
}

public void setState(String state) {
    this.state = state;
}

 }

Saddress.hbm.xml文件

  <?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
  3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
Mapping file autogenerated by MyEclipse Persistence Tools
 -->
 <hibernate-mapping>
<class name="studentonetomany.Saddress" table="saddress" 
 catalog="testingcampus">
    <id name="aid" type="java.lang.Integer">
        <column name="aid" />
        <generator class="assigned" />
    </id>
    <many-to-one name="students" class="studentonetomany.Students" 
    fetch="select">
        <column name="sid" />
    </many-to-one>
    <property name="street" type="java.lang.String">
        <column name="street" length="20" />
    </property>
    <property name="city" type="java.lang.String">
        <column name="city" length="20" />
    </property>
    <property name="state" type="java.lang.String">
        <column name="state" length="20" />
    </property>
</class>
 </hibernate-mapping>

Students.hbm.xml文件 -

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
  3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  <!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
    -->
<hibernate-mapping>
<class name="studentonetomany.Students" table="students" catalog="testingcampus">
    <id name="sid" type="java.lang.Integer">
        <column name="sid" />
        <generator class="assigned" />
    </id>
    <property name="sname" type="java.lang.String">
        <column name="sname" length="20" />
    </property>
    <property name="age" type="java.lang.Integer">
        <column name="age" />
    </property>
    <set name="saddresses" inverse="true">
        <key>
            <column name="sid" />
        </key>
        <one-to-many class="studentonetomany.Saddress" />
    </set>
   </class>
</hibernate-mapping>

主程序 - SMapping.java

 package studentonetomany;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class SMapping {

    public static void main(String[] args) {

    Session session = HibernateSessionFactory.getSession();

    Transaction t = session.beginTransaction();

    Students st = new Students(100,"pravin",29);

    Saddress addr1 = new Saddress(1,st,"BTM","Bangalore","Karnataka");

    Saddress addr2 = new Saddress(2,st,"Rajaji 
    Nagar","Bangalore","Karnataka");



    session.save(st);
    session.save(addr1);
    session.save(addr2);

    t.commit();


 }

 }

错误 -

Jan 09, 2018 10:35:54 PM 
org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator 
initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming 
application will be supplying connections
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: Connection cannot be null when 
   'hibernate.dialect' not set
     at 


org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
at studentonetomany.HibernateSessionFactory.<clinit>
(HibernateSessionFactory.java:34)
at studentonetomany.SMapping.main(SMapping.java:10)
Jan 09, 2018 10:35:54 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jan 09, 2018 10:35:54 PM org.hibernate.cfg.Configuration 
getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jan 09, 2018 10:35:54 PM org.hibernate.cfg.Configuration addResource
 INFO: HHH000221: Reading mappings from resource: 
 studentonetomany/Students.hbm.xml
  Jan 09, 2018 10:35:54 PM org.hibernate.cfg.Configuration addResource
  INFO: HHH000221: Reading mappings from resource: 
  studentonetomany/Saddress.hbm.xml
  Jan 09, 2018 10:35:55 PM org.hibernate.cfg.Configuration doConfigure
  INFO: HHH000041: Configured SessionFactory: null
   Jan 09, 2018 10:35:55 PM org.hibernate.cfg.Configuration$MappingsImpl 
  addImport
  INFO: HHH000071: Duplicate import: studentonetomany.Students -> 
     studentonetomany.Students
  Jan 09, 2018 10:35:55 PM org.hibernate.cfg.Configuration$MappingsImpl 
  addImport
   INFO: HHH000071: Duplicate import: studentonetomany.Students -> Students
   %%%% Error Creating SessionFactory %%%%
    org.hibernate.InvalidMappingException: Could not parse mapping document 
    from 
         resource studentonetomany/Students.hbm.xml
    at ....

     ... 7 more
     Exception in thread "main" java.lang.NullPointerException
     at studentonetomany.SMapping.main(SMapping.java:12)

1 个答案:

答案 0 :(得分:0)

出于某种原因,我不知道为什么你的配置没有这样的东西:

<session-factory>
    <property name = "dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name = "connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name = "connection.url">jdbc:mysql://localhost:3307/mydb</property>

    <property name = "connection.username">root</property>
    <property name = "connection.password">12345</property>
</session-factory>