Hi Guys I am getting a class cast exception when trying to create a simple one to many relationship. I have included the stack trace, the code that is causing it, the configuration file and the persistent object. What am I doing wrong?
hibernate 3.1
/* * Created on 2 Sep 2007 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package com.hibernate.skylite;
import java.util.HashSet; import java.util.Set;
public class OrderDetails { private int orderDetailsID; private String address; private String lengthOfService; private String makeOfBox; private String name; private String postCode; private String typeOfBox; private String typeOfPayment; private String salesPersonName; private String charity; private String existingFaults; private String price; private String telephoneNumber; private String mobileNumber; private String agreementID; private String notes; private String date; private Set creditCards; private Set directDebits;
public Set getDirectDebits() { return directDebits; }
public void setDirectDebits(Set directDebits) { this.directDebits = directDebits; }
public OrderDetails(){ }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public String getAgreementID() { return agreementID; }
public void setAgreementID(String agreementID) { this.agreementID = agreementID; }
public String getCharity() { return charity; }
public void setCharity(String charity) { this.charity = charity; }
public String getDate() { return date; }
public void setDate(String date) { this.date = date; }
public String getExistingFaults() { return existingFaults; }
public void setExistingFaults(String existingFaults) { this.existingFaults = existingFaults; }
public String getLengthOfService() { return lengthOfService; }
public void setLengthOfService(String lengthOfService) { this.lengthOfService = lengthOfService; }
public String getMakeOfBox() { return makeOfBox; }
public void setMakeOfBox(String makeOfBox) { this.makeOfBox = makeOfBox; }
public String getMobileNumber() { return mobileNumber; }
public void setMobileNumber(String mobileNumber) { this.mobileNumber = mobileNumber; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getNotes() { return notes; }
public void setNotes(String notes) { this.notes = notes; }
public int getOrderDetailsID() { return orderDetailsID; }
public void setOrderDetailsID(int orderDetailsID) { this.orderDetailsID = orderDetailsID; }
public String getPostCode() { return postCode; }
public void setPostCode(String postCode) { this.postCode = postCode; }
public String getPrice() { return price; }
public void setPrice(String price) { this.price = price; }
public String getSalesPersonName() { return salesPersonName; }
public void setSalesPersonName(String salesPersonName) { this.salesPersonName = salesPersonName; }
public String getTelephoneNumber() { return telephoneNumber; }
public void setTelephoneNumber(String telephoneNumber) { this.telephoneNumber = telephoneNumber; }
public String getTypeOfBox() { return typeOfBox; }
public void setTypeOfBox(String typeOfBox) { this.typeOfBox = typeOfBox; }
public String getTypeOfPayment() { return typeOfPayment; }
public void setTypeOfPayment(String typeOfPayment) { this.typeOfPayment = typeOfPayment; }
public Set getCreditCards() { return creditCards; }
public void setCreditCards(CardDetails creditCards) { this.creditCards.add(creditCards); } }
<?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> <class name="com.hibernate.skylite.OrderDetails" table="orderdetails"> <id name="orderDetailsID" column="orderDetailsID" type="java.lang.Integer"> <generator class="increment"/> </id> <property name="address" column="address" type="java.lang.String"/> <property name="lengthOfService" column="sortcode" type="java.lang.String"/> <property name="makeOfBox" column="makeOfBox" type="java.lang.String"/> <property name="name" column="name" type="java.lang.String"/> <property name="postCode" column="postCode" type="java.lang.String"/> <property name="typeOfBox" column="typeOfBox" type="java.lang.String"/> <property name="typeOfPayment" column="typeOfPayment" type="java.lang.String"/> <property name="salesPersonName" column="salesPersonName" type="java.lang.String"/> <property name="charity" column="charity" type="java.lang.String"/> <property name="existingFaults" column="existingFaults" type="java.lang.String"/> <property name="price" column="price" type="java.lang.Integer"/> <property name="telephoneNumber" column="telephoneNumber" type="java.lang.String"/> <property name="mobileNumber" column="mobileNumber" type="java.lang.Integer"/> <property name="agreementID" column="agreementID" type="java.lang.String"/> <property name="notes" column="notes" type="java.lang.String"/> <property name="date" column="date" type="java.lang.String"/> <set name="creditCards" table="cardpayment" lazy="true" cascade="all" inverse="true"> <key column="creditCardID"></key> <one-to-many class="com.hibernate.skylite.CardDetails"/> </set> <set name="directDebits" table="directdebit" lazy="true" cascade="all" inverse="true"> <key column="directDebitID"></key> <one-to-many class="com.hibernate.skylite.DirectDebitDetails"/> </set> </class> </hibernate-mapping>
Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); OrderDetails od = new OrderDetails(); od.setAddress("Hello"); session.save(od); tx.commit(); session.close();
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. %%%% Error Creating SessionFactory %%%% java.lang.ClassCastException: org.hibernate.mapping.Set at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1041) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168) at com.hibernate.skylite.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:60) at com.hibernate.skylite.HibernateSessionFactory.getSession(HibernateSessionFactory.java:43) at com.hibernate.skylite.TestClass.main(TestClass.java:19) Exception in thread "main" java.lang.NullPointerException at com.hibernate.skylite.TestClass.main(TestClass.java:20)
Name and version of the database you are using:MySQL
|