I was able to use the code supplied by the author to insert a new row into MySQL. Unfortunately his table name and class name, Employee, were the same, so he didn’t need to explicitly name the schema/database table.
I went ahead and added the statement, <mapping table="Employee"/>, in the author’s hibernate.cfg.xml file:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/employeeschema_01</property>
<property name="hibernate.connection.username">
root</property>
<property name="hibernate.connection.password">
</property>
<property name="hibernate.connection.pool_size">
10</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">
thread</property>
<mapping class="Ch_09_03_Employee" />
<mapping table="Employee"/>
</session-factory>
</hibernate-configuration>
I also went ahead and added the following annotation to my, Ch_09_03_Employee, class:
@Table(name = "Employee")
This is the full Ch_09_03_Employee class:
Code:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity //(name = "Ch_09_03_Employee")
@Table(name = "Employee")
public class Ch_09_03_Employee {
private int EmployeeID;
private String Name;
private String Gender;
private int DNR;
@Id
public int getEmployeeID() {
return EmployeeID;
}
public void setEmployeeID(int id) {
this.EmployeeID = id;
}
public String getName() {
return Name;
}
public void setName( String name ) {
this.Name = name;
}
public String getGender() {
return Gender;
}
public void setGender( String gender ) {
this.Gender = gender;
}
public int getDNR() {
return DNR;
}
public void setDNR(int dnr) {
this.DNR = dnr;
}
}
The following is the class, Ch_09_03_myDBApp, which contains the main method and the Hibernate package:
Code:
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;
import java.util.List;
public class Ch_09_03_myDBApp {
public static void main(String[] args) {
// Create new employee and store in MySQL
Ch_09_03_Employee Myemp = new Ch_09_03_Employee();
Myemp.setName("Hibernate dude1");
Myemp.setGender("Male");
Myemp.setEmployeeID(7);
Myemp.setDNR(3);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(Myemp);
session.getTransaction().commit();
// Retrieve employee data from MySQL
Query query = session.createQuery("from Employee where EmployeeID = 6");
List<?> list = query.list();
Ch_09_03_Employee emp = (Ch_09_03_Employee)list.get(0);
System.out.println(emp.getName());
System.out.println(emp.getGender());
System.out.println(emp.getDNR());
session.close();
sessionFactory.close();
}
}
I get the following output when I execute the program:
Code:
Aug 09, 2015 8:21:30 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Aug 09, 2015 8:21:31 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Aug 09, 2015 8:21:31 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 09, 2015 8:21:31 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 09, 2015 8:21:31 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 09, 2015 8:21:31 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at Ch_09_03_myDBApp.main(Ch_09_03_myDBApp.java:18)
Caused by: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 56; Attribute "table" must be declared for element type "mapping".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157)
... 3 more
Line 18 states:
Caused by: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 56; Attribute "table" must be declared for element type "mapping".
After resolving this issue, I would later on like to know how to use a differently named .xml, file. When I did:
SessionFactory sessionFactory = new Configuration().configure(
hibernateONE.cfg.xml).buildSessionFactory();
I got parse errors in return.