我正在尝试使用hibernate和注释来实现many2many关系。
股票是主表
类别是主表
stock_category维护股票和类别之间的关系。这里有一只股票可以属于多个类别,反之亦然。
这是代码。
股票类
package com.sample.hibernate.many2many.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "stock", uniqueConstraints = {
@UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {
private Integer stockId;
private String stockCode;
private String stockName;
private Set<Category> categories = new HashSet<Category>(0);
public Stock() {
}
public Stock(String stockCode, String stockName) {
this.stockCode = stockCode;
this.stockName = stockName;
}
public Stock(String stockCode, String stockName, Set<Category> categories) {
this.stockCode = stockCode;
this.stockName = stockName;
this.categories = categories;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
public String getStockCode() {
return this.stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
public String getStockName() {
return this.stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "stock_category", joinColumns = {
@JoinColumn(name = "STOCK_ID", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "CATEGORY_ID",
nullable = false, updatable = false) })
public Set<Category> getCategories() {
return this.categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
}
}
类别类
package com.sample.hibernate.many2many.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "category")
public class Category implements java.io.Serializable {
private Integer categoryId;
private String name;
private String desc;
private Set<Stock> stocks = new HashSet<Stock>(0);
public Category() {
}
public Category(String name, String desc) {
this.name = name;
this.desc = desc;
}
public Category(String name, String desc, Set<Stock> stocks) {
this.name = name;
this.desc = desc;
this.stocks = stocks;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CATEGORY_ID", unique = true, nullable = false)
public Integer getCategoryId() {
return this.categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
@Column(name = "NAME", nullable = false, length = 10)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "[DESC]", nullable = false)
public String getDesc() {
return this.desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories")
public Set<Stock> getStocks() {
return this.stocks;
}
public void setStocks(Set<Stock> stocks) {
this.stocks = stocks;
}
客户端
public class App {
public static void main(String[] args) {
System.out.println("Hibernate many to many (Annotation)");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Stock stock = new Stock();
stock.setStockCode("7052");
stock.setStockName("PADINI");
Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
Category category2 = new Category("INVESTMENT", "INVESTMENT COMPANY");
Set<Category> categories = new HashSet<Category>();
categories.add(category1);
categories.add(category2);
Set<Category> categories1 = new HashSet<Category>();
categories1.add(category1);
categories1.add(category2);
stock.setCategories(categories);
session.save(stock);
Stock stock2 = (Stock) session.get(Stock.class, new Integer(1));
System.out.println(stock2);
session.delete(stock2);
stock2 = (Stock) session.get(Stock.class, new Integer(1));
System.out.println("after del "+stock2);
session.getTransaction().commit();
session.close();
System.out.println("Done");
}
}
当我执行上述客户端时,我在控制台中看到以下内容。
Hibernate: insert into stock (STOCK_CODE, STOCK_NAME) values (?, ?)
Hibernate: insert into category (`DESC`, NAME) values (?, ?)
Hibernate: insert into category (`DESC`, NAME) values (?, ?)
com.sample.hibernate.many2many.model.Stock@84910e8
after del null
Hibernate: delete from category where CATEGORY_ID=?
Hibernate: delete from category where CATEGORY_ID=?
Hibernate: delete from stock where STOCK_ID=?
Done
为什么hibernate也会从类别中删除?我做错了什么。
我看到的其他事情是打印完成后程序没有中止。我会做的事情很奇怪。
提前致谢