I am currently trying to convert a project from using XDoclet to Hibernate Annotations, and am having trouble with cascades. The original code, shown below, worked fine, and when an entry in the ZipCode table was deleted, corresponding entries in the channel_zipcode table were deleted. However, when using Hibernate Annotations, when I try to remove a ZipCode entry, Hibernate tries to delete from the Channel table as well, which is both unwanted and causes errors.
The code is as follows:
Code:
/**
* @hibernate.class table="zip_code"
*/
public class ZipCode extends BaseObject implements Serializable
{
protected Long zip_code;
protected Set<Channel> channels = new HashSet<Channel>();
/**
* @return Returns the channels.
* @hibernate.set table="channel_zipCode"
* lazy="false"
* @hibernate.key column="zipCode_id"
* @hibernate.many-to-many class="Channel"
* column="channel_id"
*/
public Set<Channel> getChannels()
{
return channels;
}
public void setChannels(Set<Channel> channels)
{
this.channels = channels;
}
public void addChannel(Channel channel)
{
getChannels().add(channel);
}
/**
* @return Returns the zip code.
* @hibernate.id column="zip_code"
* unsaved-value="null"
*/
public Long getZip_code()
{
return zip_code;
}
public void setZip_code(Long zip_code)
{
this.zip_code = zip_code;
}
}
/**
* @hibernate.class table="channel"
*/
public class Channel extends BaseObject implements Serializable
{
protected Long service_uid;
protected Long service_id;
protected String call_letters;
protected Set<ZipCode> zip_codes = new HashSet<ZipCode>();
protected Set<Program> programs = new HashSet<Program>();
/**
* @hibernate.id column="channel_num"
* length="12"
* unique="true"
*/
public Long getChannel_num()
{
return channel_num;
}
public void setChannel_num(Long channel_num)
{
this.channel_num = channel_num;
}
/**
* @hibernate.property column="call_letters"
* length="12"
* unsaved-value="null"
*/
public String getCall_letters()
{
return call_letters;
}
public void setCall_letters(String call_letters)
{
this.call_letters = call_letters;
}
/**
* @return Returns the programs.
* @hibernate.set name="programs"
* lazy="true"
* cascade="all"
* @hibernate.key column="channel_num"
* @hibernate.one-to-many class="Program"
*/
public Set<Program> getPrograms()
{
return programs;
}
public void setPrograms(Set<Program> programs)
{
this.programs = programs;
}
public void addProgram(Program program)
{
getPrograms().add(program);
}
/**
* @return Returns the zip codes.
* @hibernate.set table="channel_zipCode"
* lazy="true"
* cascade="all"
* @hibernate.key column="channel_id"
* @hibernate.many-to-many class="ZipCode"
* column="zipCode_id"
*/
public Set<ZipCode> getZipCodes()
{
return zip_codes;
}
public void setZipCodes(Set<ZipCode> zipcodes)
{
this.zip_codes = zipcodes;
}
public void addZipCode(ZipCode zip)
{
getZipCodes().add(zip);
}
}
@Entity
@Table(name="ZipCode")
public class ZipCode extends BaseObject implements Serializable
{
protected Long zip_code;
protected Set<Channel> channels = new HashSet<Channel>();
@ManyToMany(mappedBy="zipCodes",
targetEntity=Channel.class,
fetch=FetchType.LAZY,
cascade=CascadeType.ALL)
public Set<Channel> getChannels()
{
return channels;
}
public void setChannels(Set<Channel> channels)
{
this.channels = channels;
}
public void addChannel(Channel channel)
{
getChannels().add(channel);
}
@Id
public Long getZip_code()
{
return zip_code;
}
public void setZip_code(Long zip_code)
{
this.zip_code = zip_code;
}
}
@Entity
@Table(name="Channel")
public class Channel extends BaseObject implements Serializable
{
protected Long service_uid;
protected Long service_id;
protected String call_letters;
protected Set<ZipCode> zipCodes = new HashSet<ZipCode>();
protected Set<Program> programs = new HashSet<Program>();
@Id
public Long getChannel_num()
{
return channel_num;
}
public void setChannel_num(Long channel_num)
{
this.channel_num = channel_num;
}
@Column(name="call_letters", nullable=true, length=12)
public String getCall_letters()
{
return call_letters;
}
public void setCall_letters(String call_letters)
{
this.call_letters = call_letters;
}
@OneToMany(mappedBy="channel")
public Set<Program> getPrograms()
{
return programs;
}
public void setPrograms(Set<Program> programs)
{
this.programs = programs;
}
public void addProgram(Program program)
{
getPrograms().add(program);
}
@ManyToMany(targetEntity=ZipCode.class,
fetch=FetchType.LAZY)
@JoinTable(name="channel_zipCode",
joinColumns={@JoinColumn(name="channel_id")},
inverseJoinColumns={@JoinColumn(name="zipCode_id")})
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Set<ZipCode> getZipCodes()
{
return zipCodes;
}
public void setZipCodes(Set<ZipCode> zipcodes)
{
this.zipCodes = zipcodes;
}
public void addZipCode(ZipCode zip)
{
getZipCodes().add(zip);
}
}
Does anyone have any ideas on how I could resolve these issues?