JPA: Desing Pattern: Decorator – persist decorated objects

Today i thought about how i can persist objects, which are or can be decorated with other classes.

01

In this example i used a gym and its different tariff models, e.g. we have a basic tariff, which every sportsgirl or -man has to book, to this tariff you can add a Fitnessdrinks Flatrate. Now we want to persist the choosen tariff.

package de.professional_webworkx.blog.decorator;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import de.professional_webworkx.blog.decorator.classes.BasicTariff;
import de.professional_webworkx.blog.decorator.decorators.DrinksAdditional;

public class App 
{
    public static void main( String[] args )
    {
    	Component fitBase = new BasicTariff();
    	Component fitBasePlusDrinks = new DrinksAdditional(fitBase);
    	System.out.println(fitBasePlusDrinks);
    	
    	EntityManager em = Persistence.createEntityManagerFactory("DecoratorPU").createEntityManager();
    	em.getTransaction().begin();
    	em.persist(fitBase);
    	em.persist(fitBasePlusDrinks);
    	em.getTransaction().commit();
    	em.close();
    }
}
package de.professional_webworkx.blog.decorator;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "component")
public class Component implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 545076549324980083L;
	
	@Basic
	protected long id;
	protected String name;
	protected BigDecimal price;
	
	
	public Component() {
		this.name	= "base component";
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}


	public void setId(long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "PRICE")
	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	@Column(name = "NAME")
	public String getName() {
		return name;
	}
}

BasicTariff

package de.professional_webworkx.blog.decorator.classes;

import java.math.BigDecimal;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import de.professional_webworkx.blog.decorator.Component;

@Entity
@Table(name = "basictarif")
public class BasicTariff extends Component {

	/**
	 * 
	 */
	private static final long serialVersionUID = -171672308586431250L;

	public BasicTariff() {
		this.name	= "Basic Tarif";
		this.price	= new BigDecimal(16.90);
	}
}

DrinksAdditional

package de.professional_webworkx.blog.decorator.decorators;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.Table;

import de.professional_webworkx.blog.decorator.Component;

@Entity
@Table(name = "drinksadditional")
public class DrinksAdditional extends Component {

	/**
	 * 
	 */
	private static final long serialVersionUID = 520438775084665200L;
	private Component wrappedComponent;
	
	private int drinkCounter;
	
	public DrinksAdditional() {
		super();
	}
	
	public DrinksAdditional(final Component component) {
		this.wrappedComponent = component;
		this.name	= "Fitness Beverages Flatrate" + ", " + wrappedComponent.getName();
		this.price	= new BigDecimal(11.99+wrappedComponent.getPrice().doubleValue());
		this.drinkCounter	= 10;
	}
	
	public int getDrinkCounter() {
		return drinkCounter;
	}

	public void setDrinkCounter(int drinkCounter) {
		this.drinkCounter = drinkCounter;
	}

	@Override
	public String toString() {
		return wrappedComponent.getName() + ", " + getName() + ", Gesamtpreis: " + getPrice() + " EUR";
	}
}

I am open for better examples! Feel free to comment this article!