OK here is the complete code with xml.
Code:
<?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>
<property name="hibernate.connection.driver_class">com.informix.jdbc.IfxDriver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:informix-sqli://****:****/rurl2_cadb00:INFORMIXSERVER=*****</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.dialect">org.hibernate.dialect.InformixDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Part.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code:
<?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>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="Part" table="_part">
<composite-id name="id" class="PartId">
<key-property name="partNumber" type="string">
<column name="partNumber" length="20" />
</key-property>
<key-property name="supplier" type="string">
<column name="supplier" length="20" />
</key-property>
</composite-id>
<many-to-one name="parent" class="Part">
<column name="parent_PartNumber" length="20" />
<column name="parent_Supplier" length="20" />
</many-to-one>
<property name="description" type="string">
<column name="description" length="100" />
</property>
<set name="parts" inverse="true">
<key>
<column name="parent_PartNumber" length="20" />
<column name="parent_Supplier" length="20" />
</key>
<one-to-many class="Part"/>
</set>
</class>
</hibernate-mapping>
Code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TestProjectDAO extends JFrame{
public static void main(String[] args) {
TestProjectDAO pr = new TestProjectDAO();
pr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pr.setSize(400,300);
pr.setExtendedState(JFrame.MAXIMIZED_BOTH);
pr.getContentPane().setLayout(new BorderLayout());
pr.getContentPane().add(new PartViewer(),BorderLayout.CENTER);
pr.setVisible(true);
}
}
Code:
import javax.swing.tree.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class RefreshTree implements Runnable{
private DefaultTreeModel model;
private PartId partId;
private DefaultMutableTreeNode root;
public RefreshTree(DefaultTreeModel model,PartId partId){
this.model = model;
this.partId = partId;
}
public void run(){
try{
Session session = HibernateUtil.currentSession();
//Transaction tx = session.beginTransaction();
//tx.rollback();
Part myPart = new Part();
session.load(myPart,partId);
//tx.commit();
//HibernateUtil.closeSession();
viewPart(myPart,null);
}
catch (Exception e){
root = new DefaultMutableTreeNode(e.toString());
}
model.setRoot(root);
}
private void viewPart(Part part,DefaultMutableTreeNode node){
// Check if root
if (node == null){
root = new DefaultMutableTreeNode(getPartDescription(part));
node = root;
}
else{
node.add(new DefaultMutableTreeNode(getPartDescription(part)));
}
//Check Parent
if (part.getParent() != null){
node.add(new DefaultMutableTreeNode("Parent -> " + getPartDescription(part.getParent())));
}
//Children
if (part.getParts() != null){
while (part.getParts().iterator().hasNext()){
Part childPart = (Part) part.getParts().iterator().next();
viewPart(childPart,node);
}
}
}
private String getPartDescription(Part part){
String tmp = "No Info ....";
tmp = trimMe(part.getId().getPartNumber());
tmp+= trimMe("," + part.getId().getSupplier());
tmp+= "[" + trimMe(part.getDescription()) + "]";
return tmp;
}
private String trimMe(String source){
if (source != null){
return source.trim();
}
return "";
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class PartViewer extends JPanel{
private JTextField txtPartNo = new JTextField("PARENT");
private JTextField txtSupplier = new JTextField("AMCE");
private JButton btnRefresh = new JButton("Refresh");
private DefaultMutableTreeNode root = new DefaultMutableTreeNode("...");
private DefaultTreeModel model = new DefaultTreeModel(root);
public PartViewer(){
this.setLayout(new BorderLayout());
JPanel pnlPartSelection = new JPanel();
pnlPartSelection.setLayout(new GridLayout(1,5));
pnlPartSelection.add(new JLabel("Part No"));
pnlPartSelection.add(txtPartNo);
pnlPartSelection.add(new JLabel("Supplier"));
pnlPartSelection.add(txtSupplier);
pnlPartSelection.add(btnRefresh);
add(pnlPartSelection,BorderLayout.NORTH);
JTree tree = new JTree(model);
add(new JScrollPane(tree),BorderLayout.CENTER);
btnRefresh.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
refreshPart();
}
});
}
public void refreshPart(){
try{
model.setRoot(new DefaultMutableTreeNode("Refreshing ... "));
PartId id = new PartId();
id.setPartNumber(txtPartNo.getText());
id.setSupplier(txtSupplier.getText());
new Thread(new RefreshTree(model,id)).start();
}
catch (Exception e){
e.printStackTrace();
}
}
}
Code:
import java.util.*;
public class PartId implements java.io.Serializable {
// Fields
private String partNumber;
private String supplier;
// Constructors
/** default constructor */
public PartId() {
}
// Property accessors
/**
*
*/
public String getPartNumber() {
return this.partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
/**
*
*/
public String getSupplier() {
return this.supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof PartId) ) return false;
PartId castOther = ( PartId ) other;
return ( (this.getPartNumber()==castOther.getPartNumber()) || ( this.getPartNumber()!=null && castOther.getPartNumber()!=null && this.getPartNumber().equals(castOther.getPartNumber()) ) )
&& ( (this.getSupplier()==castOther.getSupplier()) || ( this.getSupplier()!=null && castOther.getSupplier()!=null && this.getSupplier().equals(castOther.getSupplier()) ) );
}
public int hashCode() {
int result = 17;
result = 37 * result + ( getPartNumber() == null ? 0 : this.getPartNumber().hashCode() );
result = 37 * result + ( getSupplier() == null ? 0 : this.getSupplier().hashCode() );
return result;
}
}
Code:
import java.util.*;
public class Part implements java.io.Serializable {
// Fields
private PartId id;
private Part parent;
private String partNumber;
private String supplier;
private String description;
private Set parts;
// Constructors
/** default constructor */
public Part() {
}
/** constructor with id */
public Part(PartId id) {
this.id = id;
}
// Property accessors
/**
*
*/
public PartId getId() {
return this.id;
}
public void setId(PartId id) {
this.id = id;
}
/**
*
*/
public Part getParent() {
return this.parent;
}
public void setParent(Part parent) {
this.parent = parent;
}
/**
*
*/
public String getPartNumber() {
return this.partNumber;
}
public void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
/**
*
*/
public String getSupplier(){
return this.supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
/**
*
*/
public String getDescription(){
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
/**
*
*/
public Set getParts() {
return this.parts;
}
public void setParts(Set parts) {
this.parts = parts;
}
}
Code:
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.apache.commons.logging.*;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
DB2 SQL Scripts
Code:
CREATE TABLE _part (
PARTNUMBER CHAR(2) NOT NULL ,
SUPPLIER CHAR(20) NOT NULL ,
DESCRIPTION CHAR(100) ,
PARENT_PARTNUMBER CHAR(20) ,
PARENT_SUPPLIER CHAR(20) ) ;
ALTER TABLE PARTS
ADD PRIMARY KEY
(PARTNUMBER,
SUPPLIER);
ALTER TABLE PARTS
ADD CONSTRAINT PARENT_PARTS FOREIGN KEY
(PARENT_PARTNUMBER,
PARENT_SUPPLIER)
REFERENCES _part
(PARTNUMBER,
SUPPLIER);
INSERT INTO _part VALUES ('PARENT', 'AMCE', 'Top Part', null, null);
INSERT INTO _part VALUES ('CHILD1', 'AMCE', 'cHILD 1', 'PARENT', 'AMCE');
INSERT INTO _part VALUES ('CHILD2', 'AMCE', 'CHILD 2', 'PARENT', 'AMCE');
Informix
Code:
CREATE TABLE _part (
PARTNUMBER CHAR(20) NOT NULL ,
SUPPLIER CHAR(20) NOT NULL ,
DESCRIPTION CHAR(100) ,
PARENT_PARTNUMBER CHAR(20) ,
PARENT_SUPPLIER CHAR(20) ,
PRIMARY KEY (PARTNUMBER,SUPPLIER),
FOREIGN KEY (PARENT_PARTNUMBER,PARENT_SUPPLIER)
REFERENCES _part (PARTNUMBER,SUPPLIER));
INSERT INTO _part VALUES ('PARENT', 'AMCE', 'Top Part', null, null);
INSERT INTO _part VALUES ('CHILD1', 'AMCE', 'cHILD 1', 'PARENT', 'AMCE');
INSERT INTO _part VALUES ('CHILD2', 'AMCE', 'CHILD 2', 'PARENT', 'AMCE');