I would like to ask for your help on a hibernate question.
We have a system where we load an object from our relational database and serialize it into JSON.
Our Class looks like this:
Code:
@Entity
@Table(name = "mytable")
public class MyClass implements Serializable {
@Id
@NotNull
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id = 0L;
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name = "parent_id", updatable = false)
private List<ChildObject> collection = new ArrayList<ChildObject>();
}
The object serialized into json might look a little like:
Code:
{
"id" :"12",
"collection": [
{
"id": 1,
"parent_id": "12"
},
{
"id": 2,
"parent_id": "12"
}
]
}
We modify the json in the client, and then send it back to the server. The json gets deserialized back into an object, and the database is updated via hibernate.
Code:
MyClass myObject = new JSONDeserializer< MyClass >().deserializeInto(json, new MyClass());
sessionFactory.getCurrentSession().update(myObject);
This works fine when updating properties, and adding objects to the collection. My only problem comes when I want to remove an object from the collection, and have it deleted from my database. I have looked for the solution to this everywhere and have tried many variations on Cascade annotations and the deleteOrphan = true annotation without success.
I recieve the following JSON from the client, and notice that the client has removed the first element in the collection:
Code:
{
"id" :"12",
"collection": [
{
"id": 2,
"parent_id": "12"
}
]
}
I deserialize the json back into my object and update via hibernate. The missing element is ignored, and remains in the database.
Code:
MyClass myObject = new JSONDeserializer< MyClass >().deserializeInto(json, new MyClass());
sessionFactory.getCurrentSession().update(myObject);
Perhaps I am going about this the wrong way, can someone please help me out.
Many thanks,
Dan