ManyToOne, OneToMany Indexed Collection Complete Example

Here is the complete code for a ManyToOne,OneToMany Indexed Collection. It took way too long to put this together so I hope it will save many hours for others who need to solve the same problem.

package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.hibernate.annotations.IndexColumn;

@Entity
public class GameQueue {
@Id
@GeneratedValue
public Long id;

public Long getId() {
return id;
}

@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
@IndexColumn(name="position", base=1, nullable=false)
@JoinColumn(name="gameQueue_id", nullable=false)
protected List gameQueueItems = new ArrayList();

public List getGameQueueItems() {
return gameQueueItems;
}
public void setGameQueueItems(List gameQueueItems) {
this.gameQueueItems = gameQueueItems;
}

public void addGameQueueItem(GameQueueItem item) {
gameQueueItems.add(item);
item.setGameQueue(this);
}
}

package models;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class GameQueueItem {
@Id
@GeneratedValue
public Long id;

public Long getId() {
return id;
}
/** The position of this item record within the list of items belonging to the parent GameQueue */
@Column(name="position", insertable=false, updatable=false)
protected Integer position;

@ManyToOne
protected Product product;

@ManyToOne(optional=false)
@JoinColumn(name="gameQueue_id", insertable=false, updatable=false, nullable=false)
protected GameQueue gameQueue;

public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}

public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
public GameQueue getGameQueue() {
return gameQueue;
}
public void setGameQueue(GameQueue gameQueue) {
this.gameQueue = gameQueue;
}
}

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s