为什么选择查询递增@Version

时间:2016-03-24 04:53:45

标签: java spring hibernate jpa hql

在执行以下语句

之后,类Application的版本会增加
Queue queue = queueDao.fetchQueueByApplicationId(application.getApplicationId());

它只是一个简单的提取查询,它不应该如何增加调用的版本。但是在上面的行执行之后,对于Application类

,版本意外地增加了

请有人帮忙 感谢。

Queue.java

@Entity
@Table(name = "queue")
@NamedQuery(name = "queueByApplicationId", query = "SELECT q from Queue q WHERE q.application.applicationId = ?")
public class Queue implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "queue_id", unique = true, nullable = false)
    private Long queueId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created", nullable = false, length = 19)
    private Date created;

    @Column(name = "created_by")
    private Long createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated", length = 19)
    private Date updated;

    @Column(name = "updated_by")
    private Long updatedBy;

    @Version
    @Column(name = "version", nullable = false)
    private Integer version;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "application_id")
    private Application application;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "queue", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    private List<QueueAssignedToRole> queueAssignedToRoles;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "queue", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    private List<QueueAssignedUser> queueAssignedUsers;

    getter setter ....

}

Application.java

    @Entity
    @Table(name = "application")
    public class Application implements java.io.Serializable {
        private static final long serialVersionUID = 1L;

        private Long applicationId;
        private Integer version;

      @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "application_id", unique = true, nullable = false)
    public Long getApplicationId() {
        return this.applicationId;
    }

    public void setApplicationId(Long applicationId) {
        this.applicationId = applicationId;
    }

    @Version
    @Column(name = "version", nullable = false)
    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

}

ApplicationQueueDaoImpl.java

@Repository
public class ApplicationQueueDaoImpl extends AbstractDao<Queue> {

    private static final Logger LOGGER = LogManager.getLogger(ApplicationQueueDaoImpl.class);

    @Override
    public Queue fetchQueueByApplicationId(Long applicationId) {
        LOGGER.debug("Fetching Queue information for applicationId {}", applicationId);
        List<Queue> queues = executeNamedQuery("queueByApplicationId", Queue.class, applicationId);
        return CollectionUtils.isEmpty(queues) ? null : queues.get(0);
    }

}

AbstractDao.java

protected <T> List<T> executeNamedQuery(String queryName, Class<T> resultType, Object... positionalParams) {
        TypedQuery<T> query = entityManager.createNamedQuery(queryName, resultType);
        DAOUtils.setPositionalParams(query, positionalParams);

        return query.getResultList();
    }

0 个答案:

没有答案