Tags

, ,


“Many-to-many” example

This is a many-to-many relationship table design, a STOCK table has more than one CATEGORY, and CATEGORY can belong to more than one STOCK, the relationship is linked with a third table called STOCK_CATEGORY.
many-to-many-relationship

 

Stock.java

public class Stock implements java.io.Serializable {
private Set<StockCategory> stockCategories = new HashSet<StockCategory>(0);

public Set<StockCategory> getStockCategories() {
return this.stockCategories;
}

public void setStockCategories(Set<StockCategory> stockCategories) {
this.stockCategories = stockCategories;
}

Category.java

public class Category implements java.io.Serializable {
   private Set<StockCategory> stockCategories = new HashSet<StockCategory>(0);
   ...
   public Set<StockCategory> getStockCategories() {
	return this.stockCategories;
   }

   public void setStockCategories(Set<StockCategory> stockCategories) {
	this.stockCategories = stockCategories;
   }

StockCategory.java

public class StockCategory implements java.io.Serializable {
   private Integer stockCategoryId;
   private Stock stock;
   private Category category;
   ...

Now implementing all this with Hibernate annotation

Stock.java
@Entity()
@Table(name = “stock”, catalog = “mkyong”, uniqueConstraints = {
@UniqueConstraint(columnNames = “STOCK_NAME”),
@UniqueConstraint(columnNames = “STOCK_CODE”) })
public class Stock implements java.io.Serializable {
private Set stockCategories =
new HashSet(0);

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = “STOCK_ID”, unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = “stock”)
public Set getStockCategories() {
return this.stockCategories;
}

public void setStockCategories(Set stockCategories) {
this.stockCategories = stockCategories;
}

Category.java


@Entity
@Table(name = “category”, catalog = “mkyong”)
public class Category implements java.io.Serializable {
private Set stockCategories =
new HashSet(0);

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = “CATEGORY_ID”, unique = true, nullable = false)
public Integer getCategoryId() {
return this.categoryId;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = “category”)
public Set getStockCategories() {
return this.stockCategories;
}

public void setStockCategories(Set stockCategories) {
this.stockCategories = stockCategories;
}

StockCategory.java

@Entity
@Table(name = “stock_category”, catalog = “mkyong”,
uniqueConstraints = @UniqueConstraint(columnNames = {
“STOCK_ID”, “CATEGORY_ID” }))
public class StockCategory implements java.io.Serializable {
private Integer stockCategoryId;
private Stock stock;
private Category category;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = “STOCK_CATEGORY_ID”, unique = true, nullable = false)
public Integer getStockCategoryId() {
return this.stockCategoryId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “STOCK_ID”, nullable = false)
public Stock getStock() {
return this.stock;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “CATEGORY_ID”, nullable = false)
public Category getCategory() {
return this.category;
}

Advertisements