日历对象不会在数据库中更新,但在会话中更新 [英] Calendar object won't update in database but does update within session

查看:203
本文介绍了日历对象不会在数据库中更新,但在会话中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个实体bean中表示预约,它具有 startTime endTime 类型日历。这是使用JPA作为 TIMESTAMP 存储的。如果我编辑约会对象的开始/结束时间,则在会话期间对对象本身进行更新,但不会在数据库本身内更新。其他元素如预约的描述在数据库中成功更新。只有他的开始和结束时间不是。



实体类(这是间接更新在数据库中使用 EntityManager.merge()

  package mcknighte.entity; 

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax。 persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import static javax.persistence.TemporalType.TIMESTAMP;
import javax.validation.constraints.NotNull;
import mcknighte.common.Convertable;

/ **
*预约实体类,代表数据库中的约会
*和整个应用在
*
* @author Edward McKnight(UP608985)
* @see客户
* @see约会Facade
* @see AppointmentService
* @see预约控制器
* @since 2017
* @version 1.0
* /
@Entity
public class Appointment implements Serializable,Convertable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Temporal(TIMESTAMP)
private Calendar startTime;
@NotNull
@Temporal(TIMESTAMP)
private Calendar endTime;
@NotNull
private String description;
@NotNull
@ManyToOne(targetEntity = Client.class)
私人客户端创建者;
@NotNull
@OneToMany(targetEntity = Client.class)
私人列表<客户端>人;

/ **
*构造函数
* /
public Appointment(){
this.startTime = Calendar.getInstance();
this.endTime = Calendar.getInstance();
}

/ **
*获取约会人员
*
* @返回约会人员名单
* /
public List< Client> getAttendees(){
返回参加者;
}

/ **
*设定约会的参与者
*
* @param与会者有约会人员名单
* /
public void setAttendees(List< Client>与会者){
this.attendees =与会者;
}

/ **
*获取约会的创建者
*
* @返回约会的创建者
* /
public Client getCreator(){
return creator;
}

/ **
*设置约会的创建者
*
* @param创建者约会的创建者
* /
public void setCreator(Client creator){
this.creator = creator;
}

/ **
*获取约会的描述
*
* @返回约会的描述
* /
public String getDescription(){
return description;
}

/ **
*设置约会的描述
*
* @param描述约会的描述
* /
public void setDescription(String description){
this.description = description;
}

/ **
*获取约会的结束时间
*
* @返回预约的结束时间
* /
public Calendar getEndTime(){
return endTime;
}

/ **
*设置约会的结束时间
*
* @param结束约会的结束时间
* /
public void setEndTime(Calendar end){
this.endTime = end;
}

/ **
*获取约会的开始时间
*
* @返回预约的开始时间
* /
public Calendar getStartTime(){
return startTime;
}

/ **
*设置约会的开始时间
*
* @param启动约会的开始时间
* /
public void setStartTime(Calendar start){
this.startTime = start;
}

/ **
*获取约会的ID
*
* @返回约会的ID
* /
@Override
public Long getId(){
return id;
}

/ **
*设置约会的ID
*
* @param id约会的ID
* /
public void setId(Long id){
this.id = id;
}

/ **
*哈希码
*
* @return int
* /
@Override
public int hashCode(){
int hash = 0;
hash + =(id!= null?id.hashCode():0);
返回哈希;
}

/ **
*等于
*
* @param对象要与
* @return进行比较的对象无论是否这等于被比较的对象
* /
@Override
public boolean equals(Object object){
if(!(object instanceof Appointment)){
return假;
}
预约其他=(约会)对象;
return!((this.id == null&& other.id!= null)||(this.id!= null&!this.id.equals(other.id)) );
}

/ **
*将此对象表示为字符串
*
* @返回此对象的字符串表示
* /
@Override
public String toString(){
returnmcknighte.entity.Appointment [id =+ id +];
}

}


解决方案

在找到另一个StackOverflow问题并阅读之后,问题已经解决 Oracle文档



