Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Database flyweight pattern with JPA and Hibernate
PostPosted: Sat Apr 01, 2017 11:37 am 
Newbie

Joined: Sat Aug 27, 2016 11:17 am
Posts: 12
We have a large immutable object graph constituted of many immutable entities (values from the model perspective) that are all uniquely identified by an id and a version.

E.g.

Code:
class DocumentSnapshot {
    final int id;
    final int version;
    final Set<ItemSnapshot> items;
    //...
}

class ItemSnapshot {
    final int id;
    final int version;
    //...
}


What we would like is to only have a single copy of every snapshots in the DB.

DocumentSnapshot ds = new DocumentSnapshot(1, 1, [ new ItemSnapshot(1, 1) ]);

save(ds); //should store both as the snapshots never been stored

ds = new DocumentSnapshot(1, 1, [ new ItemSnapshot(1, 1), new ItemSnapshot(2, 1) ]);

save(ds); //should only store new ItemSnapshot(2, 1) since the other entities would already exist in the DB.

If I was solving this issue with plain SQL I'd loop over the values in the graph, try to INSERT, catch UNIQUE CONSTRAINT violations and ignore them (all in one transaction), but I'm a bit puzzled on how this could be achieved in Hibernate. Please note that the only features we can use are XML mappings and general persistence hooks (we are actually programming in ColdFusion, which uses Hibernate for it's persistence mechanism under the hood).

One idea I'd have if I could implement the `Persistable` interface (in java) would have been to force Hibernate to always insert by overriding the `isNew` function and then perhaps somehow solve conflicts with DB triggers, but I can't.

Any ideas?


Top
 Profile  
 
 Post subject: Re: Hibernate: Flyweight pattern in DB
PostPosted: Mon Apr 03, 2017 4:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1114
Save is a legacy method, and ever since JPA has emerged, the preferred entity state transitions methods are persist and merge.

So, in your case, you need to make sure that the merge event is cascaded from the parent DocumentSnapshot to the child collection of ItemSnapshot.

Then, using merge, Hibernate can figure out which are the existing entries and which are the new ones.

If you are using immutable entities, you might also like to use the 2nd-level caching since you will save a lot of database roundtrips.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.