最近做了个项目发现有mysql的内存泄露原来是domain层的PO定义错了,比如下面
下面的两个文件中Dishes用到了一对多的双向注释而且hashCode()方法用到了它所关联的DishesCategory类的hashCode,同时DishesCategory类也用到了Dishes的hashCode()方。这就是问题了因为在Hibernate取出或修改Dishes时会调用他的hashCode()方法但是这个方法用到了DishesCategory的hashCode方法这样就造成了死循环所以jdbc内存泄露了所以说以后我们重写hashCode方法时不要用它所关联的PO的hashCode方法,解决办法把其中一个调用对方的hashCode注释掉
Dishes
package com.hanghang.orderweb.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
/**
* 菜肴的PO
*
* @author yanghang
*
*/
@Entity
@Table(name = "dishes_inf")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Dishes implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2346807524241022864L;
@Id
@Column(name = "dishes_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "dishes_name", nullable = false)
private String name;
@Column(name = "dishes_price", nullable = false)
// 单价
private Double price;
// 数量
@Column(name = "dishes_num", nullable = false)
private int num;
@Lob
@Column(name = "dishes_pic", nullable = true)
// 图片
private byte[] pic;
@OneToMany(targetEntity = OrderDetails.class, mappedBy = "dishes")
// 关联起来就可以之间访问orders了
private Set<OrderDetails> orders = new HashSet<>();
@ManyToOne(targetEntity = DishesCategory.class)
@JoinColumn(name = "category_id", nullable = false)
// 外键
private DishesCategory category;
public Dishes() {
}
public Dishes(int id, String name, Double price, byte[] pic,
Set<OrderDetails> orders, DishesCategory category, int num) {
super();
this.id = id;
this.name = name;
this.price = price;
this.pic = pic;
this.orders = orders;
this.category = category;
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public byte[] getPic() {
return pic;
}
public void setPic(byte[] pic) {
this.pic = pic;
}
public Set<OrderDetails> getOrders() {
return orders;
}
public void setOrders(Set<OrderDetails> orders) {
this.orders = orders;
}
public DishesCategory getCategory() {
return category;
}
public void setCategory(DishesCategory category) {
this.category = category;
}
// 根据name,date,admin重写equals
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (getClass() == obj.getClass()
&& name.equals(((Dishes) obj).getName())
&& category.equals(((Dishes) obj).getCategory())) {
return true;
} else {
return false;
}
}
// 根据name、date、admin来重写hashCode()方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
// result = prime * result
// + ((category == null) ? 0 : category.hashCode());
return result;
}
}
package com.hanghang.orderweb.domain;
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
/**
* 订单详情 PO
*
* @author yanghang
*
*/
@Entity
@Table(name = "orderDetails_inf")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class OrderDetails implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2022765550155847999L;
@Id
@Column(name = "orderDetails_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "orderDetails_number", nullable = false)
// 数量
private int number;
@Column(name = "orderDetails_price", nullable = false)
//总的价格
private Double price;
@ManyToOne(targetEntity = Order.class)
@JoinColumn(name = "order_id", nullable = false)
private Order order;
@ManyToOne(targetEntity = Dishes.class)
@JoinColumn(name = "dishes_id", nullable = false)
private Dishes dishes;
public OrderDetails() {
}
public OrderDetails(int id, int number, Double price, Order order,
Dishes dishes) {
super();
this.id = id;
this.number = number;
this.price = price;
this.order = order;
this.dishes = dishes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Dishes getDishes() {
return dishes;
}
public void setDishes(Dishes dishes) {
this.dishes = dishes;
}
// 根据name,date,admin重写equals
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this == obj)
return true;
if (getClass() == obj.getClass()
&& dishes.equals(((OrderDetails) obj).getDishes())
&& order.equals(((OrderDetails) obj).getOrder())
) {
return true;
} else {
return false;
}
}
// 根据name、date、admin来重写hashCode()方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dishes == null) ? 0 : dishes.hashCode());
result = prime * result
+ ((order == null) ? 0 : order.hashCode());
return result;
}
}