lundi 2 juillet 2018

Persisting a composition interface in Hibernate

I have the following class structure:

public abstract class Creature{
   private String name;
   //strategy pattern composition
   private SkillInterface skill;
}

public interface SkillInterface {
   void attack();
}

public class NoSkill implements SkillInterface {
   @Override
   public void attack() {
       System.out.println("No skills");
   }
}

Subclasses of SkillInterface are without any fields. As they determine the behaviour, I would like to save these strategies as a column to some already existing table. I tried to implement it with @Converter annotation, using AttributeConverter class to convert SkillInterface to String and save, but always had mapping exceptions. I want to be able to save it as String and retrieve as SkillInterface reference.

But how can I implement it with Hibernate? Or do I have a design mistake?

Aucun commentaire:

Enregistrer un commentaire