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?
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:
Update model:
I tested persisting the item with this code first:
*the Hibernate query is definitely retrieving an
Itemwith anmUpdatesfield containing twoUpdateobjects, both containingDateobjects in theirmDateCreatedfieldThe
Itemobject was being saved as hashitem:100and theitem:allset was also being saved. But the hash was without theList<Update> mUpdatesfield, and the individualupdate:<id>hashes were not being persisted either. There was no sign of anUpdateobject in the redis database at all.I then thought that maybe JOhm didn't persist
Listfields along with the object they're contained in, but each object has to be saved individually and then the members of theListhad to be added to the object's list like inpersistList()inCollectionsTest.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:After this code, all the
Updateobjects were being saved to the database, but there was no indication that they were linked to theItemobject that was saved -- nomUpdatesfield initem:100hash.Any idea of what I may be doing wrong?