在我的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);
}发布于 2019-02-03 12:28:46
如果您在尝试更新的列上具有updatable=false,则问题是相同的。
发布于 2013-01-25 05:04:38
首先,有一些关于事务管理的信息会很好,因为这对于JPA的行为非常重要。
以下是我认为会发生的事情:
您已经从EntityManager加载了Event-object,并且它可能已从会话中分离,因为在执行EntityManager#find操作时不会出现错误( EntityManager中不能同时有两个具有相同id的对象,否则会出现异常。)然后,更改刚刚加载的附加对象的状态,并在EntityManager中注册它(我认为对persist的调用是不正确的,在这里甚至是不必要的。)但是,您分离的对象(作为参数发送的对象)仍然是活动的,并且没有使用新值进行更新。因此,如果该对象在以后被重新附加并保存(可能是由您显式执行,也可能在事务处理完成时自动发生),您最终将覆盖在相关方法中设置的值。
您在这里提供的方法可能(也应该)看起来像这样:
@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的正确方式,通常您有一个Repository层,它处理基本的CRUD操作(查找、保存等),而逻辑层在该层之上,它处理对对象中的值的实际更改。
顺便说一句,你的equals和hashcode方法破坏了这些方法的约定,因为id并不总是被设置的(一个新创建的事件对象没有id),并且两个不同的,新创建的事件对象还没有被存储,因此没有id (除非你自己设置id,你确实不应该这样做),equals方法会认为它们是相同的。
https://stackoverflow.com/questions/14509991
复制相似问题