1.ProductType的JPA映射
View Code
1 @Entity
2 public class ProductType {
3 private int id;
4 private String name;
5 private String note;
6 private boolean visible = true;
7 private Set<ProductType> child;
8 private ProductType parent;
9
10 @Id @GeneratedValue(strategy = GenerationType.AUTO)
11 public int getId() {
12 return id;
13 }
14
15 public void setId(int id) {
16 this.id = id;
17 }
18 @Column(length=30,nullable=false)
19 public String getName() {
20 return name;
21 }
22
23 public void setName(String name) {
24 this.name = name;
25 }
26 @Column(length=100)
27 public String getNote() {
28 return note;
29 }
30
31 public void setNote(String note) {
32 this.note = note;
33 }
34 @Column(nullable=false)
35 public boolean isVisible() {
36 return visible;
37 }
38
39 public void setVisible(boolean visible) {
40 this.visible = visible;
41 }
42 @OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
43 public Set<ProductType> getChild() {
44 return child;
45 }
46
47 public void setChild(Set<ProductType> child) {
48 this.child = child;
49 }
50 @ManyToOne(cascade=CascadeType.REFRESH) @JoinColumn(name="parentid")
51 public ProductType getParent() {
52 return parent;
53 }
54
55 public void setParent(ProductType parent) {
56 this.parent = parent;
57 }
58
59 }
2.实现ProductType的无限级分类
View Code
1 @OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")
2 public Set<ProductType> getChild() {
3 return child;
4 }
5
6 public void setChild(Set<ProductType> child) {
7 this.child = child;
8 }
9 @ManyToOne(cascade=CascadeType.REFRESH) @JoinColumn(name="parentid")
10 public ProductType getParent() {
11 return parent;
12 }
13
14 public void setParent(ProductType parent) {
15 this.parent = parent;
16 }
3.实现ProductTypeService和ProductTypeServiceImpl
a.创建DAO接口,实现增删改查代码的服用
View Code
1 package cnblogs.xiaoqiu.service.base;
2
3 public interface DAO {
4
5 /**
6 * 保存实体
7 * @param entity
8 */
9 public void save(Object entity);
10 /**
11 * 删除实体
12 * @param entity
13 */
14 public <T> void delete(Class<T> classType,Object entityId);
15 /**
16 *
17 * @param entitys 要删除的实体的ID数组
18 */
19 public <T> void delete(Class<T> classType,Object[] entitysId);
20 /**
21 * 更新实体
22 * @param entity
23 */
24 public void update(Object entity);
25 /**
26 * 查找实体
27 * @param <T>
28 * @param classType 实体的类型
29 * @param id 实体的ID
30 * @return
31 */
32 public <T> T find(Class<T> classType,Object id);
33 }
b.创建DAOSupport抽象类,为以后类实现DAO接口提供方便
View Code
1 package cnblogs.xiaoqiu.service.base;
2
3 import javax.persistence.EntityManager;
4 import javax.persistence.PersistenceContext;
5
6 import org.springframework.stereotype.Service;
7 import org.springframework.transaction.annotation.Propagation;
8 import org.springframework.transaction.annotation.Transactional;
9
10 @Service @Transactional
11 public abstract class DAOSupport implements DAO {
12 @PersistenceContext protected EntityManager entityManager;
13
14 public <T> void delete(Class<T> classType,Object entityId) {
15 entityManager.remove(entityManager.getReference(classType, entityId));
16
17 }
18
19 public <T> void delete(Class<T> classType,Object[] entitysId) {
20 for(int i=0;i<entitysId.length;i++){
21 delete(classType, entitysId[i]);
22 }
23 }
24 @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
25 public <T> T find(Class<T> classType, Object id) {
26 return entityManager.find(classType, id);
27 }
28
29 public void save(Object entity) {
30 entityManager.persist(entity);
31 }
32
33 public void update(Object entity) {
34 entityManager.merge(entity);
35 }
36
37 }
c.创建ProductTypeService接口
View Code
1 package cnblogs.xiaoqiu.service.product;
2
3 import cnblogs.xiaoqiu.service.base.DAO;
4
5 public interface ProductTypeService extends DAO{
6
7 }
d.创建ProductTypeServiceImpl类
package cnblogs.xiaoqiu.service.product.impl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cnblogs.xiaoqiu.service.base.DAOSupport;
import cnblogs.xiaoqiu.service.product.ProductTypeService;
@Service @Transactional
public class ProductTypeServiceImpl extends DAOSupport implements ProductTypeService {
}
4.覆盖DAOSupport的delete方法
View Code
1 package cnblogs.xiaoqiu.service.product.impl;
2
3 import org.springframework.stereotype.Service;
4 import org.springframework.transaction.annotation.Transactional;
5 import cnblogs.xiaoqiu.service.base.DAOSupport;
6 import cnblogs.xiaoqiu.service.product.ProductTypeService;
7
8 @Service @Transactional
9 public class ProductTypeServiceImpl extends DAOSupport implements ProductTypeService {
10
11 @Override
12 public <T> void delete(Class<T> classType, Object entityId) {
13 String jpql = "update ProductType p set p.visible=?1";
14 entityManager.createQuery(jpql).setParameter(1, false).executeUpdate();
15 }
16
17 }
5.测试ProductTypeServiceImpl