Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

JPA newbie question  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am inexperienced with JPA and J2EE, so please forgive me if the following has an obvious answer -- I can't figure out what it is.

I have a checkbox in a JSF (Primefaces) page that toggles a CHAR field in a database, from '0' to '1' and back again, using a method in a CDI bean. The bean is SessionScoped. The method is as follows:


This works, the first time: the field starts out at '0', this is changed to '1', and the change is persisted to the database. If I check later in the method, it's still '1'. But the next time the checkbox is clicked and the method is run, the field is read as '0', not '1', even though it is '1' in the database on disk. I have tried flushing the entity manager, evicting the cache, changing the cache settings, clearing the entity manager, everything I could think of, but nothing helps.

Any assistance with this would be greatly appreciated -- it's quite frustrating. Thanks in advance.

Matthew Fleming

 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your Specimen return 1/0 for whether it has been viewed or not? What is wrong with a boolean return type?
 
Matthew Fleming
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why does your Specimen return 1/0 for whether it has been viewed or not? What is wrong with a boolean return type?



Because Mysql last I checked doesn't have a boolean type. I might be wrong about this, but in any case it is irrelevant.

To reiterate, I am getting an entity instance by a method that should make it part of the persistence context, ie managed. I then change a field in this entity, and the change is saved automatically (via the container's transaction management) to the database, on disk. But when I return to this method and read the entity again, the field has the original value, not what is now in the database. I have tried detaching the entity and rereading it from disk -- still wrong. I have tried emptying all the caches, evicting the entity, then rereading -- still wrong. It just makes no sense.

Matthew Fleming
 
Bartender
Posts: 20728
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the word you need is "merge".

And yes, it is unfortunate, but many DBMS's don't have a native boolean data type. ORM systems sometimes have the ability to insert translators to cover for that deficiency, but it depends on the ORM.

It's a real pain backing JSF, since JSF checkboxes MUST reference boolean data, and therefore you have to implement a façade in front of an actual ORM model. JSF Converters don't handle that sort of thing.
 
Matthew Fleming
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:I believe the word you need is "merge".

And yes, it is unfortunate, but many DBMS's don't have a native boolean data type. ORM systems sometimes have the ability to insert translators to cover for that deficiency, but it depends on the ORM.

It's a real pain backing JSF, since JSF checkboxes MUST reference boolean data, and therefore you have to implement a façade in front of an actual ORM model. JSF Converters don't handle that sort of thing.



Thanks for this, but I still don't understand. The JSF page is not bound directly to an instance of the Specimens class. My debugging has shown that the method runs as expected and does what's expected the first time. But the field is changed only in the database record, not in the corresponding instance in memory. (Or to be more accurate, it is changed in the instance, but somehow changes back. Again, the JSF page is not bound to this instance, so nothing that happens in the page is directly chaning it. It can only be changed with this one method.

Matthew Fleming
 
Sheriff
Posts: 21743
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Fleming wrote:

Campbell Ritchie wrote:Why does your Specimen return 1/0 for whether it has been viewed or not? What is wrong with a boolean return type?



Because Mysql last I checked doesn't have a boolean type. I might be wrong about this, but in any case it is irrelevant.


You're right, but JPA will map boolean to 0/1 internally. Unless you're using native queries, there is no need to use 0/1.

Also, wouldn't 0/1 as ints (or bytes) be more logical than chars?
 
Matthew Fleming
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Matthew Fleming wrote:

Campbell Ritchie wrote:Why does your Specimen return 1/0 for whether it has been viewed or not? What is wrong with a boolean return type?



Because Mysql last I checked doesn't have a boolean type. I might be wrong about this, but in any case it is irrelevant.


You're right, but JPA will map boolean to 0/1 internally. Unless you're using native queries, there is no need to use 0/1.

Also, wouldn't 0/1 as ints (or bytes) be more logical than chars?



Yes, but again the main question is why is JPA (apparently) misbehaving.

Matthew
 
Tim Holloway
Bartender
Posts: 20728
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:
Also, wouldn't 0/1 as ints (or bytes) be more logical than chars?



Not if you're a Fortran person. The usual convention there was 'T' or 'F'.

0 and 1 are no more boolean than T or F or Y or N or any of many other commonly-used options, and binary 0 and 1 can be problematic in many cases. An extreme case is HTML, where non-text characters are not allowed at all except for spacing characters and even then mostly ignored.

If JPA automatically converts true/false to some assumed value like binary 1/0, it's doing more than other ORM systems I've struggled with.

On thing to remember when merging in JPA is that the merge() doesn't return the same ('==') object that you passed to merge. It returns an ".equals()" copy, but not (usually) an "==" copy. And in JPA, "equals" actually only applies to the primary key field(s).
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!