-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: extracting data in XML format
PostPosted: Mon Feb 12, 2007 1:18 pm 
Newbie

Joined: Mon Feb 12, 2007 1:01 pm
Posts: 2
Hello:

I am trying to extract data from my database in XML format. If I
specify a very simple query like "from OrderTab" I get the expected XML.
With the data from the child tables embedded. Very nice.

If I enter the following query " select ot.orderKey, ot.customerName from OrderTab" , I get the following exception:


C:\dev\eclipse\OrderShip\bin>runordership > runOrderShip.out.txt
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
at ordership.SalesDB2XML.resultsAsXMLelement(Unknown Source)
at ordership.SalesDB2XML.main(Unknown Source)

C:\dev\eclipse\OrderShip\bin>

I do not understand why this does not work.


My HBM's and Java classes are below.
I am using Hibernate 3.2 and connecting to DB2.


Thank you in advance for looking at this issue.


kd

** ** ** ** ** **
** ** ** SalesDB2XML.java

package ordership;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;

import org.hibernate.EntityMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class SalesDB2XML {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

// Set up DOM document
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("SalesDB_export");

// Set up hibernate
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Session xmlSession = session.getSession(EntityMode.DOM4J);
Transaction trans = xmlSession.getTransaction();
trans.begin();
resultsAsXML( xmlSession, root, " from PartTab " );

/**
"select t.templateKey, t.templateDesc, tf_TC.commentText " +
"from Template t inner join t.templateFieldTableColumns as tf_TC "
join fetch t.templateFieldTableColumns
**/


System.out.println("Dump XML");
BufferedWriter writer = new BufferedWriter( new FileWriter("salesDB.PartTab.export.xml"));
doc.write( writer );
writer.flush();
writer.close();
writer = null;
doc.clearContent();

root = doc.addElement("SalesDB_export");
resultsAsXMLelement( xmlSession, root, " from OrderTab ot " );
System.out.println("Dump XML");
writer = new BufferedWriter( new FileWriter("salesDB.OrderTab.export.xml"));
doc.write( writer );
writer.flush();
writer.close();


trans.commit();
xmlSession.close();
session.close();


System.out.println("Done");
}


public static void resultsAsXML(Session xmlSess, Element root, String hql) {
System.out.println( xmlSess.getEntityMode().toString() );
Query q = xmlSess.createQuery( hql );
List l = q.list();
System.out.println("Examine the results");
Iterator i = l.iterator();
while ( i.hasNext() ) {

Element e = (Element) i.next();
root.add( e );

/**
Object[] o = (Object[]) i.next();
System.out.println( "\n\n ** Array Length: " + o.length );

for (int a1 =0; a1 < o.length ; a1++ ) {
if (o[a1] == null)
System.out.print( " \n** array entry is null \n " );
else {
Class c = o[a1].getClass();
System.out.println( " ** Class Name: " + c.getName() );
System.out.println( " toString(): " + o[a1].toString() );
}

} //for **/

} //while


} //resultsAsXML

public static void resultsAsXMLelement(Session xmlSess, Element root, String hql) {
Element e = (Element) xmlSess.createQuery( hql).uniqueResult();
root.add( e );



}


}



** ** ** ** ** **
** ** ** PartTab.java

package ordership;
// Generated Feb 8, 2007 3:26:27 PM by Hibernate Tools 3.2.0.b9


import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

