This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Persisting an AtomicLong field

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an AtomicLong field in my class



the counterpart field in the mysql table is BIGINT(20)

as far as the field moviefollowingCount is Long it works great. But as soon as I change it to AtomicLong it throws a SerializationException : could not deserialize while synchronizing the object with the db.

I googled everywhere ideally I should use a long to persist the class property.
Well the class has a field which is the count of people following a movie. The entity is movie. User & Movie have a ManyToMany relationship. Every time a user follows a movie the count gets incremented & updated in the db therefore I've made it AtomicLong as the count is shared amongst multiple users; just like facebook likes for a post.If you think my design is incorrect please advice.

If I make the field as Long how do I share it amongst several threads? Or do I just write a concurrent strategy for the object access & update to & fro from the db & just simply use the long variable?
 
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shivang sarawagi wrote:I googled everywhere ideally I should use a long to persist the class property.
Well the class has a field which is the count of people following a movie. The entity is movie. User & Movie have a ManyToMany relationship. Every time a user follows a movie the count gets incremented & updated in the db therefore I've made it AtomicLong as the count is shared amongst multiple users; just like facebook likes for a post.If you think my design is incorrect please advice.

If I make the field as Long how do I share it amongst several threads? Or do I just write a concurrent strategy for the object access & update to & fro from the db & just simply use the long variable?


Entities are explicitly not meant to be shared among multiple threads. That's because multi-threaded transactions are not supported. At some point the entity would need to be persisted, but from which thread and at what time?

I would make the field a long field with no (public) setter, making it virtually unchangeable. Then I would add a method to some bean that uses a JPQL query to update the count:

As a side note, JPA supports only a few types. Any type that does not match those will tried to be serialized to a byte[]. That's where you got the error from, because AtomicLong is not Serializable.
If you use JPA 2.1 you can use AttributeConverter, @Convert, @Converts and @Converter to define custom mappings to the supported types.

If I recall correctly, the supported types:
- primitives and primitive wrappers.
- BigInteger and BigDecimal
- enums.
- String, char[] and CharSequence.
- byte[] and objects implementing Serializable
- Calendar, java.util.Date, java.sql.Date, java.sql.Time and java.sql.Timestamp (support for classes in java.time will come in JPA 2.2 or later).
- Collection, List, Set and Map (either as @ElementCollection, @ManyToMany or @OneToMany).
- other entities (either as @ManyToOne or @OneToOne).
- embeddables.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic