agrison / johm Goto Github PK
View Code? Open in Web Editor NEWThis project forked from xetorthio/johm
JOhm is a Object-hash mapping library for Java for storing objects in Redis
License: MIT License
This project forked from xetorthio/johm
JOhm is a Object-hash mapping library for Java for storing objects in Redis
License: MIT License
Enable JOhm to connect with Redis Sentinels to achieve HA.
I'm trying to persist list fields, and they're not being persisted. It's as if I haven't even added the annotations to the list field. As far as annotations go, my models are almost exactly similar to the models in the test folder with differences in class names and the addition of the Hibernate annotations:
Item model:
@Model("item")
@Entity
@Table(name="item")
public class Item implements Serializable{
@redis.clients.johm.Id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long mId;
@Attribute
@Column(name="date_created")
private Date mDateCreated;
@CollectionList(of = Update.class)
@Indexed
@OneToMany(mappedBy="mItem", fetch=FetchType.EAGER)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.REMOVE})
private List<Updates> mUpdates;
public List<Update> getUpdates() {
return mUpdates;
}
public void setUpdates(List<Update> updates) {
mUpdates = updates;
}
}
Update model:
@Model("update")
@Entity
@Table(name="update")
public class Update implements Serializable {
@redis.clients.johm.Id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long mId;
@Attribute
@Column(name="date_created")
private Date mDateCreated;
}
I tested persisting the item with this code first:
*the Hibernate query is definitely retrieving an Item
with an mUpdates
field containing two Update
objects, both containing Date
objects in their mDateCreated
field
public void doThis() throws InterruptedException{
Session session = HibernateUtils.openSession();
session.beginTransaction();
Item item= (Item) session.createQuery("FROM Item WHERE mId = 100").uniqueResult();
session.getTransaction().commit();
session.close();
JOhm.save(item);
}
The Item
object was being saved as hash item:100
and the item:all
set was also being saved. But the hash was without the List<Update> mUpdates
field, and the individual update:<id>
hashes were not being persisted either. There was no sign of an Update
object in the redis database at all.
I then thought that maybe JOhm didn't persist List
fields along with the object they're contained in, but each object has to be saved individually and then the members of the List
had to be added to the object's list like in persistList()
in CollectionsTest.java
. So, I then tried this next code because it was more similar to the way it was done in your test method; aside from the Hibernate query:
public void doThis() throws InterruptedException{
//same hibernate query as before
Session session = HibernateUtils.openSession();
session.beginTransaction();
Item item = (Item) session.createQuery("FROM Item WHERE mId = 100").uniqueResult();
session.getTransaction().commit();
session.close();
List<Update> updates = item.getUpdates();
item.setUpdates(new ArrayList<>());
for (Update update : updates){
JOhm.save(update);
}
JOhm.save(item);
for (Update update : updates){
item.getUpdates().add(update);
}
}
After this code, all the Update
objects were being saved to the database, but there was no indication that they were linked to the Item
object that was saved -- no mUpdates
field in item:100
hash.
Any idea of what I may be doing wrong?
Was just testing out JOhm for the first time with JOhm.save()
:
public void doThis() {
Item item = new Item(100);
JOhm.save(item);
}
When I got this exception:
*I'm pretty sure this is an issue with JOhm because I can save to the Redis database successfully using Jedis.set()
and Jedis.hmset()
redis.clients.jedis.exceptions.JedisDataException: EXECABORT Transaction discarded because of previous errors.
at redis.clients.jedis.Protocol.processError(Protocol.java:117)
at redis.clients.jedis.Protocol.process(Protocol.java:142)
at redis.clients.jedis.Protocol.read(Protocol.java:196)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:233)
at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:239)
at redis.clients.jedis.Transaction.exec(Transaction.java:38)
at redis.clients.jedis.BinaryJedis.multi(BinaryJedis.java:1632)
at redis.clients.johm.Nest.multi(Nest.java:117)
at redis.clients.johm.JOhm.save(JOhm.java:321)
at redis.clients.johm.JOhm.save(JOhm.java:224)
at org.pollinator.DoClass.doThis(DoClass.java:66)
Here is the Item class:
*also a Hibernate model class
@Model
@Entity
@Table(name="item")
public class Poll implements Serializable{
@redis.clients.johm.Id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long mId;
public Item(){
}
public Item(long id){
mId = id;
}
}
After doThis()
is called, the item's id is added to a set in the Redis database called Item:all
before the exception is thrown, but that's all that is created -- I flushed the redis database before calling doThis()
. Anyone ever experience a similar issue?
I'm using Redis Desktop Manager to look at the stored keys and noticed that JOhm was leaving behind a bunch of orphaned keys after the parent objects were being deleted. They all seemed to be Collection indexes.
Test case:
@Model
public class Test {
@Id
public Long id;
@Attribute
public String name;
@CollectionList(of = TestMessage.class)
public List<TestMessage> messages;
}
@Model
public class TestMessage {
@Id
public Long id;
@Attribute
public String message;
}
Test test = new Test();
test.name = "Test";
JOhm.save(test);
TestMessage testMessage = new TestMessage();
testMessage.message = "HelloWorld";
JOhm.save(testMessage);
test.messages.add(testMessage);
JOhm.delete(Test.class, test.id, true, true);
String key = String.format("Test:%d:messages", test.id);
boolean keyexists = _jedisPool.getResource().exists(key);
LOGGER.info(String.format("Key Exists: %s", keyexists?"YES":"NO"));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.