Hello fellow Hibernate users,
1. SituationHere is my situation. I am currently implementing a web version of a board game, and I want to store all the cards present in this game as reference data in a data base. In this table are stored all fixed (or common) pieces of data, like the name, the illustration, etc.
However, during the game each player can modify the state of the card, which needs to be reflected in the data base. Obviously I do not want to modify the reference data.
The Object Model for the card looks like
Code:
public class Card {
// Immutable data
public Long id;
public String name;
// Data that is specific to each card instance, modified in the game
public Integer counter;
}
What I would like to do is:
- to store, on one end, the reference data
- to store, on the other, end the "instanciated" cards, with would contain only the card aspects that are mutable.
- be able to create instances based on the reference data. Something like
Code:
// Load reference data
List<Card> referenceCards = getHibernateTemplate().findByNamedParam("from Card");
// Create instances
List<Card> instanceCards = copy(referenceCards);
// Store these in the DB, not overriding the reference data
getHibernateTemplate().save(instanceCards);
2. Considered approaches2.1 InheritenceI could create the "instances" as subclasses of the reference cards, but I don't see this working. Indeed, I do not want to duplicate the reference data each time I create a new instance.
2.2 Association tableSomething I have considered was:
- Create the reference table
Code:
ID NAME ...
1 Reference1
2 Reference2
- Create a table with all the instances of the cards
Code:
ID COUNTER ...
1 1
2 15
3 4
- Create an association table
Code:
INSTANCE_ID REFERENCE_ID
1 1
2 1
3 2
However:
- When loading an "instance" card from the DB, how to make sure that it looks like a Card object, with the data properly coming from the reference card?
- When creating the "instance" from the reference, I need to make sure I don't add data in the reference table, but instead simply add a link in the association table. For this, maybe specify that all the data also present in the reference table are not to be written, and manually create the link when creating the instance?
- How to be able to load Card reference data or Card instances as needed?
I came accross this post:
viewtopic.php?f=1&t=937968&p=2228092&hilit=prototype+pattern#p2228092 that mentioned the prototype design pattern, but this is not exactly what I need.
In the reference documentation, I saw these topics:
- Mapping a class more than once (http://docs.jboss.org/hibernate/stable/ ... entityname). As far as I understand, this could help me solve the last issue. When I want to load the reference data, I specify a given entity name (thus a given mapping file), and when I want to manipulate the instances, I specify another entity name. Is that correct?
- I also checked the inheritance mappings, but as mentioned above inheritance does not seem to work.
First of all, do you see an issue with the way I see things? Is there a better approach to my problem?
Would you happen to have any idea / advise as how to go on?
Please let me know if some information is missing.
Thanks a lot for your time,
--
Sébastien Tromp