巴巴运动网 17 无限级产品分类的双向一对多JPA映射
来源:互联网 发布:淘宝懒人装修 编辑:程序博客网 时间:2024/05/01 15:44
首先:结合 巴巴运动网 16,
结合 巴巴运动网 16节 增添
在 ProductType.java类中,增添如下属性:
/** 子类别 **/
private Set<ProductType> childtypes = new HashSet<ProductType>();
/** 所属 父类 **/
private ProductType parent;
// optional=true 可以选择吗 ? 这里指的是:可以没有父类。
@ManyToOne(cascade = CascadeType.REFRESH, optional = true)
@JoinColumn(name = "parentid") // 此为 外键。
public ProductType getParent() {
return parent;
}
public void setParent(ProductType parent) {
this.parent = parent;
}
// 1,cascade为级联。2,JPA规定 : 一的这一端为 关系被维护端。mappedBy="" 其值为 维护端的 标识。也就是
// 用parent负责关系的维护。
@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")
public Set<ProductType> getChildtypes() {
return childtypes;
}
public void setChildtypes(Set<ProductType> childtypes) {
this.childtypes = childtypes;
}
package com.itcast.bean;import java.io.Serializable;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;@Entitypublic class ProductType implements Serializable{/** 类别 id **/private Integer typeid;/** 类别名称 **/private String name;/** 搜索google 页面存放的 内容 **/private String note;/** 备注 **/private boolean visible = true;/** 子类别 **/private Set<ProductType> childtypes = new HashSet<ProductType>();/** 所属 父类 **/private ProductType parent;// optional=true 可以选择吗 ? 这里指的是:可以没有父类。@ManyToOne(cascade = CascadeType.REFRESH, optional = true)@JoinColumn(name = "parentid") // 此为 外键。public ProductType getParent() {return parent;}public void setParent(ProductType parent) {this.parent = parent;}// 1,cascade为级联。2,JPA规定 : 一的这一端为 关系被维护端。mappedBy="" 其值为 维护端的 标识。也就是// 用parent负责关系的维护。@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")public Set<ProductType> getChildtypes() {return childtypes;}public void setChildtypes(Set<ProductType> childtypes) {this.childtypes = childtypes;}@Id@GeneratedValue(strategy = GenerationType.AUTO)public Integer getTypeid() {return typeid;}public void setTypeid(Integer typeid) {this.typeid = typeid;}@Column(length = 36, nullable = false)public String getName() {return name;}public void setName(String name) {this.name = name;}@Column(length = 200)public String getNote() {return note;}public void setNote(String note) {this.note = note;}@Column(nullable = false)public boolean getVisible() {return visible;}public void setVisible(boolean visible) {this.visible = visible;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result+ ((childtypes == null) ? 0 : childtypes.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((note == null) ? 0 : note.hashCode());result = prime * result + ((parent == null) ? 0 : parent.hashCode());result = prime * result + ((typeid == null) ? 0 : typeid.hashCode());result = prime * result + (visible ? 1231 : 1237);return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;ProductType other = (ProductType) obj;if (childtypes == null) {if (other.childtypes != null)return false;} else if (!childtypes.equals(other.childtypes))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (note == null) {if (other.note != null)return false;} else if (!note.equals(other.note))return false;if (parent == null) {if (other.parent != null)return false;} else if (!parent.equals(other.parent))return false;if (typeid == null) {if (other.typeid != null)return false;} else if (!typeid.equals(other.typeid))return false;if (visible != other.visible)return false;return true;}}
1,说明:因为EJB3.0是分布式的,要通过网络传递,所以这里要序列化。。
2,如果:
// 1,cascade为级联。2,JPA规定 : 一的这一端为 关系被维护端。mappedBy="" 其值为 维护端的 标识。也就是
// 用parent负责关系的维护。
@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")
这句话,写到 set()方法上去了,回报如下异常:这里都是写在 get()方法上。
Caused by: org.hibernate.MappingException: Could not determine type
for: java.util.Set, at table: ProductType, for columns:
[org.hibernate.mapping.Column(childtypes)]
at org.hibernate.mapping.SimpleValue.getType
(SimpleValue.java:292)
at org.hibernate.mapping.SimpleValue.isValid
(SimpleValue.java:276)
at org.hibernate.mapping.Property.isValid(Property.java:207)
at org.hibernate.mapping.PersistentClass.validate
(PersistentClass.java:458)
at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
at org.hibernate.cfg.Configuration.validate
(Configuration.java:1135)
at org.hibernate.cfg.Configuration.buildSessionFactory
(Configuration.java:1320)
at
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory
(AnnotationConfiguration.java:867)
at
org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory
(Ejb3Configuration.java:669)
... 61 more
3,@JoinColumn(name = "parentid") // 此为 外键。
测试类:
package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itcast.bean.ProductType;import com.itcast.service.product.ProductService;public class ProductTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Testpublic void runtest() {ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");ProductService productService = (ProductService) cxt.getBean("productServiceBean");ProductType type = new ProductType();type.setName("瑜伽用品");type.setNote("很好,不错");productService.save(type);}}
结果成功:
- 巴巴运动网 17 无限级产品分类的双向一对多JPA映射
- 无限级的双向一对多JPA映射(产品分类)
- 巴巴运动网 16 (产品分类实体对象基本属性的JPA映射)
- 巴巴运动网学习笔记三之产品分类实体对象基本属性的JPA映射,用泛型技术对产品分类的业务管理Bean抽象,单元测试产品分类的业务管理Bean.,重载业务管理Bean的删除方法,
- JPA学习笔记-映射双向一对多的关联关系
- JPA映射关联关系-双向一对多
- 巴巴运动网-用泛型技术对产品分类的业务管理Bean抽象
- 【j2ee spring】36、巴巴运动网的产品品牌图片
- 【j2ee spring】41、巴巴运动网的产品查询
- 巴巴运动网JPA项目错误
- 巴巴运动网 (18--20) 用泛型技术对产品分类的业务管理Bean抽象,测试,重载
- JPA---双向一对多
- JPA一对多的双向关联
- mybatis中一对多的关系映射。比如产品分类表对应多个产品明细表
- jpa的单项一对多,双向一对多
- 双向一对多映射
- 双向一对多映射
- JPA的一对多映射(单向)
- 声波支付,超越短信/条码/磁条/NFC等支付方式
- 增量式文件存储
- 7_1_天天向上_FPGA_1
- 微软 MS Publisher另存为PDF时彩色文字变为灰色
- gridcontrol
- 巴巴运动网 17 无限级产品分类的双向一对多JPA映射
- 虚函数在基类和子类中都存在时,调用情况分析
- 什么叫回调函数
- windows 下利用 winrar 和 计划任务实现文件的自动备份
- 用python将jpg文件转为eps格式
- VMware 网络结构与编程
- VC中执行存储过程
- Apache Common HttpClient使用之七种武器
- 编写网页自动登录器、网页自动注册器、广告模拟点击器的一点思路