所有需要完成的工作是为每个 @Mutable 注释>日历属性,作为日期日历默认情况下是不可变的。


I'm representing Appointments in an entity bean which have a startTime and endTime of type Calendar. This is stored using JPA as TIMESTAMP. If I edit the Appointment object's start / end time it updates on the object itself during the session but it's not updated within the database itself. Other elements such as the Appointment's description successfully update in the database.. It's only he start and end time which are not.

Entity class (this is indirectly updated in the database with EntityManager.merge():

package mcknighte.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import static javax.persistence.TemporalType.TIMESTAMP;
import javax.validation.constraints.NotNull;
import mcknighte.common.Convertable;

/**
 * Appointment entity class, to represent an Appointment within the database
 * and throughout the application
 *
 * @author Edward McKnight (UP608985)
 * @see Client
 * @see AppointmentFacade
 * @see AppointmentService
 * @see AppointmentController
 * @since 2017
 * @version 1.0
 */
@Entity
public class Appointment implements Serializable, Convertable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotNull
    @Temporal(TIMESTAMP)
    private Calendar startTime;
    @NotNull
    @Temporal(TIMESTAMP)
    private Calendar endTime;
    @NotNull
    private String description;
    @NotNull
    @ManyToOne(targetEntity = Client.class)
    private Client creator;
    @NotNull
    @OneToMany(targetEntity = Client.class)
    private List<Client> attendees;

    /**
     * Constructor
     */
    public Appointment() {
        this.startTime = Calendar.getInstance();
        this.endTime = Calendar.getInstance();
    }

    /**
     * Get the attendees for the appointment
     *
     * @return a list of attendees for the appointment
     */
    public List<Client> getAttendees() {
        return attendees;
    }

    /**
     * Set the attendees for the appointment
     *
     * @param attendees a list of attendees for the appointment
     */
    public void setAttendees(List<Client> attendees) {
        this.attendees = attendees;
    }

    /**
     * Get the creator of the appointment
     *
     * @return the creator of the appointment
     */
    public Client getCreator() {
        return creator;
    }

    /**
     * Set the creator of the appointment
     *
     * @param creator the creator of the appointment
     */
    public void setCreator(Client creator) {
        this.creator = creator;
    }

    /**
     * Get the description for the appointment
     *
     * @return the description for the appointment
     */
    public String getDescription() {
        return description;
    }

    /**
     * Set the description for the appointment
     *
     * @param description the description for the appointment
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Get the end time of the appointment
     *
     * @return the end time of the appointment
     */
    public Calendar getEndTime() {
        return endTime;
    }

    /**
     * Set the end time of the appointment
     *
     * @param end the end time of the appointment
     */
    public void setEndTime(Calendar end) {
        this.endTime = end;
    }

    /**
     * Get the start time of the appointment
     *
     * @return the start time of the appointment
     */
    public Calendar getStartTime() {
        return startTime;
    }

    /**
     * Set the start time of the appointment
     *
     * @param start the start time of the appointment
     */
    public void setStartTime(Calendar start) {
        this.startTime = start;
    }

    /**
     * Get the ID for the appointment
     *
     * @return the ID for the appointment
     */
    @Override
    public Long getId() {
        return id;
    }

    /**
     * Set the ID for the appointment
     *
     * @param id the ID for the appointment
     */
    public void setId(Long id) {
        this.id = id;
    }

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

    /**
     * Equals
     *
     * @param object the object to compare to
     * @return whether or not this equals the object being compared to
     */
    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Appointment)) {
            return false;
        }
        Appointment other = (Appointment) object;
        return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
    }

    /**
     * Represent this object as a string
     * 
     * @return a string representation of this object
     */
    @Override
    public String toString() {
        return "mcknighte.entity.Appointment[ id=" + id + " ]";
    }

}

解决方案

The issue has been resolved after finding another StackOverflow question and reading the Oracle documentation.

All that needed to be done was to add an @Mutable annotation to each of the Calendar properties, as Date and Calendar are immutable by default.

这篇关于日历对象不会在数据库中更新,但在会话中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