Skip to content

Instantly share code, notes, and snippets.

@ssedano
Created October 24, 2012 10:23
Show Gist options
  • Save ssedano/3945358 to your computer and use it in GitHub Desktop.
Save ssedano/3945358 to your computer and use it in GitHub Desktop.
mapping
package com.abiquo.scheduler;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "machine", schema = "examples")
public class Machine
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Basic
private String name;
@Basic
private Integer cpuTotal;
@OneToMany(mappedBy = "machine", cascade = CascadeType.ALL)
private List<ComputeResources> computeResources = new ArrayList<ComputeResources>();
@Transient
private List<Datastore> datastores = new ArrayList<Datastore>();
@Transient
private Integer cpuUsed;
public Integer getId()
{
return id;
}
public void setId(final Integer id)
{
this.id = id;
}
public Integer getCpuTotal()
{
return cpuTotal;
}
public void setCpuTotal(final Integer cpuTotal)
{
this.cpuTotal = cpuTotal;
}
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
public List<ComputeResources> getComputeResources()
{
return computeResources;
}
public void setComputeResources(final List<ComputeResources> computeResources)
{
this.computeResources = computeResources;
}
public Integer getCpuUsed()
{
return cpuUsed =
computeResources != null && !computeResources.isEmpty() ? computeResources.get(0)
.getCpuUsed() : null;
}
public void setCpuUsed(final Integer cpuUsed)
{
this.cpuUsed = cpuUsed;
}
public List<Datastore> getDatastores()
{
return datastores;
}
public void setDatastores(final List<Datastore> datastores)
{
for (Datastore d : datastores)
{
ComputeResources r = new ComputeResources(this, d);
computeResources.add(r);
}
this.datastores = datastores;
}
public void addDatastore(final Datastore datastore)
{
ComputeResources r = new ComputeResources(this, datastore);
computeResources.add(r);
this.datastores.add(datastore);
}
}
package com.abiquo.scheduler;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "datastore", schema = "examples")
public class Datastore
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Basic
private String name;
@Basic
private int hdTotal;
@Transient
private Integer hdUsed;
@OneToOne(mappedBy = "datastore", cascade = CascadeType.ALL)
private ComputeResources computeResources;
@Transient
private Machine machine;
public int getHdTotal()
{
return hdTotal;
}
public void setHdTotal(final int hdTotal)
{
this.hdTotal = hdTotal;
}
public Integer getId()
{
return id;
}
public void setId(final Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
public ComputeResources getComputeResources()
{
return computeResources;
}
public void setComputeResources(final ComputeResources computeResources)
{
this.computeResources = computeResources;
}
public Integer getHdUsed()
{
return hdUsed = computeResources != null ? computeResources.getHdUsed() : null;
}
public Machine getMachine()
{
return machine;
}
public void setMachine(final Machine machine)
{
this.computeResources = new ComputeResources(machine, this);
this.machine = machine;
}
}
package com.abiquo.scheduler;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "compute_resources")
public class ComputeResources
{
@Embeddable
public static class Id implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1622028659861358483L;
@Column(name = "machine_id")
private Integer machineId;
@Column(name = "datastore_id")
private Integer datastoreId;
public Id()
{
}
public Id(final Integer machineId, final Integer datastoreId)
{
this.machineId = machineId;
this.datastoreId = datastoreId;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + (datastoreId == null ? 0 : datastoreId.hashCode());
result = prime * result + (machineId == null ? 0 : machineId.hashCode());
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Id other = (Id) obj;
if (datastoreId == null)
{
if (other.datastoreId != null)
{
return false;
}
}
else if (!datastoreId.equals(other.datastoreId))
{
return false;
}
if (machineId == null)
{
if (other.machineId != null)
{
return false;
}
}
else if (!machineId.equals(other.machineId))
{
return false;
}
return true;
}
}
@EmbeddedId
private Id id;
@Column(name = "cpuUsed", updatable = false)
private Integer cpuUsed;
@Column(name = "hdUsed", updatable = false)
private Integer hdUsed;
@ManyToOne
@JoinColumn(name = "machine_id", insertable = false, updatable = false)
private Machine machine;
@ManyToOne
@JoinColumn(name = "datastore_id", insertable = false, updatable = false)
private Datastore datastore;
public ComputeResources()
{
super();
this.id = new Id();
}
public ComputeResources(final Machine machine, final Datastore datastore)
{
super();
this.id = new Id(machine.getId(), datastore.getId());
this.cpuUsed = 0;
this.hdUsed = 0;
this.machine = machine;
this.datastore = datastore;
this.machine.getComputeResources().add(this);
this.datastore.setComputeResources(this);
}
public Integer getCpuUsed()
{
return cpuUsed;
}
public void setCpuUsed(final Integer cpuUsed)
{
this.cpuUsed = cpuUsed;
}
public Machine getMachine()
{
return machine;
}
public void setMachine(final Machine machine)
{
this.machine = machine;
}
public Integer getHdUsed()
{
return hdUsed;
}
public void setHdUsed(final Integer hdUsed)
{
this.hdUsed = hdUsed;
}
public Datastore getDatastore()
{
return datastore;
}
public void setDatastore(final Datastore datastore)
{
this.datastore = datastore;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment