java - JPA 无法更新日期

标签 java database jakarta-ee jpa

在我的 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);
}

最佳答案

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

关于java - JPA 无法更新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14509991/

相关文章:

java - 使用 JXL (JExcel) 更新 Excel 工作表导致文件大小为 0 KB

mysql - 从 Laravel 5 表单中插入多个选定值

swift - Realm DB 是否优化了未更改的数据集上的过滤器计算?

java 函数返回比 Boolean 更多的状态类型

java - NoClassDefFoundError 在带有 java 1.4.2_07-b05 的 Tomcat 上具有长类名

php - 当 2 列必须匹配唯一行时在 MySQL 数据库中使用 REPLACE INTO

java - Java 的类型参数通配符到底是什么意思? Foo 和 Foo<?> 之间的真正区别是什么?

java - Web 应用程序上下文和服务上下文之间的双向依赖

java - 每次更改我的 Java 代码时,我可以阻止重新启动我的应用程序吗?

java.net.BindException : bind failed: EACCES (Permission denied) when trying to create DatagramSocket for UDP connection