Hi all i have a problem with my class files implementing a 'cid':
my class are this ones:
Code:
/**
* @author "luker"
* @version $Id: $
*/
@Entity
@IdClass(PromotionInSingleCategory.Pk.class)
@Table(name = "PROMOTION_IN_SINGLE_CATEGORY")
@Immutable
@Proxy(lazy = false)
public class PromotionInSingleCategory implements java.io.Serializable
{
/**
*
*/
private static final long serialVersionUID = -1129976918376527071L;
@Id
private Integer categoryId;
@Id
private PromotionRule promotionRule;
@Column( name = "level", nullable = false)
private Integer level;
/**
* Returns the category.
* @return the category
*/
public Integer getCategoryId()
{
return categoryId;
}
/**
* Sets the category.
* @param category the category to set
*/
public void setCategoryId(Integer categoryId)
{
this.categoryId = categoryId;
}
/**
* Returns the promotionRule.
* @return the promotionRule
*/
public PromotionRule getPromotionRule()
{
return promotionRule;
}
/**
* Sets the promotionRule.
* @param promotionRule the promotionRule to set
*/
public void setPromotionRule(PromotionRule promotionRule)
{
this.promotionRule = promotionRule;
}
public void setLevel(Integer level)
{
this.level = level;
}
public Integer getLevel()
{
return level;
}
/**
* Empty constructor.
*/
public PromotionInSingleCategory()
{
}
/**
* Id constructor
* @param id Id
*/
public PromotionInSingleCategory(Pk id)
{
this.categoryId = id.getCategoryId();
this.promotionRule = id.getPromotionRule();
}
/**
* Primary key as inner class.
* @author luker
*/
//@Embeddable
@Proxy(lazy = false)
public class Pk implements Serializable
{
private static final long serialVersionUID = -5516338798829791594L;
@Column(name = "ID_CATEGORY", nullable = false)
private Integer categoryId;
//@ManyToOne
@Column(name = "ID_PROMOTION_RULE", nullable = false)
private PromotionRule promotionRule;
/**
* Returns the categoryId.
* @return the categoryId
*/
public Integer getCategoryId()
{
return categoryId;
}
/**
* Sets the categoryId.
* @param categoryId the categoryId to set
*/
public void setCategoryId(Integer categoryId)
{
this.categoryId = categoryId;
}
/**
* Returns the promotionRule.
* @return the promotionRule
*/
public PromotionRule getPromotionRule()
{
return promotionRule;
}
/**
* Sets the promotionRule.
* @param promotionRule the promotionRule to set
*/
public void setPromotionRule(PromotionRule promotionRule)
{
this.promotionRule = promotionRule;
}
/**
* Empty constructor.
*/
public Pk()
{
}
/**
* Key constructor
* @param productId Product id
* @param contributor Contributor
* @param contributorRole ContributorRoleFOS
*/
public Pk(Integer categoryId, PromotionRule promotionRule)
{
this.categoryId = categoryId;
this.promotionRule = promotionRule;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object object)
{
if (!(object instanceof Pk))
{
return false;
}
Pk rhs = (Pk) object;
return new EqualsBuilder().append(this.getCategoryId(),
rhs.getCategoryId()).append(this.getPromotionRule(), rhs.getPromotionRule()).isEquals();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode()
{
return new HashCodeBuilder(-1132495773, -522090417).append(this.getCategoryId()).append(
this.getPromotionRule()).toHashCode();
}
}
Code:
@Entity
@Immutable
@Table(name = "CATEGORY")
@Proxy(lazy = false)
public class Category implements java.io.Serializable
{
private static final long serialVersionUID = -4618588281659768575L;
@Id
@Column(name = "ID_CATEGORY")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
/**
* Parent of the category. Null if this is a top level category.
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_CATEGORY_PARENT")
private Category parent;
/**
* All the subcategories.
*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
private Set<Category> subcategories;
@Column(name = "DESCRIPTION", length = 64)
private String shortDescription;
@Column(name = "CATEGORY_LEVEL")
private Integer level;
//@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = PromotionInSingleCategory.class )
//@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY )
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "categoryId", targetEntity = PromotionInSingleCategory.class)
private List<PromotionInSingleCategory> promotionInSingleCategory;
/**
* Empty constructor
*/
public Category()
{
}
/**
* Id constructor
* @param id Id
*/
public Category(Integer id)
{
this.id = id;
}
/**
* @return the id
*/
public Integer getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id)
{
this.id = id;
}
/**
* @return the parent
*/
public Category getParent()
{
return parent;
}
/**
* @param parent the parent to set
*/
public void setParent(Category parent)
{
this.parent = parent;
}
/**
* @return the shortDescription
*/
public String getShortDescription()
{
return shortDescription;
}
/**
* @param shortDescription the shortDescription to set
*/
public void setShortDescription(String shortDescription)
{
this.shortDescription = shortDescription;
}
/**
* Returns the level.
* @return the level
*/
public Integer getLevel()
{
return level;
}
/**
* Sets the level.
* @param level the level to set
*/
public void setLevel(Integer level)
{
this.level = level;
}
/**
* Returns the subcategories.
* @return the subcategories
*/
public Set<Category> getSubcategories()
{
return subcategories;
}
/**
* Returns the subcategories.
* @return the subcategories
*/
public SortedSet<Category> getSortedSubcategories()
{
if (subcategories == null)
{
return null;
}
SortedSet<Category> sortedCategories = new TreeSet<Category>(new Comparator<Category>()
{
@Override
public int compare(Category o1, Category o2)
{
return StringUtils.defaultString(o1.getShortDescription()).compareTo(
StringUtils.defaultString(o2.getShortDescription()));
}
});
sortedCategories.addAll(subcategories);
return sortedCategories;
}
/**
* Sets the subcategories.
* @param subcategories the subcategories to set
*/
public void setSubcategories(Set<Category> subcategories)
{
this.subcategories = subcategories;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object object)
{
if (!(object instanceof Category))
{
return false;
}
Category rhs = (Category) object;
return new EqualsBuilder().append(this.getId(), rhs.getId()).isEquals();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode()
{
return new HashCodeBuilder(-1102371821, 947897663).append(this.getId()).toHashCode();
}
/**
* @return
*/
public List<PromotionInSingleCategory> getPromotionInSingleCategory()
{
return this.promotionInSingleCategory;
}
/**
* Sets the promotionInSingleCategory.
* @param promotionInSingleCategory the promotionInSingleCategory to set
*/
public void setPromotionInSingleCategory(List<PromotionInSingleCategory> promotionInSingleCategory)
{
this.promotionInSingleCategory = promotionInSingleCategory;
}
}
Code:
@Entity
@Immutable
@Table(name = "PROMOTION_RULE")
@Proxy(lazy = false)
public class PromotionRule implements Serializable
{
private static final long serialVersionUID = 5711705936344986478L;
@Id
@Column(name = "ID_PROMOTION_RULE", length = 24)
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
@Column(name = "DESCRIPTION", length = 256)
private String description;
@Column(name = "SEG_ID", length = 256)
private String segment;
@Column(name = "DISCOUNT_METHOD")
@Enumerated(EnumType.STRING)
private DiscountMethodType discountMethod;
@Column(name = "TARGET_PROMO")
@Enumerated(EnumType.STRING)
private TargetPromoType targetPromo;
@Column(name = "PROMO_TYPE")
@Enumerated(EnumType.STRING)
private PromoType promoType;
@Column(name = "PROMOTION_VALUE")
private BigDecimal promotionValue;
/**
* Used only for the point promotion on order amount. It's the min amount to decide if include a product.
*/
@Column(name = "MIN_AMOUNT")
private BigDecimal minAmount;
/**
* Used only for the point promotion on order amount. It's the step quantity to calculate the points.
*/
@Column(name = "STEP_QUANTITY")
private BigDecimal stepQuantity;
@Column(name = "START_DATE")
@Temporal(TemporalType.DATE)
private Calendar startDate;
@Column(name = "END_DATE")
@Temporal(TemporalType.DATE)
private Calendar endDate;
/**
* TIME is not castable as calendar. It's better to switch to a sigle custom type for date and time on 2 columns.
*/
@Column(name = "START_TIME")
@Temporal(TemporalType.TIME)
private Date startTime;
@Column(name = "END_TIME")
@Temporal(TemporalType.TIME)
private Date endTime;
/**
* Transient field used to put the discount code for point redemption. This code is generated by fcom.
* @deprecated this is an ugly hack, put it on db or remove.
*/
@Transient
@Deprecated
private Integer discountCode;
/**
* Empty constructor
*/
public PromotionRule()
{
}
/**
* Id constructor
* @param id Id
*/
public PromotionRule(String id)
{
this.id = id;
}
/**
* Returns the id.
* @return the id
*/
public String getId()
{
return id;
}
/**
* Sets the id.
* @param id the id to set
*/
public void setId(String id)
{
this.id = id;
}
/**
* Returns the description.
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* Sets the description.
* @param description the description to set
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* Returns the DiscountMethodType.
* @return the DiscountMethodType
*/
public DiscountMethodType getDiscountMethod()
{
return discountMethod;
}
/**
* Sets the DiscountMethodType.
* @param discountMethodType DiscountMethodType
*/
public void setDiscountMethod(DiscountMethodType discountMethodType)
{
this.discountMethod = discountMethodType;
}
/**
* Returns the promotionValue.
* @return the promotionValue
*/
public BigDecimal getPromotionValue()
{
return promotionValue;
}
/**
* Sets the promotionValue.
* @param promotionValue the promotionValue to set
*/
public void setPromotionValue(BigDecimal promotionValue)
{
this.promotionValue = promotionValue;
}
/**
* Returns the startDate.
* @return the startDate
*/
public Calendar getStartDate()
{
return startDate;
}
/**
* Sets the startDate.
* @param startDate the startDate to set
*/
public void setStartDate(Calendar startDate)
{
this.startDate = startDate;
}
/**
* Returns the endDate.
* @return the endDate
*/
public Calendar getEndDate()
{
return endDate;
}
/**
* Sets the endDate.
* @param endDate the endDate to set
*/
public void setEndDate(Calendar endDate)
{
this.endDate = endDate;
}
/**
* Returns the startTime.
* @return the startTime
*/
public Date getStartTime()
{
return startTime;
}
/**
* Sets the startTime.
* @param startTime the startTime to set
*/
public void setStartTime(Date startTime)
{
this.startTime = startTime;
}
/**
* Returns the endTime.
* @return the endTime
*/
public Date getEndTime()
{
return endTime;
}
/**
* Sets the endTime.
* @param endTime the endTime to set
*/
public void setEndTime(Date endTime)
{
this.endTime = endTime;
}
/**
* Return a Calendar as start istant of validity for the promotion. The calendar is created by start date a start
* time.
* @return start instant
*/
public Calendar getStartInstant()
{
return composeCalendarWith(getStartDate(), getStartTime());
}
/**
* Return a Calendar as end istant of validity for the promotion. The calendar is created by end date a end time.
* @return start instant
*/
public Calendar getEndInstant()
{
return composeCalendarWith(getEndDate(), getEndTime());
}
/**
* @param date
* @param time
*/
private Calendar composeCalendarWith(Calendar date, Date time)
{
if (date != null && time != null)
{
return new GregorianCalendar(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date
.get(Calendar.DAY_OF_MONTH), time.getHours(), time.getMinutes(), time.getSeconds());
}
else
{
return null;
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object object)
{
if (!(object instanceof PromotionRule))
{
return false;
}
PromotionRule rhs = (PromotionRule) object;
return new EqualsBuilder().append(this.getId(), rhs.getId()).isEquals();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode()
{
return new HashCodeBuilder(-1613394857, 1921563479).append(this.getId()).toHashCode();
}
/**
* {@inheritDoc}}
*/
@Override
public String toString()
{
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("id", this.id)
.append("description", this.description)
.append("promotionType", this.discountMethod)
.append("promotionValue", this.promotionValue)
.append("endTime", this.endTime)
.append("startTime", this.startTime)
.append("endDate", this.endDate)
.append("startDate", this.startDate)
.toString();
}
/**
* @return the segment
*/
public String getSegment()
{
return segment;
}
/**
* @param segment the segment to set
*/
public void setSegment(String segment)
{
this.segment = segment;
}
/**
* Returns the minAmount.
* @return the minAmount
*/
public BigDecimal getMinAmount()
{
return minAmount;
}
/**
* Sets the minAmount.
* @param minAmount the minAmount to set
*/
public void setMinAmount(BigDecimal minAmount)
{
this.minAmount = minAmount;
}
/**
* Returns the stepQuantity.
* @return the stepQuantity
*/
public BigDecimal getStepQuantity()
{
return stepQuantity;
}
/**
* Sets the stepQuantity.
* @param stepQuantity the stepQuantity to set
*/
public void setStepQuantity(BigDecimal stepQuantity)
{
this.stepQuantity = stepQuantity;
}
/**
* Returns the targetPromo.
* @return the targetPromo
*/
public TargetPromoType getTargetPromo()
{
return targetPromo;
}
/**
* Sets the targetPromo.
* @param targetPromo the targetPromo to set
*/
public void setTargetPromo(TargetPromoType targetPromo)
{
this.targetPromo = targetPromo;
}
/**
* Returns the promoType.
* @return the promoType
*/
public PromoType getPromoType()
{
return promoType;
}
/**
* Sets the promoType.
* @param promoType the promoType to set
*/
public void setPromoType(PromoType promoType)
{
this.promoType = promoType;
}
/**
* Returns the discountCodeForPointRedemption.
* @return the discountCodeForPointRedemption
* @deprecated this is an ugly hack, put it on db or remove.
*/
@Deprecated
public Integer getDiscountCode()
{
return discountCode;
}
/**
* Sets the discountCodeForPointRedemption.
* @param discountCodeForPointRedemption the discountCodeForPointRedemption to set
* @deprecated this is an ugly hack, put it on db or remove.
*/
@Deprecated
public void setDiscountCode(Integer discountCodeForPointRedemption)
{
this.discountCode = discountCodeForPointRedemption;
}
}
The calling throwing exception is this:
Code:
Assert.assertNotNull(category.getPromotionInSingleCategory());
List<PromotionInSingleCategory> lista = thirdLevelCategory.getPromotionInSingleCategory();
if ( lista != null ) {
Assert.assertEquals(lista.size(), 2);
}
Hibernate version: 3.2.6
warning from execution:
WARN org.hibernate.engine.loading.LoadContexts.cleanup(LoadContexts.java:108) fail-safe cleanup (collections) : org.hibernate.engine.loading.CollectionLoadContext@6cae00e3<rs=org.apache.commons.dbcp.DelegatingResultSet@40442b95>
WARN org.hibernate.engine.loading.CollectionLoadContext.cleanup(CollectionLoadContext.java:332) On CollectionLoadContext#cleanup, localLoadingCollectionKeys contained [1] entries
Full stack trace of any exception that occurs:org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
at org.hibernate.type.SerializableType.get(SerializableType.java:39)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.type.ComponentType.hydrate(ComponentType.java:560)
at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:275)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1097)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:565)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at it.lafeltrinelli.product.dao.CategoryDAOTest.testMapping(CategoryDAOTest.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
at org.testng.internal.MethodHelper$1.runTestMethod(MethodHelper.java:698)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeHookable(MethodHelper.java:706)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:468)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:617)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:885)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
at org.testng.TestRunner.runWorkers(TestRunner.java:712)
at org.testng.TestRunner.privateRun(TestRunner.java:582)
at org.testng.TestRunner.run(TestRunner.java:477)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
at org.testng.SuiteRunner.run(SuiteRunner.java:198)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823)
at org.testng.TestNG.runSuitesLocally(TestNG.java:790)
at org.testng.TestNG.run(TestNG.java:708)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
Caused by: java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
... 51 more
Name and version of the database you are using:sql server 2003
The generated SQL (show_sql=true): select promotioni0_.ID_CATEGORY as ID1_1_, promotioni0_.ID_PROMOTION_RULE as ID2_1_, promotioni0_.ID_CATEGORY as ID1_46_0_, promotioni0_.ID_PROMOTION_RULE as ID2_46_0_, promotioni0_.level as level46_0_ from PROMOTION_IN_SINGLE_CATEGORY promotioni0_ where promotioni0_.ID_CATEGORY=?