我尝试过什么,当我从userService类调用delete()时,我无法删除用户实体。我得到一个异常java.lang.IllegalArgumentException:必须管理实体以调用remove:com.blackbox.genesis.entities.User @ 30168a,尝试合并分离并再次尝试删除。我显然做错了 - 尽管合并,但我看不清楚。其他一切正常 - 我可以毫无问题地创建和更新用户实体。
此致
我的实体类;
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = "EMAIL")})
public class User implements Serializable {
@Id
@Column(name="username", length=50)
private String username;
@OneToOne(cascade = {CascadeType.ALL})
private Password password;
private boolean enabled;
private int serial;
private String email;
@Version
private int version;
@ElementCollection(targetClass=Authority.class)
@CollectionTable(name="USER_AUTHORITY")
private List<Authority> authorities;
@OneToMany(mappedBy="user", fetch=FetchType.LAZY, cascade=CascadeType.ALL, ``orphanRemoval=true)
private Set<License> licenses;
private static final long serialVersionUID = 1L;
public User() {
super();
this.authorities = new ArrayList<Authority>();
}
.... getters/setters.
我的DAO课程;
@Repository
public class UserJpaController {
@PersistenceContext
EntityManager em;
protected static final Logger logger = Logger.getLogger("com.blackbox.genesisng.entities.UsersJpaController");
public void create(User user) throws PreexistingEntityException, Exception {
if (findUser(user.getUsername()) != null) {
throw new PreexistingEntityException("Users " + user + " already exists.");
}
em.persist(user);
em.flush();
}
public void edit(User user) throws NonexistentEntityException, Exception {
user = em.merge(user);
em.flush();
}
public void destroy(String id) throws NonexistentEntityException {
User user = em.find(User.class, id);
user = em.merge(user);
em.remove(user);
}
public List<User> findUserEntities() {
return findUserEntities(true, -1, -1);
}
public List<User> findUserEntities(int maxResults, int firstResult) {
return findUserEntities(false, maxResults, firstResult);
}
private List<User> findUserEntities(boolean all, int maxResults, int firstResult) {
Query q = em.createQuery("select object(o) from User as o");
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
}
public User findUser(String id) {
return em.find(User.class, id);
}
public int getUserCount() {
Query q = em.createQuery("select count(o) from User as o");
return ((Long) q.getSingleResult()).intValue();
}
public User findUserByEmail(String email) {
Query q = em.createQuery("select Object(o) from User as o where o.email = :email");
q.setParameter("email", email);
List list = q.getResultList();
if (list.size() == 0) {
return null;
}
return (User) list.get(0);
}
public boolean exists(String id) {
try {
em.getReference(User.class,id);
return true;
}
catch (EntityNotFoundException e) {
return false;
}
}
}
最后,我的服务类的相关部分
@Service
public class UserService {
@Autowired
UserJpaController dao;
@Autowired
LicenseJpaController licenseDao;
@Transactional
public void delete(UserDTO userDTO) {
if (exists(userDTO.getUserName())){
try {
dao.destroy(userDTO.getUserName());
} catch (NonexistentEntityException e) {
// ignore as the previous test should prevent this.
}
}
}
答案 0 :(得分:1)
很抱歉,但我是个白痴!我没有打电话给我认为我的服务类。修复了这一切,一切都按预期工作。再一次,抱歉的人。
此致
答案 1 :(得分:0)
删除
user = em.merge(user);
语句。我不确定它是否会导致探测,但不需要它,因为之前在语句中加载了用户。