我正在创建一个Web应用程序,我有一个小问题。 我已经google了很多,但找不到合适的答案。
我想使用实体管理器.remove方法从数据库中删除实体。 奇怪的是,除了delete方法之外,其他所有方法都有效。
提前致谢。
我的一个模特:
package nl.****.deploy.****.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "STAGES", uniqueConstraints = @UniqueConstraint(columnNames = {"NAME", "APPLICATION_ID"}) )
@SequenceGenerator(name="STAGES_SEQ", initialValue=1, allocationSize=100)
public class Stage extends DomainObject {
private static final long serialVersionUID = ******;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STAGES_SEQ")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "SORT_KEY", nullable = false)
private Integer sortKey;
@ManyToOne
@JoinColumn(name = "APPLICATION_ID", nullable = false)
@NotNull
private ApplicationInfo application;
@OrderBy("sortKey ASC")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "stage")
private Set<Environment> environments;
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ApplicationInfo getApplication() {
return application;
}
public void setApplication(ApplicationInfo application) {
this.application = application;
}
public Set<Environment> getEnvironments() {
return environments;
}
public void setEnvironments(Set<Environment> environments) {
this.environments = environments;
}
@Override
public int compareTo(DomainObject obj) {
if (obj instanceof Stage) {
return this.sortKey.compareTo(((Stage) obj).getSortKey());
} else {
return super.compareTo(obj);
}
}
public Integer getSortKey() {
return sortKey;
}
public void setSortKey(Integer sortKey) {
this.sortKey = sortKey;
}
}
服务:
package nl.****.deploy.****.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import nl.****.deploy.****.dao.StageDao;
import nl.****.deploy.****.model.Stage;
import nl.****.deploy.****.service.StageService;
@Service
public class StageServiceImpl implements StageService {
@Autowired
private StageDao StageDao;
@Override
@Transactional
public Stage load(Long stageId) {
return StageDao.find(stageId);
}
@Override
@Transactional
public void saveStage(Stage stage) {
StageDao.save(stage);
}
@Override
public void delete(Stage stage) {
StageDao.delete(stage);
}
}
我的baseDao,我从其他所有人那里打来的话
package nl.****.deploy.****.dao.impl;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import nl.****.deploy.****.dao.BaseDao;
import nl.****.deploy.****.model.DomainObject;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
public abstract class BaseDaoImpl<T extends DomainObject> implements BaseDao<T> {
@PersistenceContext
protected EntityManager entityManager;
private Class<T> domainObjectClass;
public EntityManager getEntityManager() {
return entityManager;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public BaseDaoImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
domainObjectClass = (Class) pt.getActualTypeArguments()[0];
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public T save(T t) {
if (t.getId() == null) {
getEntityManager().persist(t);
} else {
t = getEntityManager().merge(t);
}
return t;
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void delete(T t) {
if (!getEntityManager().contains(t)) {
t = getEntityManager().merge(t);
}
getEntityManager().remove(t);
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
@Override
public T find(Object id) {
return getEntityManager().find(getDomainObjectClass(), id);
}
@Override
public List<T> findAll() {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> cq = builder.createQuery(getDomainObjectClass());
Root<T> root = cq.from(getDomainObjectClass());
cq.select(root);
return entityManager.createQuery(cq).getResultList();
}
protected Class<T> getDomainObjectClass() {
return domainObjectClass;
}
}