0

在我的 JavaEE 应用程序中,我使用 JPA 来存储我的数据。但是我在更新存储在数据库中的日期时遇到了问题。当我想在我的应用程序中更改它时(以及当我合并时),所有字段都在更新,除了日期的字段... $ 你能帮帮我吗?

这是我的实体:

package persistence
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Event implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "NAME")
private String name;
@Column(name = "PLACE")
private String place;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "BEGINDATE")
private Date beginDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "ENDDATE")
private Date endDate;
@Column(name = "VISIBILITY")
private int visibility;
@Column(name = "DESCRIPTION")
private String description;

@OneToMany(mappedBy="relatedEvent")
private List<CustomizeEvent> customizedEvents;

@OneToOne(cascade = CascadeType.PERSIST,optional=false)
private Periodicity periodicity;

@ManyToOne(optional=false)
private UserAgenda eventOwner;

@ManyToMany
@JoinTable(name="EVENTS_BELONG_AGENDAS",joinColumns= @JoinColumn(name="EVENT_ID",referencedColumnName="ID"),inverseJoinColumns=@JoinColumn(name="AGENDA_ID",referencedColumnName="ID"))
private List<Agenda> belongToAgendas;

@ManyToMany
@JoinTable(name="EVENTS_GUESTS_AGENDAS",joinColumns= @JoinColumn(name="EVENT_ID",referencedColumnName="ID"),inverseJoinColumns=@JoinColumn(name="AGENDA_ID",referencedColumnName="ID"))    
private List<Agenda> guestToAgendas;

public Event() {
}

public Event(String name, String place, Date beginDate, Date endDate, int visibility, String description, Periodicity periodicity, UserAgenda eventOwner, Agenda agenda) {
    this.name = name;
    this.place = place;
    this.beginDate = beginDate;
    this.endDate = endDate;
    this.visibility = visibility;
    this.description = description;
    this.periodicity = periodicity;
    this.eventOwner = eventOwner;
    this.belongToAgendas = new ArrayList<Agenda>();
    this.belongToAgendas.add(agenda);
    this.customizedEvents = new ArrayList<CustomizeEvent>();
    this.guestToAgendas = new ArrayList<Agenda>();
}

public Event(String name, String place, Date beginDate, Date endDate, int visibility, String description, Periodicity periodicity, UserAgenda eventOwner, Agenda agenda,List<Agenda> guests) {
    this.name = name;
    this.place = place;
    this.beginDate = beginDate;
    this.endDate = endDate;
    this.visibility = visibility;
    this.description = description;
    this.periodicity = periodicity;
    this.eventOwner = eventOwner;
    this.belongToAgendas = new ArrayList<Agenda>();
    this.belongToAgendas.add(agenda);
    this.customizedEvents = new ArrayList<CustomizeEvent>();
    this.guestToAgendas = guests;
}


public Long getId() {
    return id;
}

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

public String getName() {
    return name;
}

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

public String getPlace() {
    return place;
}

public void setPlace(String place) {
    this.place = place;
}

public Date getBeginDate() {
    return beginDate;
}

public void setBeginDate(Date beginDate) {
    this.beginDate = beginDate;
}

public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public int getVisibility() {
    return visibility;
}

public void setVisibility(int visibility) {
    this.visibility = visibility;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public List<CustomizeEvent> getCustomizedEvents() {
    return customizedEvents;
}

public void setCustomizedEvents(List<CustomizeEvent> customizedEvents) {
    this.customizedEvents = customizedEvents;
}

public Periodicity getPeriodicity() {
    return periodicity;
}

public void setPeriodicity(Periodicity periodicity) {
    this.periodicity = periodicity;
}

public UserAgenda getEventOwner() {
    return eventOwner;
}

public void setEventOwner(UserAgenda eventOwner) {
    this.eventOwner = eventOwner;
}

public List<Agenda> getBelongToAgendas() {
    return belongToAgendas;
}

public void setBelongToAgendas(List<Agenda> belongToAgendas) {
    this.belongToAgendas = belongToAgendas;
}

public List<Agenda> getGuestToAgendas() {
    return guestToAgendas;
}

public void setGuestToAgendas(List<Agenda> guestToAgendas) {
    this.guestToAgendas = guestToAgendas;
}





@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Event)) {
        return false;
    }
    Event other = (Event) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "persistence.Event[ id=" + id + " ]";
}

}

这是我在托管 bean 中调用的函数:

public void setNewEndDate(Event event, Date endDate) {
    Event e = em.find(Event.class, event.getId());
    e.setEndDate(endDate);
    e.setDescription("New description");
    em.persist(e);
}
4

2 回答 2

1

updatable=false如果您在尝试更新的列上有问题,则问题是相同的。

于 2019-02-03T04:28:45.627 回答
0

首先,如果有一些关于事务管理的信息会很好,因为它对于 JPA 的行为方式非常重要。

这是我认为会发生的事情:

您已经从 EntityManager 加载了 Event-object,它可能已与会话分离,因为在执行 EntityManager#find 时不会出现错误(在 EntityManager 中不能有 2 个具有相同 id 的对象一次,你会得到一个异常..)那么,你改变刚刚加载的附加对象的状态,并将它注册到 EntityManager 中(我不认为持久化的调用是正确的,甚至在这里没有必要..) 但是,您的分离对象(作为参数发送的对象)仍然存在,并且尚未使用新值更新。因此,如果此对象在稍后被重新附加并保存(它可能由您显式执行,或者它可能在事务完成时自动发生),您最终将覆盖相关方法中设置的值。

您在此处提供的方法可能(并且应该)如下所示,如果有的话:

@Transactional
public void setNewDate(final Integer eventId, final Date endDate() {
    final Event e = em.find(Event.class, eventId);
    e.setEndDate(endDate);
    //no need to explicitly save here, the changes will be saved when the transcation ends,
    //and the transaction will only last this method, as indicated by the @Transactional annotation.
}

但是,这仍然不是使用 JPA 的正确方法,通常你有一个存储库层,它处理基本的 CRUD 操作(查找、保存等),而在它之上的一层是逻辑层,它处理实际的更改对象中的值。

顺便说一句,您的 equals 和 hashcode 方法违反了这些方法的约定,因为 id 并不总是设置(新创建的尚未持久化的事件对象将没有 id),并且两个不同的新创建的事件对象没有' 还没有被存储,因此没有 id(除非你自己设置了 id,你真的不应该这样做),将被 equals 方法认为是相同的。

于 2013-01-24T21:04:37.783 回答