简体   繁体   English

无法使用JPA删除实体

[英]Can't remove entity with JPA

I'm trying to remove an entity from memory (at the moment I don't use DB) with JPA, when I use remove and then try to find the deleted entity it shows null , but when I use findAll method it retrieve all data (with removed entity)... 我试图从内存中删除一个实体(目前我不使用DB),当我使用remove然后尝试找到它显示为null的已删除实体时,但是当我使用findAll方法时它会检索所有数据(已删除实体)...

Profile.java Profile.java

@Entity
@Table(name = "profile")
public class Profile {

    @Id
    @GeneratedValue
    private Long id;

    private String nombre;

    private Boolean restrictedAccess;

    private Boolean canValidate;

    // private Set<AccessField> accessFields = new HashSet<AccessField>();

    // private Set<AccessEntity> accessEntities = new HashSet<AccessEntity>();
    @OneToMany(mappedBy = "profile", fetch = FetchType.EAGER)
    private Set<AccessMenu> menuSections = new HashSet<AccessMenu>();

    @OneToMany(mappedBy = "profile", fetch = FetchType.EAGER)
    private Set<User> users = new HashSet<User>();
[getters and setters]

ProfileRepository ProfileRepository

@Repository
@Transactional
public class ProfileRepository {
    @PersistenceContext
    private EntityManager entityManager;

    public Profile save(Profile p) {
        p = this.entityManager.merge(p);
        this.entityManager.flush();
        return p;
    }

    public void delete(Long id){
        Profile profile = this.entityManager.find(Profile.class, id);
        this.entityManager.remove(profile);
    }

    public List<Profile> findAll() {
        CriteriaQuery cq = this.entityManager.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Profile.class));
        return (List<Profile>) this.entityManager.createQuery(cq).getResultList();
    }

    public Profile findById(Long id){
        return this.entityManager.find(Profile.class, id);
    }
}

Controller method 控制器方法

@RequestMapping(value="profile/delete/{idProfile}", method = RequestMethod.GET)
    public String delete(@PathVariable String idProfile,RedirectAttributes ra, Model model){


        profileRepo.delete(Long.valueOf(idProfile));

        model.addAttribute("profiles", profileRepo.findAll());

        return "profile/list";
    }

if you are are trying to delete an entity by using Id in the controller, do it like profileRepo.deleteById(Long.valueOf(idProfile)); 如果您正在尝试使用控制器中的Id删除实体,请执行profileRepo.deleteById(Long.valueOf(idProfile)); this, not like this profileRepo.delete(profileRepo.findById(Long.valueOf(idProfile))); 这个,不像这个profileRepo.delete(profileRepo.findById(Long.valueOf(idProfile)));

Also use your repository functions like these, 还可以使用这些存储库函数,

public void deleteArtistById(Long artistId) {
    Artist artist = manager.find(Artist.class, artistId);
    if (artist != null) {
        manager.getTransaction().begin();
        manager.remove(artist);
        manager.getTransaction().commit();
    }
}


public void deleteArtist(Artist artist) {
    manager.getTransaction().begin();
    manager.remove(artist);
    manager.getTransaction().commit();
}

You can take a look at this link for more detail: http://kodejava.org/how-do-i-delete-entity-object-in-jpa/ 您可以查看此链接以获取更多详细信息: http//kodejava.org/how-do-i-delete-entity-object-in-jpa/

最后我找到了一个解决方案,问题是当我试图删除ProfileusersmenuSections都有相关数据,所以最后我把menuSectionscascade = CascadeType.REMOVEusers设置的profile属性设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM