Hello,
I perform an update against a Table Entity with setStringField("Updated"), that works well.
Then I try to use a Named Query from a separate class to get the updated value, but it shows the original value from the database.
It's only a problem with a Named Query. A similar Query in a Native Query works well.
Native Query:
Query queryDB = entityManager.createNativeQuery("select /* xxx.frameworks.test.data.ReadOnlyTestView */ " +
"et.string_field " +
"from entity_test1 et " +
"where et.test_id1 = ? ");
queryDB.setParameter(1, "1001");
System.out.println("Test Native DB Result "+queryDB.getSingleResult());
When I switch the Primary Key column in the Named Query class, it works.
Before change (doesn't work):
@Id
@Column(name="TEST_ID1")
private BigDecimal testId1;
@Column(name="STRING_FIELD")
private String stringField;
After change (works):
@Id
@Column(name="STRING_FIELD")
private String stringField;
@Column(name="TEST_ID1")
private BigDecimal testId1;
But the Primary Key of the table is TEST_ID1 (in SQL below).
Have I done something wrong? How can I predict this behavior?
Any advice or pointers are greatly appreciated.
Thank you.
Oracle Database, EclipseLink 2.4.2
Table SQL:
DROP TABLE entity_test1;
/
DROP SEQUENCE entity_test1_seq;
/
CREATE TABLE entity_test1 (
test_id1 NUMBER(22) NOT NULL
,string_field VARCHAR2(255)
);
/
CREATE UNIQUE INDEX entity_test1_pk ON entity_test1 (test_id1);
/
ALTER TABLE entity_test1 ADD (
CONSTRAINT entity_test1_pk
PRIMARY KEY
(test_id1)
USING INDEX entity_test1_pk
ENABLE VALIDATE);
/
CREATE SEQUENCE entity_test1_seq START WITH 1000 INCREMENT BY 1;
/
INSERT INTO entity_test1 VALUES (entity_test1_seq.NEXTVAL,'Test1 Insert String1');
/
INSERT INTO entity_test1 VALUES (entity_test1_seq.NEXTVAL,'Test1 Insert String2');
/
COMMIT;
/
Table Entity:
@Entity
@Table(name="ENTITY_TEST1")
public class EntityTestImpl extends xxxEntity {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="ENTITY_TEST1_TESTID_GENERATOR", sequenceName="ENTITY_TEST1_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ENTITY_TEST1_TESTID_GENERATOR")
@Column(name="TEST_ID1")
private BigDecimal testId;
@Column(name="STRING_FIELD")
private String stringField;
public EntityTestImpl() {
}
public BigDecimal getTestId() {
return this.testId;
}
public void setTestId(BigDecimal testId) {
this.testId = testId;
}
public String getStringField() {
return this.stringField;
}
public void setStringField(String stringField) {
this.stringField = stringField;
}
}
Named Query Entity:
@Entity
@NamedNativeQuery(name="xxx.frameworks.test.data.ReadOnlyTest",
query="select /* xxx.frameworks.test.data.ReadOnlyTest */ " +
"et.string_field " +
",et.test_id1 " +
"from entity_test1 et " +
"where et.test_id1 = ? ",
resultClass=xxx.frameworks.test.data.ReadOnlyTest.class)
public class ReadOnlyTest extends xxxEntity
{
private static final long serialVersionUID = -1L;
@Id
@Column(name="TEST_ID1")
private BigDecimal testId1;
@Column(name="STRING_FIELD")
private String stringField;
public ReadOnlyTest() {
}
public BigDecimal getTestId1() {
return testId1;
}
public void setTestId1(BigDecimal testId1) {
this.testId1 = testId1;
}
public String getStringField() {
return stringField;
}
public void setStringField(String stringField) {
this.stringField = stringField;
}
}
[Updated on: Tue, 18 October 2016 18:55]
Report message to a moderator