Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 366465

Summary: .Query.setLockMode ( LockModeType ) does not throw IllegalStateException when the query is found not to be a Java Persistence query language SELECT query or a Criteria API query
Product: z_Archived Reporter: Stephen DiMilla <stephen.dimilla>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED INVALID QA Contact:
Severity: blocker    
Priority: P3 CC: lance.andersen, stephen.dimilla, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Stephen DiMilla CLA 2011-12-12 16:59:18 EST
Build Identifier: eclipselink-2.3.0.v20110604-r9504

Using entity:

@Entity
@Table(name="EMPLOYEE")
public class Employee implements java.io.Serializable {

    private int                  id;
    private String         firstName;
    private String         lastName;
    private float                 salary;


    public Employee() {
    }

    public Employee(int id, String firstName, String lastName, float salary)
    {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        this.salary = salary;
    }

   // ===========================================================
   // getters and setters for the state fields

    @Id
    @Column(name="ID")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Column(name="FIRSTNAME")
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name="LASTNAME")
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name="SALARY")
    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }

------------------
client code:

        try {
            Query q = getEntityManager().createQuery(
                    "UPDATE Employee e SET e.salary = e.salary * 10.0");
            q.setLockMode(LockModeType.PESSIMISTIC_READ);
            System.out.println("IllegalStateException should have been thrown");
        } catch (IllegalStateException e) {
            System.out.println("Got TransactionRequiredException as expected");
        } catch (Exception e) {
            System.out.println("Unexpected exception occurred", e);
        }

------------------
output:

Unexpected exception occurred
java.lang.IllegalArgumentException: A lock type can only be used with a select query (which allows the database to be locked where necessary).
 	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setLockMode(EJBQueryImpl.java:1039)
 	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setLockMode(EJBQueryImpl.java:81)

Reproducible: Always
Comment 1 Tom Ware CLA 2012-04-05 11:01:15 EDT
Updating target milestone.
Comment 2 Tom Ware CLA 2013-02-05 09:12:35 EST
My tests on the current build show the correct exception being thrown.
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:35:37 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink