I did a smal example of how to handle jpa inheritance in play, it's
pretty simple and it's working ok, I'd just like to add a few
touches...
the parent class is Producto, with two child classes, Remera (t-shirt)
and Mochila (backpack), and an entity definint the product type
(TipoProducto)
1- I'm setting the product type manually, on a PrePersist method, but
I'd like to do it from the @DiscriminatorValue anootation, I just need
to know how to access the value of the annotation
2- being a java newbie, I'd like to know what would be the best way to
handle the error in case I can't find the product type in entity
TipoProdcucto
--
models.Producto
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING,
name="tipoDiscriminator")
public abstract class Producto extends Model {
public String codigo;
@ManyToOne
public TipoProducto tipoProducto;
public Integer stock;
@PrePersist
public abstract void initTipoProducto(); //force child clasess to
implement this method
public void setTipoProductoByCodigo(String codigo) {
TipoProducto tp = TipoProducto.find("codigo", codigo).first(); //
should handle error here!!!
if (tp!=null) this.tipoProducto = tp;
}
}
-----
models.Remera
@Entity
@DiscriminatorValue("remera")
public class Remera extends Producto {
public String color;
public String talle;
public void initTipoProducto() {
setTipoProductoByCodigo("remera"); // I've already defined it in
@DiscriminatorValue("remera")
}
}
---
I tried to do it on the chld constructor, but it gave me an error when
starting the framework, so I implemented it with a PrePersist
method...
thanks a lot