从 javabean class 更新数据库是否合适?

Is it appropriate to update database from a javabean class?

一些背景知识:我正在创建某种游戏。在该游戏中,玩家可以拥有一座房子。房子可能包含家具对象,并且这些家具可能设置了自定义纹理。一件家具可能包含不同数量的纹理。一件家具有几个插槽 (0 - x) 用于纹理。

房子class:

public class House {
      private List<Furniture> furniture;

      public House() {
           furniture = new ArrayList<Furniture>();
      }

      public List<Furniture> getFurniture() {
            return furniture;
      }

      public void addFurniture(Furniture furniture) {
             furniture.add(furniture);
      }
}

家具class:

 public class Furniture {
       private Map<Integer, Texture> textures;

       public Furniture() {
             textures = new HashMap<Integer, Texture>();
       }

       public void setTexture(int index, Texture texture) {
             textures.put(index, texture);
       }
 }

在玩家房屋家具管理界面中会有这样的东西:

List<Furniture> furniture = house.getFurniture();
// Let's assume that player selected one piece of furniture which he wants to edit
Furniture furniture = .... // This is the one he selected.
// Later on he chooeses a texture and I set it:
furniture.setTexture(some_index, new Texture(...));

问题

从 setTexture 方法内部更新我的数据库是否有意义?从数据保存 class?

中与数据存储交互是一种好习惯

根据我的经验,直接在这些 java bean 中进行更新不是一个好习惯。您应该使用 DAO 模式。 通过这种方式,您可以抽象出所有对模型的 CRUD 操作。

更多详细信息,请访问DAO pattern