Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Named Query after Update before Commit
Named Query after Update before Commit [message #1745881] Tue, 18 October 2016 18:53 Go to next message
Shannon Scott is currently offline Shannon ScottFriend
Messages: 3
Registered: June 2016
Junior Member
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

Re: Named Query after Update before Commit [message #1746304 is a reply to message #1745881] Wed, 26 October 2016 15:48 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1274
Registered: July 2009
Senior Member
Show the JPA code that changes the field and tries to read back the entity. JPA doesn't allow changing ID field values, so it isn't quite clear from your working version and non-working version what you are doing exactly.

It also isn't clear what EntityManager context you are reading and writing from. Please remember that there are 2 levels of caching that could be in effect, one at the EMF level (shared) and another at the EntityManager which operates like a transactional cache. Your query for raw data gets it directly from the database, so it implies when you are searching for the entity, you are getting it from a stale cache. You may need to clear your EntityManager - they are meant to be transactional and once an entity becomes managed in one, it won't pick up changes made in other EntityManagers without an explicit refresh operation.

[Updated on: Wed, 26 October 2016 15:49]

Report message to a moderator

Re: Named Query after Update before Commit [message #1747130 is a reply to message #1746304] Wed, 09 November 2016 20:22 Go to previous message
Shannon Scott is currently offline Shannon ScottFriend
Messages: 3
Registered: June 2016
Junior Member
I was not persisting the data where I thought it was being persisted. It was an error in the coding. Thank you for your gracious help.
Previous Topic:Lazy loaded relation after detach with cascade detach not detached.
Next Topic:How to create DB2 tables from entitys
Goto Forum:
  


Current Time: Fri Nov 24 11:26:06 GMT 2017

Powered by FUDForum. Page generated in 0.02095 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software