I'm using java persistence to save a list of entities that are associated to another entity. Here's a quick rundown of where I'm having some problems.
@Entity public class Offer implements Serializable {
@Id private Long offerId;
@OneToMany
@Column List<OfferCategory> offerCategories;
}
@Entity public class OfferCategory implements Serializable {
@Embeddable public static class Id implements Serializable
{
@Column(name="offer_id")
private Long offerId;
@Column(name="category_id")
private Long categoryId;
public Id() {}
public Id(Long offerId, Long categoryId) {
this.offerId = offerId;
this.categoryId = categoryId;
}
public boolean equals(Object o) {
if(o != null && o instanceof Id) {
Id other = (Id) o;
return this.offerId.equals(other.offerId) &&
this.categoryId.equals(other.categoryId);
}
else
return false;
}
public int hashCode() {
return offerId.hashCode() + categoryId.hashCode();
}
}
@EmbeddedId private Id id = new Id();
}
Essentially, due to an architecture I cannot change, I need to save a list of Categories as being assigned to an Offer.
Right now, I'm getting a list of Categories from the user and then putting them into the offerCategories field of Offer. However, this doesn't work for new Offers, because there's no way for me to set the ID of a new item.
I'm new to JPA and Seam, so if someone could give me a nudge in the right direction it would be greatly appreciated.