无法在java spring中删除对象列表

时间:2015-12-29 19:20:50

标签: java spring

我的代码是:

    List<Session> futureSessions = this.getFutureSession(group.getSessions());
    for (Session session: futureSessions) {
        Boolean exists = false;
        for (SessionDTO sessionDTO: groupDTO.getSessions()) {
            if (session.getId() == sessionDTO.getId()) {
                exists = true;
            }
        }

        if(false == exists) {
            // sessionService.delete(session);
            group.getSessions().remove(session);
        } else {
            exists = false;
        }
    }  Group groupUpdated = groupService.save(group);

这部分代码用于将对象(会话)移除到我的组对象中。我使用java spring进行角度和java之间的稀释。 我有这两个对象之间的关系,在保存我的组对象之前,如果我的会话groupServive.save(group)我的会话没有从数据库中删除,我会查找。 任何帮助都将非常感谢!

2 个答案:

答案 0 :(得分:0)

这是我的小组模特:

`@Entity @Table(name =&#34; t_group&#34;) public class Group实现Serializable {

private static final long serialVersionUID = 1L;

private Long id;
private String name;
private Boolean actif;
private Boolean deleted;
private Enterprise enterprise;
private String avatar;

private Date date_creation;
private Date date_update;
private Integer version;

private Set<User> users;
private List<Rule> rules;
private List<Session> sessions;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(nullable = false, unique = true)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Column(nullable = false)
public Boolean getActif() {
    return actif;
}

public void setActif(Boolean actif) {
    this.actif = actif;
}

@Column(nullable = false)
public Boolean getDeleted() {
    return deleted;
}

public void setDeleted(Boolean deleted) {
    this.deleted = deleted;
}

@ManyToOne
@JoinColumn(name = "t_enterprise", nullable = true)
public Enterprise getEnterprise() {
    return enterprise;
}

public void setEnterprise(Enterprise enterprise) {
    this.enterprise = enterprise;
}

@Lob
@Column(columnDefinition = "LONGTEXT")
public String getAvatar() {
    return avatar;
}

public void setAvatar(String avatar) {
    this.avatar = avatar;
}

public Date getDate_creation() {
    return date_creation;
}

public void setDate_creation(Date date_creation) {
    this.date_creation = date_creation;
}

public Date getDate_update() {
    return date_update;
}

public void setDate_update(Date date_update) {
    this.date_update = date_update;
}

@ManyToMany
@JoinTable(name = "t_user_group", joinColumns = { 
        @JoinColumn(name = "t_group", nullable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "t_user") })
public Set<User> getUsers() {
    return users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="group")
public List<Rule> getRules() {
    return rules;
}

public void setRules(List<Rule> rules) {
    this.rules = rules;
}

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="group")
public List<Session> getSessions() {
    return sessions;
}

public void setSessions(List<Session> sessions) {
    this.sessions = sessions;
}

@Version
@Column(nullable = false)
public Integer getVersion() {
    return version;
}

public void setVersion(Integer version) {
    this.version = version;
}

public static long getSerialversionuid() {
    return serialVersionUID;
}

@PreUpdate
private void setLastUpdate() {
    this.setDate_update(new Date());
}

@PrePersist
private void setFirstCreationDate() {
    this.setLastUpdate();
    if(null == this.getDate_creation()){
        this.setDate_creation(new Date());
    }
}

`

答案 1 :(得分:0)

好的,解决方案只是在我的sessionDAO中添加一个方法

`

@Override
public void delete(Session session) {
    // repository.delete(session);
    if (session.getId() > 0) {

        query = em.createNativeQuery("DELETE FROM `t_session` WHERE `id` = :t_session", Session.class);
        query.setParameter("t_session", session.getId());
        query.executeUpdate();
    }
}`