1

我正在尝试为本地解决方案创建一个仪表板,该解决方案收集应用程序服务器的本机性能相关统计信息。这是各种代理收集的数据的 DDL。为简洁起见,我跳过了几列和不相关的表格。




create table server (
   id                integer not null auto_increment,
   name              varchar(50) not null,

   primary key(id)
);

create table metric (
   id               integer not null auto_increment,
   name             varchar(50) not null,

   primary key (id)
);

create table server_metric (
   id             integer not null auto_increment,
   server_id      integer not null,
   metric_id      integer not null,

   constraint foreign key (server_fk) references server(id),
   constraint foreign key (metric_fk) references metric(id),

   primary key (id)
);

create table value (
   id                integer not null auto_increment,
   server_metric_id     integer not null,
   value                varchar(500) not null,
   collect_time         timestamp  not null,

   constraint foreign key (server_metric_fk) references server_metric(id)
   primary key (id)
);


作为标准的一部分,仪表板应允许用户根据这些表中的任何列查看报告。所以我会根据用户的选择生成 Hibernate Criteria 查询。

当我对 POJO 进行逆向工程时,Metric 对象看起来像这样:


private long id;
private String name;
private Set serverMetrics = new HashSet(0);
... constructors, getters, setters truncated ...

我想做的是将 Metric 公开为上面这些表关系的单个 POJO。所以本质上你可以通过 Metric POJO 获取服务器的名称、值、时间戳。这将简单地生成 Criteria 查询,结果集将始终是 Metric 对象的列表。

我提到了这个链接——它适用于对象之间的一对一关系。但在我的情况下,metric 与 server_metric 有一对多的关系,依此类推......我不确定我的 Metric 表的映射文件如何实现相同的

任何帮助将不胜感激......
干杯!

4

2 回答 2

1

server您的架构在和metric(通过表)之间具有多对多关联,而server_metric后者本身具有附加属性(值的集合)。Hibernate 不支持这种多对多映射,因此您必须将它们分解为多对一映射:

@Entity
public class Server {
    // id, name getters / setters
}

@Entity
public class Metric {
    // id, name getters / setters
}

@Entity
public class ServerMetric {
    // id

    @ManyToOne
    public Server getServer();

    @ManyToOne
    public Metric getMetric();

    @OneToMany(mappedBy = "serverMetric")
    public List<Value> getValues();
}

@Entity
public class Value {
    // id, value, collect_time

    @ManyToOne
    public ServerMetric getServerMetric();
}

如果您希望关联是双向的,您可以向 Server 和 Metric 添加适当的 @OneToMany 声明;我不确定这在逻辑上是否有意义。

于 2009-10-01T18:20:02.113 回答
0

我敢肯定为时已晚,但对于后来的任何人,我不相信您可以将 Id 映射为原语。

于 2011-02-24T15:43:59.600 回答