/**
* PartTab generated by hbm2java
*/
public class PartTab implements java.io.Serializable {


private int partKey;
private ordership.OrderTab orderTab;
private String color;
private Integer quantity;
private BigDecimal price;
private Float tax;
private Set shipTabs = new HashSet(0);

public PartTab() {
}


public PartTab(int partKey, OrderTab orderTab) {
this.partKey = partKey;
this.orderTab = orderTab;
}
public PartTab(int partKey, OrderTab orderTab, String color, Integer quantity, BigDecimal price, Float tax, Set shipTabs) {
this.partKey = partKey;
this.orderTab = orderTab;
this.color = color;
this.quantity = quantity;
this.price = price;
this.tax = tax;
this.shipTabs = shipTabs;
}

public int getPartKey() {
return this.partKey;
}

public void setPartKey(int partKey) {
this.partKey = partKey;
}
public OrderTab getOrderTab() {
return this.orderTab;
}

public void setOrderTab(OrderTab orderTab) {
this.orderTab = orderTab;
}
public String getColor() {
return this.color;
}

public void setColor(String color) {
this.color = color;
}
public Integer getQuantity() {
return this.quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public BigDecimal getPrice() {
return this.price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}
public Float getTax() {
return this.tax;
}

public void setTax(Float tax) {
this.tax = tax;
}
public Set getShipTabs() {
return this.shipTabs;
}

public void setShipTabs(Set shipTabs) {
this.shipTabs = shipTabs;
}


}



** ** ** ** ** **
** ** ** OrderTab.java

package ordership;
// Generated Feb 8, 2007 3:26:27 PM by Hibernate Tools 3.2.0.b9


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

/**
* OrderTab generated by hbm2java
*/
public class OrderTab implements java.io.Serializable {


private int orderKey;
private String customerName;
private String customerEmail;
private String customerPhone;
private Set partTabs = new HashSet(0);

public OrderTab() {
}


public OrderTab(int orderKey, String customerPhone) {
this.orderKey = orderKey;
this.customerPhone = customerPhone;
}
public OrderTab(int orderKey, String customerName, String customerEmail, String customerPhone, Set partTabs) {
this.orderKey = orderKey;
this.customerName = customerName;
this.customerEmail = customerEmail;
this.customerPhone = customerPhone;
this.partTabs = partTabs;
}

public int getOrderKey() {
return this.orderKey;
}

public void setOrderKey(int orderKey) {
this.orderKey = orderKey;
}
public String getCustomerName() {
return this.customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerEmail() {
return this.customerEmail;
}

public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public String getCustomerPhone() {
return this.customerPhone;
}

public void setCustomerPhone(String customerPhone) {
this.customerPhone = customerPhone;
}
public Set getPartTabs() {
return this.partTabs;
}

public void setPartTabs(Set partTabs) {
this.partTabs = partTabs;
}


}





** ** ** ** ** **
** ** ** parttab.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">
<!-- Generated Feb 6, 2007 11:08:18 AM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="ordership.PartTab" table="PART_TAB" schema="ADMINISTRATOR" node="parttab" >
<id name="partKey" type="int" node="@id" >
<column name="PART_KEY" />
<generator class="assigned" />
</id>
<many-to-one name="orderTab" class="ordership.OrderTab" fetch="select"
node="ordertab/@id" embed-xml="false" >
<column name="ORDER_KEY" not-null="true" />
</many-to-one>
<property name="color" type="string" node="color" >
<column name="COLOR" length="6" />
</property>
<property name="quantity" type="java.lang.Integer" node="quantity" >
<column name="QUANTITY" />
</property>
<property name="price" type="big_decimal" node="price" >
<column name="PRICE" precision="10" />
</property>
<property name="tax" type="java.lang.Float" node="tax" >
<column name="TAX" precision="24" scale="0" />
</property>
<!--
<set name="shipTabs" inverse="true" node=".">
<key>
<column name="PART_KEY" not-null="true" />
</key>
<one-to-many class="ordership.ShipTab" embed-xml="false" node="shiptab" />
</set> -->
</class>
</hibernate-mapping>


** ** ** ** ** **
** ** ** ordertab.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">
<!-- Generated Feb 6, 2007 11:08:18 AM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="ordership.OrderTab" table="ORDER_TAB" schema="ADMINISTRATOR" >
<id name="orderKey" type="int" node="@id" >
<column name="ORDER_KEY" />
<generator class="assigned" />
</id>
<property name="customerName" type="string" node="customername" >
<column name="CUSTOMER_NAME" length="16" />
</property>
<property name="customerEmail" type="string" node="customeremail" >
<column name="CUSTOMER_EMAIL" length="16" />
</property>
<property name="customerPhone" type="string" node="customerphone" >
<column name="CUSTOMER_PHONE" length="16" not-null="true" />
</property>
<set name="partTabs" inverse="true" node="." >
<key>
<column name="ORDER_KEY" not-null="true" />
</key>
<one-to-many class="ordership.PartTab" embed-xml="true" node="parttab" />
</set>
</class>
</hibernate-mapping>



C:\dev\eclipse\OrderShip\bin>runordership > runOrderShip.out.txt
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
at ordership.SalesDB2XML.resultsAsXMLelement(Unknown Source)
at ordership.SalesDB2XML.main(Unknown Source)

C:\dev\eclipse\OrderShip\bin>

_________________
KBD


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.