Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
jpa inheritance sample...
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
sas  
View profile  
 More options Mar 26 2011, 3:33 pm
From: sas <open...@gmail.com>
Date: Sat, 26 Mar 2011 12:33:53 -0700 (PDT)
Local: Sat, Mar 26 2011 3:33 pm
Subject: jpa inheritance sample...
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sas  
View profile  
 More options Mar 26 2011, 5:03 pm
From: sas <open...@gmail.com>
Date: Sat, 26 Mar 2011 14:03:53 -0700 (PDT)
Local: Sat, Mar 26 2011 5:03 pm
Subject: Re: jpa inheritance sample...
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »