jpa inheritance sample...

133 views
Skip to first unread message

sas

unread,
Mar 26, 2011, 3:33:53 PM3/26/11
to play-framework
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

sas

unread,
Mar 26, 2011, 5:03:53 PM3/26/11
to play-framework
well, I kept searching around and found the following solution

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;
public Integer stockMinimo;
public Integer stockMaximo;

@PrePersist
public void initTipoProducto() { //force child clasess to implement
this method

Class<? extends Producto> clazz = this.getClass();

DiscriminatorValue discriminatorAnnotation =
(DiscriminatorValue) clazz.getAnnotation(DiscriminatorValue.class);

if (discriminatorAnnotation==null)
throw new RuntimeException("No @DiscriminatorValue annotation
defined in class '" + clazz.getName() + "'.");

String discriminatorValue = discriminatorAnnotation.value();

if ("".equals(discriminatorValue.trim()))
throw new RuntimeException("@DiscriminatorValue annotation cannot
be empty in class '" + clazz.getName() + "'.");

setTipoProductoByCodigo(discriminatorValue);

}

public void setTipoProductoByCodigo(String codigo) {
TipoProducto tp = TipoProducto.find("codigo", codigo).first();
if (tp==null) throw new RuntimeException("No TipoProducto found with
code '" + codigo + "'.");
this.tipoProducto = tp;
}

}

--

and child entities are just

models.Remera

@Entity
@DiscriminatorValue("remera")
public class Remera extends Producto {

public String color;
public String talle;

}

saludos

sas
Reply all
Reply to author
Forward
0 new messages