休眠插入两行而不是一行

时间:2018-12-24 14:08:55

标签: hibernate jpa spring-data-jpa

package com.nec.message.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * The Class Message.
 */
@Entity
@Table(name = "message")
public class Message extends BaseBean implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 6179971808272872726L;

    /** The message id. */
    @Id
    @JsonProperty("id")
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE , generator="message_sequence")
    @SequenceGenerator(name = "message_sequence", sequenceName = "message_sequence", allocationSize = 1,initialValue=1001)
    private Long messageId;

    /** The message name. */
    @JsonProperty("message_name")
    @Column(name = "message_name",unique=true)
    private String messageName;

    /** The duration. */
    @JsonProperty("duration")
    @Column(name = "duration")
    private Integer duration;

    /** The chassis id. */
    @JsonProperty("chassis_id")
    @Column(name = "chassis_id")
    private Long chassisId;

    /** The sub windows. */
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "message_id", referencedColumnName = "id")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<MessageSubWindow> subWindows;

    /** The html url. */
    @JsonProperty("html_url")
    @Column(name = "html_url")
    private String htmlUrl;

    /** The snapshot url. */
    @JsonProperty("snapshot_url")
    @Column(name = "snapshot_url")
    private String snapshotUrl;

    /** The rule. */
    @JsonProperty("rule_id")
    private Long ruleId;

    /** The status. */
    @JsonProperty("status")
    @Column(name = "status")
    private String status;

    @JsonProperty("asset_id")
    @Column(name = "asset_id")
    private String assetId;

    @JsonProperty("skuCodes")
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "skucode_id", referencedColumnName = "id")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<SkuCode> skucodes;

    @JsonProperty("tags")
    @Column(name = "tags")
    private String tags;

    @JsonProperty("description")
    @Column(name = "description")
    private String description;

    @JsonProperty("isPrecreated")
    @Column(name = "isprecreated")
    private Boolean isPrecreated = Boolean.FALSE;

    /**
     * Instantiates a new message.
     */
    public Message() {
        super();

    }

    /**
     * Instantiates a new message.
     *
     * @param messageId
     *            the message id
     * @param messageName
     *            the message name
     * @param duration
     *            the duration
     * @param chassisId
     *            the chassis id
     * @param subWindows
     *            the sub windows
     * @param htmlUrl
     *            the html url
     * @param snapshotUrl
     *            the snapshot url
     * @param ruleId
     *            the rule id
     * @param status
     *            the status
     */
    public Message(Long messageId, String messageName, Integer duration, Long chassisId,
            List<MessageSubWindow> subWindows, String htmlUrl, String snapshotUrl, Long ruleId, String status) {
        super();
        this.messageId = messageId;
        this.messageName = messageName;
        this.duration = duration;
        this.chassisId = chassisId;
        this.subWindows = subWindows;

        this.htmlUrl = htmlUrl;
        this.snapshotUrl = snapshotUrl;
        this.ruleId = ruleId;
        this.status = status;

    }

    public Message(Long messageId, String messageName, Integer duration, Long chassisId,
            List<MessageSubWindow> subWindows, String htmlUrl, String snapshotUrl, Long ruleId, String status,
            Date createdDate, Date lastModifiedDate, Long retailerId, String folderId) {
        super(createdDate, lastModifiedDate, folderId, retailerId);
        this.messageId = messageId;
        this.messageName = messageName;
        this.duration = duration;
        this.chassisId = chassisId;
        this.subWindows = subWindows;

        this.htmlUrl = htmlUrl;
        this.snapshotUrl = snapshotUrl;
        this.ruleId = ruleId;
        this.status = status;
        this.retailerId = retailerId;

    }

    public static Message messageWithRetailerId(Long reatilerId, Message message) {
        return new Message(message.getMessageId(), message.getMessageName(), message.getDuration(),
                message.getChassisId(), message.getSubWindows(), message.getHtmlUrl(), message.getSnapshotUrl(),
                message.getRuleId(), message.getStatus(), message.getCreatedDate(), message.getLastModifiedDate(),
                reatilerId, message.getFolderId());

    }

    /**
     * Gets the rule id.
     *
     * @return the rule id
     */
    public Long getRuleId() {
        return ruleId;
    }

    /**
     * Sets the rule id.
     *
     * @param ruleId
     *            the new rule id
     */
    public void setRuleId(Long ruleId) {
        this.ruleId = ruleId;
    }

    /**
     * Gets the message id.
     *
     * @return the message id
     */
    public Long getMessageId() {
        return messageId;
    }

    /**
     * Gets the message name.
     *
     * @return the message name
     */
    public String getMessageName() {
        return messageName;
    }

    /**
     * Gets the duration.
     *
     * @return the duration
     */
    public Integer getDuration() {
        return duration;
    }

    /**
     * Gets the chassis id.
     *
     * @return the chassis id
     */
    public Long getChassisId() {
        return chassisId;
    }

    /**
     * Gets the sub windows.
     *
     * @return the sub windows
     */

    public List<MessageSubWindow> getSubWindows() {
        return subWindows;
    }

    public void setSubWindows(List<MessageSubWindow> subWindows) {
        this.subWindows = subWindows;
    }

    /**
     * Gets the html url.
     *
     * @return the html url
     */
    public String getHtmlUrl() {
        return htmlUrl;
    }

    /**
     * Gets the snapshot url.
     *
     * @return the snapshot url
     */
    public String getSnapshotUrl() {
        return snapshotUrl;
    }

    /**
     * Gets the status.
     *
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * Sets the message name.
     *
     * @param messageName
     *            the new message name
     */
    public void setMessageName(String messageName) {
        this.messageName = messageName;
    }

    /**
     * Sets the duration.
     *
     * @param duration
     *            the new duration
     */
    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    /**
     * Sets the chassis id.
     *
     * @param chassisId
     *            the new chassis id
     */
    public void setChassisId(Long chassisId) {
        this.chassisId = chassisId;
    }

    /**
     * Sets the html url.
     *
     * @param htmlUrl
     *            the new html url
     */
    public void setHtmlUrl(String htmlUrl) {
        this.htmlUrl = htmlUrl;
    }

    /**
     * Sets the snapshot url.
     *
     * @param snapshotUrl
     *            the new snapshot url
     */
    public void setSnapshotUrl(String snapshotUrl) {
        this.snapshotUrl = snapshotUrl;
    }

    /**
     * Sets the status.
     *
     * @param status
     *            the new status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * Sets the message id.
     *
     * @param messageId
     *            the new message id
     */
    public void setMessageId(Long messageId) {
        this.messageId = messageId;
    }

    /**
     * Gets the asset id.
     *
     * @return the asset id
     */
    public String getAssetId() {
        return assetId;
    }

    /**
     * Sets the asset id.
     *
     * @param assetId
     *            the new asset id
     */
    public void setAssetId(String assetId) {
        this.assetId = assetId;
    }

    /**
     * Gets the tags.
     *
     * @return the tags
     */
    public String getTags() {
        return tags;
    }

    /**
     * Sets the tags.
     *
     * @param tags
     *            the new tags
     */
    public void setTags(String tags) {
        this.tags = tags;
    }

    /**
     * Gets the description.
     *
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets the description.
     *
     * @param description
     *            the new description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Gets the skucodes.
     *
     * @return the skucodes
     */
    public List<SkuCode> getSkucodes() {
        return skucodes;
    }

    /**
     * Sets the skucodes.
     *
     * @param skucodes
     *            the new skucodes
     */
    public void setSkucodes(List<SkuCode> skucodes) {
        this.skucodes = skucodes;
    }

    public Boolean getIsPrecreated() {
        return isPrecreated;
    }

    public void setIsPrecreated(Boolean isPrecreated) {
        this.isPrecreated = isPrecreated;
    }

    /**
     * Instantiates a new message.
     *
     * @param messageId
     *            the message id
     * @param messageName
     *            the message name
     */
    public Message(Long messageId, String messageName) {
        super();
        this.messageId = messageId;
        this.messageName = messageName;
    }

},

