Hello all,
I'm working on an application that needs to support configuration history we are thinking of doing this by adding validFrom and validTo dates to the join tables for our many to many relationships so that when the configuration changes we just need to update the join to point to the new linked object instead of creating a point in time copy of the related objects.
Basically we have:
Code:
@Entity
@Table(name = "product_group", schema = "public")
@FilterDef(name = "currentDateFilter",
parameters = @ParamDef(name = "currentDateFilterParam", type = "date"),
defaultCondition="validFrom<=:currentDateFilterParam AND (validTo>=:currentDateFilterParam OR validTo is null)")
public class ProductGroup
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 128)
private String name;
@Column(name = "description", length = 1024)
private String description;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "product_group_product", joinColumns = { @JoinColumn(name = "productgroupid") }, inverseJoinColumns = { @JoinColumn(name = "productid") })
@FilterJoinTable(name="currentDateFilter")
private Set<Product> products = new HashSet<Product>();
public Set<Product> getProducts()
{
return this.products;
}
public void setProducts(Set<Product> products)
{
this.products = products;
}
...
}
@Entity
@Table(name = "product", schema = "public")
public class Product
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "code", length = 32)
private String code;
@Column(name = "name", length = 128)
private String name;
@ManyToMany(mappedBy = "products", cascade = CascadeType.ALL)
private Set<ProductGroup> productGroups = new HashSet<ProductGroup>();
public Set<ProductGroup> getProductGroups()
{
return this.productGroups;
}
public void setProductGroups(Set<ProductGroup> productGroups)
{
this.productGroups = productGroups;
}
the join table in the data base looks like this (but has no Hibernate mapping / model because one was not needed)
Code:
product_group_product
(
productgroupid bigint NOT NULL,
productid bigint NOT NULL,
validfrom date,
validto date,
CONSTRAINT product_group_product_pkey PRIMARY KEY (productgroupid , productid ),
CONSTRAINT product_group_product_productgroupid_fkey FOREIGN KEY (productgroupid)
REFERENCES product_group (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT product_group_product_productid_fkey FOREIGN KEY (productid)
REFERENCES product (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
This works well when calling productGroup.getProducts() and i get the expected results based on the filter.
Is there any way to persist these objects and set the validFrom/validTo into the relationship without making a model for the join table?