I am not entirely sure how to explain what I want to do but I am hoping the title gives a hint
I have 2 entities:
Partner
Type
And, we want to introduce a join table between them where the partner -> Type relationship is managed by date. This join table is essentially a history record. Utilimately what I want from my partner entity is to have a method that returns the currently active type for a partner as:
Partner.getActiveType(); // select * from partner_type_history h, partner p, account_key_type t where p.partner_id = xxx and h.partner_id = p.partner_id and h.account_key_type_id = t.account_key_type_id and now() between h.effective_from and h.effective_to;
And have this all magically populated by Hibernate. I will post all relavent code below.
table definitionsCode:
CREATE TABLE `partner` (
`partner_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`partner_name` varchar(16) NOT NULL,
`partner_description` varchar(255) NOT NULL,
`partner_active` tinyint(1) NOT NULL,
PRIMARY KEY (`partner_id`),
UNIQUE KEY `partner_name` (`partner_name`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
Code:
CREATE TABLE `partner_type_history` (
`partner_id` int(11) unsigned NOT NULL,
`account_key_type_id` int(10) unsigned NOT NULL,
`effective_from` datetime NOT NULL,
`effective_to` datetime NOT NULL,
PRIMARY KEY (`partner_id`),
UNIQUE KEY `unq_account_key_type_id` (`account_key_type_id`),
KEY `FK_account_key_type_id` (`account_key_type_id`),
KEY `FK_partner_id` (`partner_id`),
CONSTRAINT `FK_account_key_type_id` FOREIGN KEY (`account_key_type_id`) REFERENCES `account_key_type` (`account_key_type_id`),
CONSTRAINT `FK_partner_id` FOREIGN KEY (`partner_id`) REFERENCES `partner` (`partner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Code:
CREATE TABLE `account_key_type` (
`account_key_type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_key_type_name` varchar(128) NOT NULL,
`account_key_type_value` varchar(100) NOT NULL,
`account_key_type_is_active` tinyint(1) unsigned NOT NULL DEFAULT '1',
`account_key_type_user_visible` tinyint(1) unsigned NOT NULL DEFAULT '1',
`account_key_type_redemption_limit` int(10) unsigned DEFAULT NULL,
`account_key_type_is_default` tinyint(1) DEFAULT '0',
`account_key_type_desc` text,
`account_key_type_url_override` int(11) DEFAULT NULL,
`account_key_type_email_content_id_redeem` int(11) DEFAULT NULL,
`user_friendly_description` varchar(255) DEFAULT NULL,
PRIMARY KEY (`account_key_type_id`),
UNIQUE KEY `unique_account_key_type_desc` (`account_key_type_name`)
) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8;
Entities:Code:
@Entity
@Table(name = "partner", catalog = "gaz_platform")
public class Partner {
private long id;
private String name;
private String description;
private boolean active;
private Set<PartnerPixel> pixels;
public Partner() {
}
public Partner(String name) {
this.name = name;
}
public Partner(String name, String description, boolean isActive) {
this.name = name;
this.description = description;
this.active = isActive;
}
@Id
@GeneratedValue
@Column(name = "partner_id", nullable = false, insertable = true, unique = true, length = 11, precision = 0)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Basic
@Column(name = "partner_name", nullable = false, insertable = true, updatable = true, unique = true, length = 16, precision = 0)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "partner_description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Basic
@Column(name = "partner_active")
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "partner_id", referencedColumnName = "partner_id")
public Set<PartnerPixel> getPixels() {
return pixels;
}
public void setPixels(Set<PartnerPixel> pixels) {
this.pixels = pixels;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Partner partner = (Partner) o;
if (active != partner.active) return false;
if (id != partner.id) return false;
if (description != null ? !description.equals(partner.description) : partner.description != null) return false;
if (name != null ? !name.equals(partner.name) : partner.name != null) return false;
if (pixels != null ? !pixels.equals(partner.pixels) : partner.pixels != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (active ? 1 : 0);
result = 31 * result + (pixels != null ? pixels.hashCode() : 0);
return result;
}
}
Code:
@Table(name = "account_key_type", catalog = "gaz_platform", schema = "")
@Entity
public class Type implements IActiveNow {
@Transient
private final Logger log = LoggerFactory.getLogger(Type.class);
private long id;
private String name;
private String value;
private Integer redemptionLimit;
private boolean active;
private boolean userVisible;
private boolean defaultType;
private String urlOverride;
private Content redeemEmailOverride;
private String desc;
private String friendlyDescription;
private Set<LoginAccessGroup> loginAccessGroups;
private Set<TypeData> data;
private Set<TypeBenefits> benefits;
private Set<TypeGameItem> items;
@Column(name = "account_key_type_id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "user_friendly_description", nullable = true, insertable = true, updatable = true, length = 255, precision = 0)
@Basic
public String getFriendlyDescription() {
return friendlyDescription;
}
public void setFriendlyDescription(String friendlyDescription) {
this.friendlyDescription = friendlyDescription;
}
@Column(name = "account_key_type_url_override")
@Basic
public String getUrlOverride() {
return urlOverride;
}
public void setUrlOverride(String urlOverride) {
this.urlOverride = urlOverride;
}
public void setRedeemEmailOverride(Content redeemEmailOverride) {
this.redeemEmailOverride = redeemEmailOverride;
}
@Column(name = "account_key_type_name", nullable = false, insertable = true, updatable = true, length = 128, precision = 0)
@Basic
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "account_key_type_value", nullable = false, insertable = true, updatable = true, length = 100, precision = 0)
@Basic
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Column(name = "account_key_type_is_active", nullable = false, insertable = true, updatable = true, length = 0, precision = 0)
@Basic
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@Column(name = "account_key_type_user_visible", nullable = false, insertable = true, updatable = true, length = 0, precision = 0)
@Basic
public boolean isUserVisible() {
return userVisible;
}
public void setUserVisible(boolean userVisible) {
this.userVisible = userVisible;
}
@Column(name = "account_key_type_redemption_limit", nullable = true, insertable = true, updatable = true, length = 10, precision = 0)
@Basic
public Integer getRedemptionLimit() {
return redemptionLimit;
}
public void setRedemptionLimit(Integer redemptionLimit) {
this.redemptionLimit = redemptionLimit;
}
@Column(name = "account_key_type_is_default", nullable = true, insertable = true, updatable = true, length = 0, precision = 0)
@Basic
public boolean isDefaultType() {
return defaultType;
}
public void setDefaultType(boolean defaultType) {
this.defaultType = defaultType;
}
@Column(name = "account_key_type_desc", nullable = true, insertable = true, updatable = true, length = 65535, precision = 0)
@Basic
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@OneToOne
@JoinColumn(name = "account_key_type_email_content_id_redeem", referencedColumnName = "email_content_id")
public Content getRedeemEmailOverride() {
return redeemEmailOverride;
}
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "account_key_type_id")
public Set<TypeData> getData() {
return data;
}
public void setData(Set<TypeData> data) {
this.data = data;
}
@OneToMany
@JoinTable(
name = "account_key_type_login_access_groups",
joinColumns = @JoinColumn(name = "account_key_type_id"),
inverseJoinColumns = @JoinColumn(name = "login_access_group_id")
)
public Set<LoginAccessGroup> getLoginAccessGroups() {
return loginAccessGroups;
}
public void setLoginAccessGroups(Set<LoginAccessGroup> loginAccessGroups) {
this.loginAccessGroups = loginAccessGroups;
}
@OneToMany
@JoinColumn(name = "code_type_id")
public Set<TypeBenefits> getBenefits() {
return benefits;
}
public void setBenefits(Set<TypeBenefits> benefits) {
this.benefits = benefits;
}
@OneToMany
@JoinColumn(name = "code_type_id")
public Set<TypeGameItem> getItems() {
return items;
}
public void setItems(Set<TypeGameItem> items) {
this.items = items;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Type type = (Type) o;
if (active != type.active) return false;
if (defaultType != type.defaultType) return false;
if (id != type.id) return false;
if (data != null ? !data.equals(type.data) : type.data != null) return false;
if (desc != null ? !desc.equals(type.desc) : type.desc != null) return false;
if (name != null ? !name.equals(type.name) : type.name != null) return false;
if (redemptionLimit != null ? !redemptionLimit.equals(type.redemptionLimit) : type.redemptionLimit != null)
return false;
if (value != null ? !value.equals(type.value) : type.value != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (redemptionLimit != null ? redemptionLimit.hashCode() : 0);
result = 31 * result + (active ? 1 : 0);
result = 31 * result + (defaultType ? 1 : 0);
result = 31 * result + (desc != null ? desc.hashCode() : 0);
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
@Override
@Transient
public boolean isActiveNow() {
return active;
}
}