1

The version for the class Application is getting incremented after the execution of the following statement

Queue queue = queueDao.fetchQueueByApplicationId(application.getApplicationId());

It is a simple fetch query only and it should not increment the version of the call any how. But after above line execution the version is getting incremented unexpectedly for Application class

Could someone please help Thanks.

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();
    }
Noorus Khan
  • 1,342
  • 3
  • 15
  • 33
  • 1
    How do you _know_ the version is being incremented by your query? Is there a trigger in the database that is incrementing the version? Could there be some other process incrementing the version? Have you stepped through the code to debug it? – Jim Garrison Mar 24 '16 at 05:00
  • @Jim Garrison while debugging, put the logger before and after the fetch query to capture version, came to know its incrementing here in this statement only – Noorus Khan Mar 24 '16 at 05:05
  • 1
    That doesn't answer his first two questions. Dump the SQL commands. – chrylis -cautiouslyoptimistic- Mar 24 '16 at 05:11
  • there is no trigger, which sql commands u r syng? – Noorus Khan Mar 24 '16 at 05:18
  • Look in the log of your chosen JPA provider for what SQL it is invoking. That should be step 1 in debugging dialogue with the database – Neil Stockton Mar 24 '16 at 08:33

0 Answers0