Hi,
I have 3 tables: MetadataCollection, Item and CollectionItem. The primary key for CollectionItem is the composit key collection_id+item_id. So CollectionItem item has an @Embeddable CollectionItemPK class.
My question is how do I set up the @OneToMany between Item and CollectionItem and the second @OneToMany between MetadataCollection and CollectionItem.
Thanks for the help.
BTW - I have Hibernate Made Easy which explains setting up compound primary keys and one-to-many relationships. But it doesn't show how to solve my problem, a one-to-many relationship with a compound primary key.
My current code is below and giving my this error:
Code:
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: org.maflt.flashlit.pojo.CollectionI
em.Item in org.maflt.flashlit.pojo.Item.collectionItems
CollectionItem Code:
package org.maflt.flashlit.pojo;
import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;
/**
* <p>Pojo mapping TABLE collection_item</p>
* <p></p>
*
* <p>Generated at Mon Jun 15 20:37:52 MDT 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "collection_item", catalog = "tigger")
@SuppressWarnings("serial")
public class CollectionItem implements Serializable {
/**
* Primary key
*/
private CollectionItemPK collectionItemPK;
/**
* Attribute createdBy.
*/
private Integer createdBy;
/**
* Attribute creationDate.
*/
private Timestamp creationDate;
/**
* Attribute updateBy.
*/
private Integer updateBy;
/**
* Attribute lastUpdate.
*/
private Timestamp lastUpdate;
/**
* Get the primary key
*/
@Basic
@Id
public CollectionItemPK getCollectionItemPK() {
return this.collectionItemPK;
}
/**
* set the primary key
*/
public void setCollectionItemPK(CollectionItemPK collectionItemPK) {
this.collectionItemPK = collectionItemPK;
}
/**
* <p>
* </p>
* @return createdBy
*/
@Basic
@Column(name = "created_by")
public Integer getCreatedBy() {
return createdBy;
}
/**
* @param createdBy new value for createdBy
*/
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
/**
* <p>
* </p>
* @return creationDate
*/
@Basic
@Column(name = "creation_date")
public Timestamp getCreationDate() {
return creationDate;
}
/**
* @param creationDate new value for creationDate
*/
public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}
/**
* <p>
* </p>
* @return updateBy
*/
@Basic
@Column(name = "update_by")
public Integer getUpdateBy() {
return updateBy;
}
/**
* @param updateBy new value for updateBy
*/
public void setUpdateBy(Integer updateBy) {
this.updateBy = updateBy;
}
/**
* <p>
* </p>
* @return lastUpdate
*/
@Basic
@Column(name = "last_update")
public Timestamp getLastUpdate() {
return lastUpdate;
}
/**
* @param lastUpdate new value for lastUpdate
*/
public void setLastUpdate(Timestamp lastUpdate) {
this.lastUpdate = lastUpdate;
}
/**
* <p>Composite primary key for table collection_item</p>
*
* <p>Generated at Mon Jun 15 20:37:52 MDT 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*/
@SuppressWarnings("serial")
@Embeddable
public static class CollectionItemPK implements Serializable {
/**
* Attribute metacollection
*/
private Metacollection metacollection;
/**
* Attribute item
*/
private Item item;
/**
* get metacollection
*/
@ManyToOne
@JoinColumn(name = "collection_id")
public Metacollection getMetacollection() {
return this.metacollection;
}
/**
* set metacollection
*/
public void setMetacollection(Metacollection metacollection) {
this.metacollection = metacollection;
}
/**
* get item
*/
@ManyToOne
@JoinColumn(name = "item_id")
public Item getItem() {
return this.item;
}
/**
* set item
*/
public void setItem(Item item) {
this.item = item;
}
/**
* calculate hashcode
*/
@Override
public int hashCode()
{
//TODO : implement this method
return super.hashCode();
}
/**
* equals method
*/
@Override
public boolean equals(Object object)
{
//TODO : implement this method
return super.equals(object);
}
}
}
ItemCode:
package org.maflt.flashlit.pojo;
import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;
/**
* <p>Pojo mapping TABLE item</p>
* <p></p>
*
* <p>Generated at Mon Jun 15 20:37:53 MDT 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "item", catalog = "tigger")
@SuppressWarnings("serial")
public class Item implements Serializable {
/**
* Attribute itemId.
*/
private Long itemId;
/**
* Attribute installationId.
*/
private Integer installationId;
/**
* Attribute createdBy.
*/
private Integer createdBy;
/**
* Attribute creationDate.
*/
private Timestamp creationDate;
/**
* Attribute updateBy.
*/
private Integer updateBy;
/**
* Attribute lastUpdate.
*/
private Timestamp lastUpdate;
/**
* Attribute uri.
*/
private String uri;
/**
* Attribute identifier.
*/
private String identifier;
/**
* Attribute md5.
*/
private String md5;
/**
* List of CollectionItem
*/
private List<CollectionItem> collectionItems = null;
/**
* List of ItemTag
*/
private List<ItemTag> itemTags = null;
/**
* <p>
* </p>
* @return itemId
*/
@Basic
@Id
@GeneratedValue
@Column(name = "item_id")
public Long getItemId() {
return itemId;
}
/**
* @param itemId new value for itemId
*/
public void setItemId(Long itemId) {
this.itemId = itemId;
}
/**
* <p>For when multiple customers/installations are supported in one database/install.
* </p>
* @return installationId
*/
@Basic
@Column(name = "installation_id")
public Integer getInstallationId() {
return installationId;
}
/**
* @param installationId new value for installationId
*/
public void setInstallationId(Integer installationId) {
this.installationId = installationId;
}
/**
* <p>
* </p>
* @return createdBy
*/
@Basic
@Column(name = "created_by")
public Integer getCreatedBy() {
return createdBy;
}
/**
* @param createdBy new value for createdBy
*/
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
/**
* <p>
* </p>
* @return creationDate
*/
@Basic
@Column(name = "creation_date")
public Timestamp getCreationDate() {
return creationDate;
}
/**
* @param creationDate new value for creationDate
*/
public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}
/**
* <p>
* </p>
* @return updateBy
*/
@Basic
@Column(name = "update_by")
public Integer getUpdateBy() {
return updateBy;
}
/**
* @param updateBy new value for updateBy
*/
public void setUpdateBy(Integer updateBy) {
this.updateBy = updateBy;
}
/**
* <p>
* </p>
* @return lastUpdate
*/
@Basic
@Column(name = "last_update")
public Timestamp getLastUpdate() {
return lastUpdate;
}
/**
* @param lastUpdate new value for lastUpdate
*/
public void setLastUpdate(Timestamp lastUpdate) {
this.lastUpdate = lastUpdate;
}
/**
* <p>
* </p>
* @return uri
*/
@Basic
@Column(name = "uri", length = 2048)
public String getUri() {
return uri;
}
/**
* @param uri new value for uri
*/
public void setUri(String uri) {
this.uri = uri;
}
/**
* <p>
* </p>
* @return identifier
*/
@Basic
@Column(name = "identifier", length = 36)
public String getIdentifier() {
return identifier;
}
/**
* @param identifier new value for identifier
*/
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
/**
* <p>
* </p>
* @return md5
*/
@Basic
@Column(name = "md5", length = 32)
public String getMd5() {
return md5;
}
/**
* @param md5 new value for md5
*/
public void setMd5(String md5) {
this.md5 = md5;
}
/**
* Get the list of CollectionItem
*/
@OneToMany(mappedBy="item")
public List<CollectionItem> getCollectionItems() {
return this.collectionItems;
}
/**
* Set the list of CollectionItem
*/
public void setCollectionItems(List<CollectionItem> collectionItems) {
this.collectionItems = collectionItems;
}
/**
* Get the list of ItemTag
*/
@OneToMany(mappedBy="item")
public List<ItemTag> getItemTags() {
return this.itemTags;
}
/**
* Set the list of ItemTag
*/
public void setItemTags(List<ItemTag> itemTags) {
this.itemTags = itemTags;
}
}
MetadataCollectionCode:
package org.maflt.flashlit.pojo;
import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;
/**
* <p>Pojo mapping TABLE metacollection</p>
* <p></p>
*
* <p>Generated at Mon Jun 15 20:37:53 MDT 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "metacollection", catalog = "tigger")
@SuppressWarnings("serial")
public class Metacollection implements Serializable {
/**
* Attribute collectionId.
*/
private Integer collectionId;
/**
* Attribute metadataSet
*/
private MetadataSet metadataSet;
/**
* Attribute collectionName.
*/
private String collectionName;
/**
* List of CollectionItem
*/
private List<CollectionItem> collectionItems = null;
/**
* <p>
* </p>
* @return collectionId
*/
@Basic
@Id
@GeneratedValue
@Column(name = "collection_id")
public Integer getCollectionId() {
return collectionId;
}
/**
* @param collectionId new value for collectionId
*/
public void setCollectionId(Integer collectionId) {
this.collectionId = collectionId;
}
/**
* get metadataSet
*/
@ManyToOne
@JoinColumn(name = "set_id")
public MetadataSet getMetadataSet() {
return this.metadataSet;
}
/**
* set metadataSet
*/
public void setMetadataSet(MetadataSet metadataSet) {
this.metadataSet = metadataSet;
}
/**
* <p>
* </p>
* @return collectionName
*/
@Basic
@Column(name = "collection_name", length = 45)
public String getCollectionName() {
return collectionName;
}
/**
* @param collectionName new value for collectionName
*/
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
/**
* Get the list of CollectionItem
*/
@OneToMany(mappedBy="metacollection")
public List<CollectionItem> getCollectionItems() {
return this.collectionItems;
}
/**
* Set the list of CollectionItem
*/
public void setCollectionItems(List<CollectionItem> collectionItems) {
this.collectionItems = collectionItems;
}
}