Need help with Hibernate Mapping

I am trying to create a dashboard for a homegrown solution that collects native performance related statistics of application servers. Here's the DDL for the data that's been gathered by various agents. I have skipped few columns and non-relevant tables for brevity.




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)
);


The dashboard should allow users to view reports based on any column(s) from these tables as part of the criteria. So I will generate Hibernate Criteria query based on the user's selection.

When I reverse engineered POJO, the Metric object looks something like this:


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

What I would like to do is expose Metric as the single POJO for the relationship of those tables above. So essentially you can get server's name, value, timestamp all through Metric POJO. This will simply the generation of Criteria queries and result set would always be a list of Metric objects.

I referred to this link - which would work fine for a one-to-one relationship between objects. But in my case, metric has one to many relationship with server_metric and so on... I am not sure how my mapping file for Metric table would look like to achieve the same

Any help would be appreciated...
Cheers!!


Your schema has many-to-many association between server and metric (via server_metric table) which in itself then has additional attributes (collection of values). Hibernate does not support such many-to-many mappings, so you'll have to decompose them into many-to-one mappings instead:

@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();
}

You can add appropriate @OneToMany declarations to Server and Metric if you want associations to be bi-directional; I'm not sure whether that makes sense logically.


我相信这太晚了,但对于任何追随者,我不认为你可以将Id作为原始图。

链接地址: http://www.djcxy.com/p/63676.html

上一篇: hibernate映射问题

下一篇: 需要Hibernate Mapping的帮助