Hi, I am getting problem exporting data into xml format. Following is the complete information of what I am doing. Please! Guide me porperly ....
Classes involved:
Code:
public class Main {
public static void main(String[] args) throws Exception {
Data usersData = new Data();
usersData.executeExport("C:\\Users.xml");
}
}
public class Data {
Session session;
Document document;
Transaction transaction;
public Data() {
session = SessionFactory.currentSession();
transaction = session.beginTransaction();
}
public void executeExport(String fileName) {
try {
document = getData();
XMLWriter writer = new XMLWriter(new FileWriter(fileName), OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private Document getData() {
Document doc = DocumentHelper.createDocument();
Element users = doc.addElement("users");
Session dom4jsession = session.getSession(EntityMode.DOM4J);
List usersList = dom4jsession.createQuery("FROM Users").list();
Iterator i = usersList.iterator();
for (i=usersList.iterator(); i.hasNext();) {
Element user = (Element)i.next();
if (user != null) {
users.add(user);
}
}
return doc;
}
}
public class Users
extends AbstractUsers
implements Serializable {
private static final long serialVersionUID = 8975703870006542838L;
public Users() {
super.setAddressesSet(new HashSet());
}
public Users(java.lang.String userName) {
super(userName);
}
public void initialize(){
Hibernate.initialize(this);
this.getAddressesSet().size();
}
}
public class AddressesKey
implements Serializable
{
private static final long serialVersionUID = -915486979622282724L;
private volatile int hashValue = 0;
private java.lang.String addressId;
private Users users;
public AddressesKey()
{
}
public java.lang.String getAddressId()
{
return addressId;
}
public void setAddressId(java.lang.String addressId)
{
hashValue = 0;
this.addressId = addressId;
}
public Users getUsers()
{
return users;
}
public void setUsers(Users users)
{
hashValue = 0;
this.users = users;
}
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof AddressesKey))
return false;
AddressesKey that = (AddressesKey) rhs;
if (this.getAddressId() == null || that.getAddressId() == null)
{
return false;
}
if (! this.getAddressId().equals(that.getAddressId()))
{
return false;
}
if (this.getUsers() == null || that.getUsers() == null)
{
return false;
}
if (! this.getUsers().equals(that.getUsers()))
{
return false;
}
return true;
}
public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int addressIdValue = this.getAddressId() == null ? 0 : this.getAddressId().hashCode();
result = result * 37 + addressIdValue;
int usersValue = this.getUsers() == null ? 0 : this.getUsers().hashCode();
result = result * 37 + usersValue;
this.hashValue = result;
}
return this.hashValue;
}
}
public class Addresses
extends AbstractAddresses
implements Serializable
{
private static final long serialVersionUID = -1610477145149174065L;
public Addresses()
{
}
public Addresses(AddressesKey id)
{
super(id);
}
}
public abstract class AbstractUsers
implements Serializable
{
private int hashValue = 0;
private java.lang.String userName;
private java.util.Set addressesSet = new HashSet();
private java.lang.String firstName;
private java.lang.String lastName;
public AbstractUsers()
{
}
public AbstractUsers(java.lang.String userName)
{
this.setUserName(userName);
}
public java.lang.String getUserName()
{
return userName;
}
public void setUserName(java.lang.String userName)
{
this.hashValue = 0;
this.userName = userName;
}
public java.lang.String getFirstName()
{
return this.firstName;
}
public void setFirstName(java.lang.String firstName)
{
this.firstName = firstName;
}
public java.lang.String getLastName()
{
return this.lastName;
}
public void setLastName(java.lang.String lastName)
{
this.lastName = lastName;
}
public java.util.Set getAddressesSet()
{
return this.addressesSet;
}
public void setAddressesSet(java.util.Set addressesSet)
{
this.addressesSet = addressesSet;
}
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof Users))
return false;
Users that = (Users) rhs;
if (this.getUserName() == null || that.getUserName() == null)
return false;
return (this.getUserName().equals(that.getUserName()));
}
public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int userNameValue = this.getUserName() == null ? 0 : this.getUserName().hashCode();
result = result * 37 + userNameValue;
this.hashValue = result;
}
return this.hashValue;
}
}
public abstract class AbstractAddresses
implements Serializable
{
private int hashValue = 0;
private AddressesKey id;
private java.lang.String street;
private java.lang.String city;
private java.lang.String region;
private java.lang.String country;
public AbstractAddresses()
{
}
public AbstractAddresses(AddressesKey id)
{
this.setId(id);
}
public AddressesKey getId()
{
return this.id;
}
public void setId(AddressesKey id)
{
this.hashValue = 0;
this.id = id;
}
public java.lang.String getStreet()
{
return this.street;
}
public void setStreet(java.lang.String street)
{
this.street = street;
}
public java.lang.String getCity()
{
return this.city;
}
public void setCity(java.lang.String city)
{
this.city = city;
}
public java.lang.String getRegion()
{
return this.region;
}
public void setRegion(java.lang.String region)
{
this.region = region;
}
public java.lang.String getCountry()
{
return this.country;
}
public void setCountry(java.lang.String country)
{
this.country = country;
}
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof Addresses))
return false;
Addresses that = (Addresses) rhs;
if (this.getId() == null || that.getId() == null)
return false;
return (this.getId().equals(that.getId()));
}
public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
if (this.getId() == null)
{
result = super.hashCode();
}
else
{
result = this.getId().hashCode();
}
this.hashValue = result;
}
return this.hashValue;
}
}
Hibernate version: 3.0
Mapping documents:Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.test.hibernate.classes">
<class name="Users" table="users" node="user">
<id name="userName" column="USER_NAME" type="string" node="@user_name">
<generator class="assigned"/>
</id>
<property name="firstName" column="FIRST_NAME" type="string" node="first_name"/>
<property name="lastName" column="LAST_NAME" type="string" node="last_name"/>
<set name="addressesSet" embed-xml="true" node="addresses">
<key column="USER_NAME"/>
<one-to-many class="Addresses"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.test.hibernate.classes">
<class name="Addresses" table="addresses">
<composite-id name="id" class="AddressesKey">
<key-property name="addressId" column="ADDRESS_ID" type="string"/>
<key-many-to-one name="users" column="USER_NAME" class="Users"/>
</composite-id>
<property name="street" column="STREET" type="string"/>
<property name="city" column="CITY" type="string"/>
<property name="region" column="REGION" type="string"/>
<property name="country" column="COUNTRY" type="string"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
package com.test.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class SessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private SessionFactory() {
}
}
Full stack trace of any exception that occurs:Code:
Exception in thread "main" java.lang.ClassCastException: org.dom4j.tree.DefaultElement
at org.hibernate.type.EntityType.toLoggableString(EntityType.java:134)
at org.hibernate.type.ComponentType.toLoggableString(ComponentType.java:283)
at org.hibernate.pretty.MessageHelper.infoString(MessageHelper.java:68)
at org.hibernate.engine.EntityKey.toString(EntityKey.java:77)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at org.hibernate.util.StringHelper.toString(StringHelper.java:168)
at org.hibernate.loader.Loader.getRow(Loader.java:760)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:293)
at org.hibernate.loader.Loader.doQuery(Loader.java:387)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1360)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1353)
at org.hibernate.type.CollectionType.getCollection(CollectionType.java:470)
at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:327)
at org.hibernate.type.CollectionType.resolve(CollectionType.java:321)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:105)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:492)
at org.hibernate.loader.Loader.doQuery(Loader.java:411)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
at org.hibernate.loader.Loader.doList(Loader.java:1515)
at org.hibernate.loader.Loader.list(Loader.java:1498)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:266)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:788)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at com.test.importexport.Data.getData(Data.java:55)
at com.test.importexport.Data.executeExport(Data.java:34)
at com.test.Main.main(Main.java:20)
Name and version of the database you are using:Database : MySQL
Version : 5.0.18
The generated SQL (show_sql=true):Code:
Hibernate: select users0_.USER_NAME as USER1_, users0_.FIRST_NAME as FIRST2_0_, users0_.LAST_NAME as LAST3_0_ from users users0_
Hibernate: select addressess0_.USER_NAME as USER2___, addressess0_.ADDRESS_ID as ADDRESS1___, addressess0_.ADDRESS_ID as ADDRESS1_0_, addressess0_.USER_NAME as USER2_0_, addressess0_.STREET as STREET1_0_, addressess0_.CITY as CITY1_0_, addressess0_.REGION as REGION1_0_, addressess0_.COUNTRY as COUNTRY1_0_ from addresses addressess0_ where addressess0_.USER_NAME=?
Debug level Hibernate log excerpt: