堆栈跟踪:
ERROR: HHH000196: Error parsing XML (2) : cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.nagarro.flightSystem.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
at com.nagarro.flightSystem.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
at com.nagarro.flightSystem.dao.FlightDAO.convertDAO(FlightDAO.java:70)
at com.nagarro.flightSystem.service.impl.FlightDataImpl.loadFlightData(FlightDataImpl.java:78)
at com.nagarro.flightSystem.flightServiceController.MainService.main(MainService.java:58)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:375)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:304)
at org.hibernate.cfg.Configuration.add(Configuration.java:516)
at org.hibernate.cfg.Configuration.add(Configuration.java:512)
at org.hibernate.cfg.Configuration.add(Configuration.java:686)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
at com.nagarro.flightSystem.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 4 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 61; cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1901)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:741)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3065)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:325)
... 16 more
映射文件是
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping package="com.nagarro.flightSystem.model">
<class name="Flight" table="flight">
<property name="flightNo" type="string">
<column name="flight_no" />
</property>
<property name="depatureLocation" type="string">
<column name="depature_location" length="3" />
</property>
<property name="arrivalLocation" type="string">
<column name="arrival_location" length="3" />
</property>
<property name="validTill" type="string">
<column name="valid_till" />
</property>
<property name="flightTime" type="string">
<column name="flight_time" />
</property>
<property name="flightDuration" type="string">
<column name="flight_duration" />
</property>
<property name="fare" type="string">
<column name="fare" />
</property>
<property name="seatAvailability" type="string">
<column name="seat_availability" length="1" />
</property>
<property name="flightClass" type="string">
<column name="flight_class" length="2" />
</property>
</class>
</hibernate-mapping>
package com.nagarro.flightSystem.model;
public class Flight implements Comparable<Flight> {
/**
* Holds a string value in flight no.
*/
private String flightNo;
/**
* Holds a string value in depatureLocation.
*/
private String depatureLocation;
/**
* Holds a string value in arrivalLocation.
*/
private String arrivalLocation;
/**
* Holds a string value in vaildTill.
*/
private String validTill;
/**
* Holds a string value in flightTime.
*/
private String flightTime;
/**
* Holds a string value in flightDuration.
*/
private String flightDuration;
/**
* Holds a string value in fare.
*/
private String fare;
/**
* Holds a string value in seatAvailability.
*/
private String seatAvailability;
/**
* Holds a string value in flightClass.
*/
private String flightClass;
/**
* Instantiates a new flight.
*/
public Flight() {
}
/**
* Instantiates a new flight.
*
* @param flightNo the flight no
*/
public Flight(String flightNo) {
this.flightNo = flightNo;
}
/**
* Gets the flight no.
*
* @return the flight no
*/
public String getFlightNo() {
return flightNo;
}
/**
* Sets the flight no.
*
* @param flightNo
* the new flight no
*/
public void setFlightNo(String flightNo) {
this.flightNo = flightNo;
}
/**
* Gets the depature location.
*
* @return the depature location
*/
public String getDepatureLocation() {
return depatureLocation;
}
/**
* Sets the depature location.
*
* @param depatureLocation
* the new depature location
*/
public void setDepatureLocation(String depatureLocation) {
this.depatureLocation = depatureLocation;
}
/**
* Gets the arrible location.
*
* @return the arrible location
*/
public String getArribleLocation() {
return arrivalLocation;
}
/**
* Sets the arrible location.
*
* @param arrivalLocation
* the new arrible location
*/
public void setArribleLocation(String arrivalLocation) {
this.arrivalLocation = arrivalLocation;
}
/**
* Gets the valid till.
*
* @return the valid till
*/
public String getValidTill() {
return validTill;
}
/**
* Sets the valid till.
*
* @param validTill
* the new valid till
*/
public void setValidTill(String validTill) {
this.validTill = validTill;
}
/**
* Gets the flight time.
*
* @return the flight time
*/
public String getFlightTime() {
return flightTime;
}
/**
* Sets the flight time.
*
* @param flightTime
* the new flight time
*/
public void setFlightTime(String flightTime) {
this.flightTime = flightTime;
}
/**
* Gets the flight duration.
*
* @return the flight duration
*/
public String getFlightDuration() {
return flightDuration;
}
/**
* Sets the flight duration.
*
* @param flightDuration
* the new flight duration
*/
public void setFlightDuration(String flightDuration) {
this.flightDuration = flightDuration;
}
/**
* Gets the fare.
*
* @return the fare
*/
public String getFare() {
return fare;
}
/**
* Sets the fare.
*
* @param fare
* the new fare
*/
public void setFare(String fare) {
this.fare = fare;
}
/**
* Gets the seat availability.
*
* @return the seat availability
*/
public String getSeatAvailability() {
return seatAvailability;
}
/**
* Sets the seat availability.
*
* @param seatAvailability
* the new seat availability
*/
public void setSeatAvailability(String seatAvailability) {
this.seatAvailability = seatAvailability;
}
/**
* Gets the flight class.
*
* @return the flight class
*/
public String getFlightClass() {
return flightClass;
}
/**
* Sets the flight class.
*
* @param flightClass
* the new flight class
*/
public void setFlightClass(String flightClass) {
this.flightClass = flightClass;
}
@Override
public int compareTo(Flight flightObject) {
return (this.fare).compareTo(flightObject.fare);
}
}
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(flightObject);
tx.commit();
}catch (HibernateException e) {
if(tx!=null){
tx.rollback();
}
e.printStackTrace();
}
finally{
session.close();
}