Hi,
As i am trying to execute event-person example of hibernate i am able to work on individual but when i start working with mapping associations i am geting some error as shown below.
#############################################################################################
D:\hibernateexample>ant run -Daction=store
Buildfile: build.xml
clean:
[delete] Deleting directory D:\hibernateexample\bin
[mkdir] Created dir: D:\hibernateexample\bin
copy-resources:
[copy] Copying 9 files to D:\hibernateexample\bin
compile:
[javac] Compiling 4 source files to D:\hibernateexample\bin
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
run:
[java] 2005-12-02 14:37:51,302 INFO [EventManager] - <Hello this is test for logfile entry>
[java] 2005-12-02 14:37:51,349 INFO [org.hibernate.cfg.Environment] - <Hibernate 3.0>
[java] 2005-12-02 14:37:51,349 INFO [org.hibernate.cfg.Environment] - <hibernate.properties not found>
[java] 2005-12-02 14:37:51,349 INFO [org.hibernate.cfg.Environment] - <using CGLIB reflection optimizer>
[java] 2005-12-02 14:37:51,349 INFO [org.hibernate.cfg.Environment] - <using JDK 1.4 java.sql.Timestamp handling>
[java] 2005-12-02 14:37:51,474 INFO [org.hibernate.cfg.Configuration] - <configuring from resource: /hibernate.cfg.xml>
[java] 2005-12-02 14:37:51,474 INFO [org.hibernate.cfg.Configuration] - <Configuration resource: /hibernate.cfg.xml>
[java] 2005-12-02 14:37:52,005 INFO [org.hibernate.cfg.Configuration] - <Mapping resource: Event.hbm.xml>
[java] 2005-12-02 14:37:52,192 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: Event -> EVENTS>
[java] 2005-12-02 14:37:52,270 INFO [org.hibernate.cfg.Configuration] - <Mapping resource: Person.hbm.xml>
[java] 2005-12-02 14:37:52,333 ERROR [org.hibernate.util.XMLHelper] - <Error parsing XML: XML InputStream(17) The conten
t of element type "hibernate-mapping" must match "(meta*,typedef*,import*,(class|subclass|joined-subclass)*,(query|sql-query)
*,filter-def*)".>
[java] Initial SessionFactory creation failed.org.hibernate.MappingException: Error reading resource: Person.hbm.xml
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at HibernateUtil.<clinit>(Unknown Source)
[java] at EventManager.createAndStoreEvent(Unknown Source)
[java] at EventManager.main(Unknown Source)
[java] Caused by: org.hibernate.MappingException: Error reading resource: Person.hbm.xml
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.java:448)
[java] at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1312)
[java] at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1284)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1266)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1233)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:1147)
[java] ... 3 more
[java] Caused by: org.hibernate.MappingException: invalid mapping
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:395)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.java:445)
[java] ... 9 more
[java] Caused by: org.xml.sax.SAXParseException: The content of element type "hibernate-mapping" must match "(meta*,type
def*,import*,(class|subclass|joined-subclass)*,(query|sql-query)*,filter-def*)".
[java] at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
[java] at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
[java] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
[java] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
[java] at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
[java] at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
[java] at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
[java] at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
[java] at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
[java] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
[java] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
[java] at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
[java] at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
[java] at org.dom4j.io.SAXReader.read(SAXReader.java:334)
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:394)
[java] ... 10 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 4 seconds
D:\hibernateexample>
##############################################################################################
I am posting all the files i am using for this example.
1> Person.java
import java.util.*;
public class Person {
private Long id;
private int age;
private String firstname;
private String lastname;
Person() {}
public Long getId(){
return id;
}
private void setId(Long id){
this.id=id;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public String getFirstname(){
return firstname;
}
public void setFirstname(String firstname){
this.firstname=firstname;
}
public String getLastname(){
return lastname;
}
public void setLastname(String lastname){
this.lastname=lastname;
}
private Set events = new HashSet();
public Set getEvents() {
return events;
}
public void setEvents(Set events) {
this.events = events;
}
}
################################################
2>Event.java
import java.util.*;
public class Event {
private Long id;
private String title;
private Date date;
Event() {}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
###############################################
3>Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="increment"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>
</class>
<set name="events" table="PERSON_EVENT">
<key column="PERSON_ID"/>
<many-to-many column="EVENT_ID" class="Event"/>
</set>
</hibernate-mapping>
###############################################
4>Event.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="increment"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
################################################
5>hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@server2:1521:oracle</property>
<property name="connection.username">hrdl</property>
<property name="connection.password">hrdl</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto"></property>
<mapping resource="Event.hbm.xml"/>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
################################################
6>EventManager.java
import org.hibernate.Transaction;
import org.hibernate.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.*;
public class EventManager {
final Log logger = LogFactory.getLog(getClass());
public static void main(String[] args) {
EventManager mgr = new EventManager();
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
mgr.createAndStorePerson(23,"Srinivas", "Krishna");
}
else if (args[0].equals("list")) {
List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() +" Time: " + theEvent.getDate()+" ID :"+theEvent.getId());
}
List person = mgr.listPerson();
for (int i = 0; i < person.size(); i++) {
Person thePerson = (Person) person.get(i);
System.out.println("Person: " + thePerson.getFirstname() +" "+ thePerson.getLastname() +" ID: " + thePerson.getId());
}
mgr.addPersonToEvent(1L,0L);
}
HibernateUtil.sessionFactory.close();
}
private void createAndStoreEvent(String title, Date theDate) {
logger.info("Hello this is test for logfile entry");
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
tx.commit();
HibernateUtil.closeSession();
}
private List listEvents() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List result = session.createQuery("from Event").list();
tx.commit();
//session.close();
return result;
}
private void createAndStorePerson(int age, String fname, String lname) {
logger.info("Hello this is test for logfile entry");
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Person thePerson = new Person();
thePerson.setAge(age);
thePerson.setFirstname(fname);
thePerson.setLastname(lname);
session.save(thePerson);
tx.commit();
HibernateUtil.closeSession();
}
private List listPerson() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List result = session.createQuery("from Person").list();
tx.commit();
session.close();
return result;
}
private void addPersonToEvent(Long personId, Long eventId) {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
Event anEvent = (Event) session.load(Event.class, eventId);
aPerson.getEvents().add(anEvent);
tx.commit();
HibernateUtil.closeSession();
}
}
################################################
7>hibernateutil.java is used with all these files.
################################################
MY Questions
> in person.hbm.xml i am using set tag and giving table name and column. do i need to create that table manually?
>If the problem is not in person.hbm.xml. Where exactly i am going wrong.?
I would be great help if anyone post the solution for this problem.
With thanks and Regards
_________________ Seenu
|