【问题标题】:Multiple tables share one model class in play framework游戏框架中多个表共享一个模型类
【发布时间】:2013-08-21 06:19:56
【问题描述】:

我正在使用 Playframework 2.1.0 构建一个简单的 Web 应用程序。

现在我有一个问题:

我的数据库中有大约 20 个表,名称为 AB、DF、HD、ER 等。由于某些原因,它们的表格式完全相同,我不能只使用一张表来存储它们。我将一次性使用一个特定的表。

那么我可以只写一个模型类来指示这些表格的表格格式吗?怎么样?

如果可以,我应该如何编写代码以将模型类动态映射到数据库中的一个特定表?

【问题讨论】:

    标签: java playframework playframework-2.0 playframework-2.1 ebean


    【解决方案1】:

    我认为您可以编写一个包含所有列映射的抽象超类:

    @MappedSuperclass
    public abstract class AbstractModel {
        protected int id;
        protected String property1;
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_SEQ")
        @Column(name = "id")
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
             this.id = id;
        }
    
        @Column(name = "prop1")
        public String getProperty1() {
            return property1;
        }
    
        public void setProperty1(String property1) {
             this.property1 = property1;
        }
    }
    

    然后写具体的模型:

    @Entity
    @Table(name = "AB")
    public class AB extends AbstractModel {
        //some additional fields if you wish
        private String property2;
    
        @Column(name = "prop2")
        public String getProperty2() {
            return property2;
        }
    
        public void setProperty2(String property2) {
             this.property2 = property2;
        }
       //additional code here
    }
    

    所以所有的字段映射都应该被继承。 希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多