将两行插入到msg_dist表中,但预期为一行 我对Hibernate的JPA实施有疑问。我使用spring-boot-starter-data-jpa和PostgreSql v9。 我有两个通过OneToOne单向连接的实体 如何解决问题 我使用saveorupdate方法将包含消息的MessageDist实体保存为字段,两者之间是一对一的关系,我运行调度程序以保存或更新MessageDist对象。 @Entity

@Table(name = "msg_dist")
public class MessageDist implements Serializable {

    private static final long serialVersionUID = 3739444446563047237L;

    @Id
    @JsonProperty("id")
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator ="message_dist_sequence")
    @SequenceGenerator(name = "message_dist_sequence", sequenceName = "message_dist_sequence")
    private Long id;


    @OneToOne(fetch=FetchType.EAGER)
    @JsonProperty("message_id")


    @JoinColumn(name = "message_id", referencedColumnName = "id")
    private Message messageId;

    @JsonProperty("player_id")
    @Column(name = "player_id")
    private String playerId;

    @JsonProperty("duration")
    @Column(name = "duration")
    private Integer duration;

    @JsonProperty("download_url")
    @Column(name = "download_url")
    private String downloadUrl;

    @JsonProperty("start_validity_date")
    @Column(name = "start_validity_date")
    private Long startValidityDate;

    @JsonProperty("end_validity_date")
    @Column(name = "end_validity_date")
    private Long endValidityDate;

    @JsonProperty("scheduler_id")
    @Column(name = "scheduler_id")
    private Long schedulerId;

    public Long getStartValidityDate() {
        return startValidityDate;
    }

    public void setStartValidityDate(Long startValidityDate) {
        this.startValidityDate = startValidityDate;
    }

    public Long getEndValidityDate() {
        return endValidityDate;
    }

    public void setEndValidityDate(Long endValidityDate) {
        this.endValidityDate = endValidityDate;
    }

    @JsonProperty("isActive")
    @Column(name = "isActive")
    private Boolean isActive;

    @JsonProperty("checksum")
    @Column(name = "checksum")
    private String checksum;

    @JsonProperty("status")
    @Column(name = "status")
    private String status;

    @JsonProperty("acknowledgetime")
    @Column(name = "acknowledgetime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date acknowledgeTime;

    @JsonProperty("cmsid")
    @Column(name = "cms_message_id")
    private String cmsMessageId;

    public Long getId() {
        return id;
    }

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

    public Message getMessageId() {
        return messageId;
    }

    public void setMessageId(Message messageId) {
        this.messageId = messageId;
    }

    public String getPlayerId() {
        return playerId;
    }

    public void setPlayerId(String playerId) {
        this.playerId = playerId;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    public String getChecksum() {
        return checksum;
    }

    public void setChecksum(String checksum) {
        this.checksum = checksum;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public Date getAcknowledgeTime() {
        return acknowledgeTime;
    }

    public void setAcknowledgeTime(Date acknowledgeTime) {
        this.acknowledgeTime = acknowledgeTime;
    }

    public String getCmsMessageId() {
        return cmsMessageId;
    }

    public void setCmsMessageId(String cmsMessageId) {
        this.cmsMessageId = cmsMessageId;
    }

    public Long getSchedulerId() {
        return schedulerId;
    }

    public void setSchedulerId(Long schedulerId) {
        this.schedulerId = schedulerId;
    }


},

    @Component
public class MessageDistrbutionScheduler extends AbstractController {

    @Autowired
    private MessageDistService messageDistService;

    @Autowired
    private IMessageSchedulerService messageSchedulerService;

    @Autowired
    private IChassisDao chassisDao;

    @Autowired
    private MessageRestClient messageRestClient;

    private static boolean outOfTime = true;

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageDistrbutionScheduler.class);

     @Scheduled(cron = "${cron.job.pos}")
    public CustomHttpResponseEntity scheduledTask() {

        HashMap<String, DataSource> dataSource = MultiTenantInitService.getDataSourceMap();
        Boolean msgSaved = false;
        MessageDist messageDist = new MessageDist();
        for (Map.Entry<String, DataSource> entry : dataSource.entrySet()) {
            TenantContext.setCurrentTenant(entry.getKey());
            LOGGER.info("Scheduler run for this tenant id:" + entry.getKey() + "time is" + new Date());
            Date currentDate = dateConverter(new Date());
            List<MessageScheduler> messageScheduleList = messageSchedulerService.fetchScheduleData(currentDate);
            LOGGER.info("Scheduler fetch data for current data from message schedule list:"
                    + TenantContext.getCurrentTenant() + "time is" + new Date());
            if (!messageScheduleList.isEmpty()) {
                messageScheduleList = messageSchedulerService.compareListTimeZone(messageScheduleList,
                        TenantContext.getCurrentTenant());
                LOGGER.info("Scheduler fetch data for current data from current tenant id:"
                        + TenantContext.getCurrentTenant() + "and compare with store time zone  and time is"
                        + new Date());
                LOGGER.info("message scheduler started data distributing" + TenantContext.getCurrentTenant());
                messageDist = messageDistService.saveDistributeMessage(messageScheduleList);
                LOGGER.info("message scheduler completed data distributing for tenant id:"
                        + TenantContext.getCurrentTenant());

                if (messageDist != null) {
                    msgSaved = true;
                    LOGGER.info("message schedule successfully" + TenantContext.getCurrentTenant());
                }
                LOGGER.info("message schedule successfully");
            }
        }
        if (msgSaved) {
            return buildSuccessResponse(ResponseMessage.MESSAGE_SCHEDULE_START, null, messageDist);
        } else {
            LOGGER.info(TenantContext.getCurrentTenant() + " this tenant not have any message for scheduling");
            return null;
        }

    }},
@Transactional
    public MessageDist saveDistributeMessage(List<MessageScheduler> messageScheduleList) {
        MessageDist response = null;
        for (int i = 0; i < messageScheduleList.size(); i++) {
            MessageScheduler messageScheduler = messageScheduleList.get(i);
            Message msg=messageDao.getByKey(messageScheduler.getMessageId().getMessageId());
            MessageDist dist = new MessageDist();
            dist.setChecksum(messageScheduler.getCheckSum());
            dist.setDownloadUrl(messageScheduler.getDownloadUrl());
            dist.setDuration(messageScheduler.getDuration());
            dist.setEndValidityDate(messageScheduler.getExpiryDate().getTime());
            Message message = new Message();
            message.setMessageId(messageScheduler.getMessageId().getMessageId());
            dist.setMessageId(message);
            dist.setPlayerId(messageScheduler.getPlayerId());
            //dist.setMessageName(msg.getMessageName());
            dist.setIsActive(true);
            dist.setSchedulerId(messageScheduler.getId());
            dist.setStartValidityDate(messageScheduler.getStartDate().getTime());
            dist.setStatus(AckStatusEnum.SCHEDULED.toString());
            LOGGER.info("Scheduler try to data for current data for scheduler id"+messageScheduler.getId() +"for current tenant id:"+TenantContext.getCurrentTenant() + "and time is" + new Date());
            response = messageDistDao.saveOrUpdate(dist);
            LOGGER.info("Scheduler distribute data for data for scheduler id"+messageScheduler.getId() +"for current tenant id:"+TenantContext.getCurrentTenant() + "and time is" + new Date() +"and count value is"+ i);
            messageSchedulerDao.updateScheduledmessagesStatus(messageScheduler.getId(),
                    AckStatusEnum.SCHEDULED.toString());
            messageSchedulerServiceImpl.messageAudit(messageScheduler.getMessageId().getMessageId(), messageScheduler.getId(), AckStatusEnum.SCHEDULED.toString(), COMMENT, messageScheduler.getPlayerId());

        }
        return response;
    }

2 个答案:

答案 0 :(得分:0)

为什么要在MessageDist内创建2个response对象distsaveDistributeMessage?我认为这可能是您问题的根源,

删除 MessageDist dist = new MessageDist();

改为response = new MessageDist(); 并将所有内容直接设置为response对象。

让我知道这是否有帮助。

答案 1 :(得分:0)

Scheduler运行了两次,所以这是在db中输入了两个条目的原因。现在它已修复。